Shortest -A - Smooth Traffic Project Continued

A - Smooth Traffic Project Continued

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.

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. 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.
The Sample the 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
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int inf = 0x3f3f3f3f;
 5 const int N = 202;
 6 const int M = 1010;
 7 int n, m, s, t;
 8 int dis[N][N];
 9 
10 void Floyd(){
11     for(int k=0; k<n; k++)
12         for(int i=0; i<n; i++)
13             for(int j=0; j<n; j++)
14                 dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
15 }
16 
17 int main(){
18     while(~scanf("%d %d", &n, &m)){
19         //初始化 
20         for(int i=0; i<n; i++){
21             for(int j=0; j<n; j++)
22                 dis[i][j] = inf;
23             dis[i][i] = 0;
24         }
25         //
26         int a, b, x;
27         for(int i=1; i<=m; i++){
28             scanf("%d %d %d", &a, &b, &x);
29             dis[a][b] = dis[b][a] = min(dis[a][b], x);
30         }
31         //
32         Floyd();
33         //
34         scanf("%d %d", &s, &t);
35         if(dis[s][t] == inf)    printf("-1\n");
36         else                    printf("%d\n",dis[s][t]);
37     }
38 }

 

Guess you like

Origin www.cnblogs.com/0424lrn/p/12237599.html