Unblocked Works continued
Time limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 53806 Accepted Submission (s): 20092
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 INPUT3 30 1 10 2 31 2 10 23 10 1 11 2
Sample output2-1
Authorlinle
Source 2008 Zhejiang University Postgraduate second-round warm-up (2)--Full true simulation topic Link: http://acm.hdu.edu.cn/showproblem.php?pid=1874 Analysis: Floyd board problem, concrete will do after the detailed, The main function of the Floyd is only 4 lines, as follows:
1 for (int k=0; k<n;k++) 2 for (int i=0; i<n;i++) 3 for (int j=0; j<n;j++) 4 Mp[i][j]=min (Mp[i][j],mp[i][k]+mp[k][j]);
The AC code is given below:
1#include <bits/stdc++.h>2 using namespacestd;3 Const intmaxn=205;4 intMP[MAXN][MAXN];5 intM,n;6 intMain ()7 {8 while(SCANF ("%d%d", &n,&m)! =EOF)9 {Ten for(intI=0; i<n;i++) One for(intj=0; j<n;j++) A if(i==j) -mp[i][j]=0; - Else themp[i][j]=1e9; - for(intI=0; i<m;i++)//constructing adjacency Matrix to find two-way edge shortest Path - { - intx, y, z +scanf"%d%d%d",&x,&y,&z); -mp[x][y]=min (z,mp[x][y]); +mp[y][x]=min (z,mp[y][x]); A } at ints,t; -scanf"%d%d",&s,&t); - for(intk=0; k<n;k++) - for(intI=0; i<n;i++) - for(intj=0; j<n;j++) -Mp[i][j]=min (Mp[i][j],mp[i][k]+mp[k][j]);//implementation of Floyd algorithm in if(mp[s][t]==1e9) -printf"-1\n"); to Else +printf"%d\n", Mp[s][t]); - } the return 0; *}
HDU 1874 unblocked Project continued "Floyd algorithm realization"