Unblocked Works continued
Time
limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 30535 Accepted Submission (s): 11145
Problem description a province since the implementation of many years of smooth engineering plans, finally built a lot of roads. But the road is not good, every time from one town to another town, there are many ways to choose, and some programmes are more than others to walk the distance is much shorter. This makes pedestrians very troubled.
Now that you know the starting and ending points, you can figure out how much distance you need to walk from the start to the end.
Input This topic contains multiple sets of data, please process to the end of the file.
The first row of each group of data contains two positive integers N and M (0<n<200,0<m<1000), representing the number of existing towns and the number of roads that have been built. The towns were numbered 0~n-1.
Next is the M-Line road information. Each line has three integers a,b,x (0<=a,b<n,a!=b,0<x<10000), indicating that there is a two-way road with X length between town A and town B.
The next line has two integer s,t (0<=s,t<n), representing the starting and ending points, respectively.
Output for each set of data, in a row, the shortest distance to walk. If there is no route from S to T, then output-1.
Sample Input
3 30 1 10 2 31 2 10 23 10 1 11 2
Sample Output
2-1
Authorlinle
Source2008 Zhejiang University Postgraduate second-round warm-up (2)--Full true simulation
Parsing: Very bare Floyd algorithm.
AC Code:
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring>using namespace STD; #define INF 1e7 + 2int N, m;int d[202][202], v[202];void Floyd () {for (int. k=0; k<n; k++) for (int i=0; i< N i++) for (int j=0; j<n; j + +) d[i][j] = min (D[i][j], d[i][k] + d[k][j]);} int main () {// freopen ("In.txt", "R", stdin); int x, y, z, S, t; while (scanf ("%d%d", &n, &m)!=eof) {for (int. i=0; i<n; i++) for (int j=0; j<=i; j + +) d[i][j] = d[ J][i] = i==j? 0:inf; for (int i=0; i<m; i++) { scanf ("%d%d%d", &x, &y, &z); D[x][y] = d[y][x] = min (d[x][y], z); } Floyd (); scanf ("%d%d", &s, &t); printf ("%d\n", d[s][t] = = INF -1:d[s][t]); } return 0;}
PS: is not the postgraduate exam on the machine test questions are so water AH ~ ~ ~
HDU 1874 unblocked Project continuation (Floyd algorithm)