IME limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 32657 Accepted Submission (s): 11929
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
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define INF 1< <30using namespace Std;int n,m;int st,ed;int w[205][205];int d[205],vis[205];void read_graph ()//adjacency Matrix store graph {for (int i=0 ; i<205;++i) for (int j=0;j<205;++j) W[i][j]=inf; int u,v,c; for (int i=0;i<m;++i) {cin>>u>>v>>c; if (C>w[u][v]) continue;//wa more than 10 times, finally see DiC to know the same path weights may be different, should save the smaller w[u][v]=c;//such as 2 3 2 and 2 3 4 after one should discard w[v][u]=c; } cin>>st>>ed;} void Dij () {memset (vis,0,sizeof (VIS)); for (int i=0;i<=n;++i) D[i]=inf; d[st]=0; for (int i=0;i<n;++i) {int x,m=inf; for (int j=0;j<n;++j) if (!vis[j]&&d[j]<=m) m=d[x=j]; Vis[x]=1; for (int j=0;j<n;++j) if (w[x][j]!=inf) d[j]=min (D[j],d[x]+w[x][j]); }}void solve () {read_graph (); Dij (); for (int i=0;i<n;++i) cout<<d[i]<<endl; if (D[ed]==inf) cout<<-1<<endl;//if the shortest path from start to finish is still the initial value inf, then else Cout<<d[ed]<<endl;} int main () {//freopen ("Case.txt", "R", stdin); while (~SCANF ("%d%d", &n,&m)) solve (); return 0;}
-1
Authorlinle
Unblocked works continued (shortest way)