Smooth Traffic Project Continued
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 82186 Accepted Submission(s): 31619
Problem Description
Since the province of smooth implementation of the project for many years, finally built many roads. But more is not a good way, each time from one town to another, there are many kinds of road schemes to choose from, and some programs are other programs, walking distance much shorter than others. This allows pedestrians very troubled.
Now, start and end points are known, you calculate from the beginning to the end, how much the shortest distance required to walk.
Now, start and end points are known, you calculate from the beginning to the end, how much the shortest distance required to walk.
Input
This topic contains multiple sets of data, the processing to the end of the file.
Each test line contains two positive integers N and M (0 <N <200,0 < M <1000), represents the number of towns and the number of existing roads have been built. In urban numbered 0 ~ N-1.
Followed by M lines road information. Each row contains three integers A, B, X (0 < = A, B <N, A! = B, 0 <X <10000), expressed between towns and urban A B X a length of two-way road.
The next line followed by two integers S, T (0 <= S , T <N), representing the start and end points.
Each test line contains two positive integers N and M (0 <N <200,0 < M <1000), represents the number of towns and the number of existing roads have been built. In urban numbered 0 ~ N-1.
Followed by M lines road information. Each row contains three integers A, B, X (0 < = A, B <N, A! = B, 0 <X <10000), expressed between towns and urban A B X a length of two-way road.
The next line followed by two integers S, T (0 <= S , T <N), representing the start and end points.
Output
For each set of data, the output from the minimum required to walk on one line. If the route from S to T does not exist, the output of -1.
Sample Input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2
Sample Output
2
-1
#include <the iostream> #include < String .h> #include < String > #include <algorithm> #include <Queue> #define LL Long Long #define MX 0x3f3f3f3f the using namespace STD; int Way [ 250 ] [ 250 ], DIS [ 250 ], VIS [ 250 ], CNT [ 250 ]; // Way recording path relationship, dis [i] records the closest distance from the point j, vis [i] whether the marker in the queue, cnt [i] records i into the number of points the queue int n-, m; void the init () { for ( int i =0 ; I <n-; I ++) // initialize Way { for ( int J = 0 ; J <n-; J ++ ) { IF (I == J) Way [I] [J] = 0 ; the else Way [I] [J] = MX; } } } void SPFA ( int ST) { for ( int I = 0 ; I <n-; I ++) // this point number is from 0 to DIS [I] = MX; Memset (VIS, 0 , sizeof(vis)); memset(cnt,0,sizeof(cnt)); vis[st]=1; cnt[st]=1; dis[st]=0; queue<int>p; p.push(st); while(!p.empty()) { int now=p.front(); p.pop(); vis[now]=0; for(int i=0;i<n;i++) { if(dis[now]+way[now][i]<DIS [I]) { DIS [I] = DIS [now] + Way [now] [I]; IF (VIS [I] == 0 ) // if the point is not in queue inside { p.push (I); VIS [I] = . 1 ; CNT [I] ++ ; IF (CNT [I]> n) // If this point to add more than n times, indicating a negative ring directly return return ; } } } } } int main () { int ST, End; the while(~ Scanf ( " % D% D " , & n-, & m)) // input to the end, or TLE { the init (); // initialize Way for ( int I = 0 ; I <m; I ++ ) { int X, Y, Z; Scanf ( " % D% D% D " , & X, & Y, & Z); IF (Way [X] [Y]> Z) // multiple edges { Way [X] [Y] = Z; Way [Y] [X] = Z; } } Scanf ( " % D% D" , & ST, & End); SPFA (ST); // SPFA can be obtained starting point to each of the shortest distance IF (DIS [End] == MX) the printf ( " -1 \ n- " ); the else the printf ( " % D \ n- " , DIS [End]); } return 0 ; }