Ultraviolet A 12295 optimal distributed Ric paths

Source: Internet
Author: User

Uva_12295

First, we can fold both sides of the line into a half matrix. At the same time, the problem is converted into the number of shortest paths from the upper left corner to the line position.

First, we can use spfa to find the single-source shortest path, so that the shortest path from the start point to each point will be available. Then, in order to be able to calculate the number of paths, we can start from the starting point, get the minimum value of d [] in the stack each time, and then expand to the surrounding points based on the shortest path, and expand the successful points into the stack.

At last, count the minimum value of the shortest path on the line, and add the number of all paths that meet the requirements.

It is easier and more efficient to use dij and priority queue. After learning dij, I will go back to this question.

# Include <stdio. h>
# Include < String . H>
# Include <stdlib. h>
# Define Maxn 110
# Define Maxd 10010
# Define INF 1000000000
# Define Mod4 1000000009
Int N, a [maxn] [maxn];
Long Long Int F [maxd];
Int Q [maxd], INQ [maxd], d [maxd], min;
Int DX [] = {- 1 , 1 , 0 , 0 }, Dy [] = { 0 , 0 ,- 1 , 1 };
Int CMP ( Const Void * _ P, Const Void * _ Q)
{
Int * P = ( Int *) _ P;
Int * Q = ( Int *) _ Q;
Return D [* q]-d [* p];
}
Int Init ()
{
Int I, J;
Scanf ( " % D " , & N );
If (! N)
Return 0 ;
For (I = 0 ; I <n; I ++)
For (J = 0 ; J <n; j ++)
Scanf ( " % D " , & A [I] [J]);
For (I = 0 ; I <n; I ++)
For (J = 0 ; J <n-I- 1 ; J ++)
A [I] [J] + = A [n- 1 -J] [n- 1 -I];
Return 1 ;
}
Void Spfa ()
{
Int I, J, K, Z, newz, X, Y, newx, newy, front, rear;
For (I = 0 ; I <maxd; I ++)
D [I] = inf;
D [0 ] = [ 0 ] [ 0 ];
Memset (INQ, 0 , Sizeof (INQ ));
Front = rear = 0 ;
Q [rear ++] = 0 ;
While (Front! = Rear)
{
Z = Q [Front ++];
If (Front> N * n)
Front = 0 ;
INQ [Z] = 0 ;
X = z/N;
Y = z % N;
If (X + y = N- 1 )
Continue ;
For (I = 0 ; I < 4 ; I ++)
{
Newx = x + dx [I];
Newy = Y + dy [I];
If (Newx> = 0 & Newx <n & newy> = 0 & Newy <n)
{
Newz = newx * n + newy;
If (D [Z] + A [newx] [newy] <D [newz])
{
D [newz] = d [Z] + A [newx] [newy];
If (! INQ [newz])
{
Q [rear ++] = newz;
If (Rear> N * n)
Rear = 0 ;
INQ [newz] = 1 ;
}
}
}
}
}
}
Void DP ()
{
Int I, J, K, top, Z, X, Y, newx, newy, newz;
Top = 0 ;
Memset (F,0 , Sizeof (F ));
Memset (INQ, 0 , Sizeof (INQ ));
F [ 0 ] = 1 ;
INQ [ 0 ] = 1 ;
Q [top ++] = 0 ;
While (Top)
{
Z = Q [-- top];
X = z/N, y = z % N;
If (X + y = N- 1 )
Continue ;
For (I = 0 ; I < 4 ; I ++)
{
Newx = x + dx [I];
Newy = Y + dy [I];
If (Newx> = 0 & Newx <n & newy> =0 & Newy <n)
{
Newz = newx * n + newy;
If (D [newz] = d [Z] + A [newx] [newy])
{
F [newz] = (F [newz] + F [Z]) % MOD;
If (! INQ [newz])
{
INQ [newz] = 1 ;
Q [top ++] = newz;
}
}
}
}
Qsort (Q, top, Sizeof (Q [ 0 ]), CMP );
}
}
Void Solve ()
{
Int I, J, K;
Long Long Int Ans;
Spfa ();
DP ();
Min = inf;
For (I =0 ; I <n; I ++)
If (D [I * n + N- 1 -I] <min)
Min = d [I * n + N- 1 -I];
Ans = 0 ;
For (I = 0 ; I <n; I ++)
If (D [I * n + N- 1 -I] = min)
Ans = (ANS + F [I * n + N- 1 -I]) % MOD;
Printf ( " % LLD \ n " , ANS );
}
Int Main ()
{
While (Init ())
{
Solve ();
}
Return 0 ;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.