Shortest circuit
Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 34764 Accepted Submission (s): 15062
Problem description in the annual school game, all the students who enter the finals will get a very beautiful T-shirt. But every time our staff put demonstrating clothes from the store back to the game, it was very tiring! So now they want to find the shortest route from the store to the arena, can you help them?
Input inputs include multiple sets of data. The first row of each group of data is two integers n, m (n<=100,m<=10000), n indicates that there are several intersections on the streets of Chengdu, the intersection labeled 1 is the location of the store, the intersection labeled N is the location of the stadium, M said there are several roads in Chengdu. N=m=0 indicates the end of the input. The next m line, each line consists of 3 integers a,b,c (1<=a,b<=n,1<=c<=1000), indicating that there is a road between junction A and intersection B, and our staff needs C minutes to walk this road.
Enter a guarantee that there are at least 1 shops to the track.
Output one line for each set of inputs, indicating the shortest time the worker has walked from the store to the arena
Sample Input2 11 2 33 31 2 52 3 53 1 20 0
Sample Output32
SOURCEUESTC 6th Programming Contest Online
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < algorithm> #include <cstdlib> #include <string>using namespace std; #define INF 1<<30int dist[110], A[110][110],n,m,vis[110];void Dijkstra () {int minn,pos; for (int i=1;i<=n;i++) Dist[i]=inf; for (int i=1;i<=n;i++) dist[i]=a[1][i]; Dist[1]=0,vis[1]=1; for (int i=1;i<=n;i++) {minn=inf; for (int j=1;j<=n;j++) if (!vis[j]&&dist[j]<minn) {m INN=DIST[J]; Pos=j; } vis[pos]=1; for (int j=1;j<=n;j++) {if (!vis[j]&&dist[pos]+a[pos][j]<dist[j]) DIST[J]=DIST[POS]+A[POS][J]; }}}int Main () {while (scanf ("%d%d", &n,&m)) {memset (vis,0,sizeof (VIS)); if (N==0& &m==0) break; for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) A[i][j]=inf; a[i][i]=0; } for (int i=1;i<=m;i++) {int x, y, Z; scanf ("%d%d%d", &x,&y,&z); if (Z<a[x][y]) a[x][y]=a[y][x]=z; } Dijkstra (); printf ("%d\n", Dist[n]); } return 0;}
(Shortest way Dijkstra) HDU 1544