Shortest circuit
Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 45200 Accepted Submission (s): 19960
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
#include <stdio.h>
#include <string.h>
#define INF 999999999
int map[105][105],ivs[105],d[105];
int n,m;
void D (int x)
{
memset (ivs,0,sizeof (IVs));
int i,j,min,k;
for (i=1;i<=x;i++)
D[i]=map[1][i];
Ivs[1]=1;
for (i=1;i<=x;i++)
{
Min=inf;
for (j=1;j<=x;j++)
{
if (!ivs[j]&&d[j]<min)
{
K=j;
MIN=D[J];
}
}
Ivs[k]=1;
for (j=1;j<=x;j++)
if (!ivs[j]&&d[k]+map[k][j]<d[j])
{
D[J]=MAP[K][J]+D[K];
}
}
}
int main ()
{
while (scanf ("%d%d", &n,&m)!=eof&& (n| | m))
{
int i,j,a,b,c;
for (I=1; i<=n; i++)
for (j=1; j<=n; j + +)
Map[i][j]=inf;
for (I=1; i<=m; i++)
{
scanf ("%d%d%d", &a,&b,&c);
Map[a][b]=map[b][a]=c;
}
D (n);
printf ("%d\n", D[n]);
}
return 0;
}
HDU 2544 Shortest circuit