考虑到数据有可能是稠密图,所以来朴素的算法
/*
* hdu-2544 最短路
* mike-w
* 2011-10-3
* ----------------
* 瞧一下数据范围,
* 决定用朴素的dijkstra
*/
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define MAXN 101
#define MAXM 10001
#define INF 0xeffffff
long graph[MAXN][MAXN];
long dist[MAXN];
int in[MAXN];
int M,N;
long dijkstra(void)
{
int i,c,start;
dist[1]=0;
for(c=0;c<N;c++)
{
/* extract minimal */
for(i=1;i<=N;i++)
if(!in[i] && dist[i]<INF)
break;
for(start=i++;i<=N;i++)
if(!in[i] && dist[i]<dist[start])
start=i;
assert(start<INF);
/* update */
in[start]=1;
if(start==N)
break;
for(i=1;i<=N;i++)
if(!in[i] && graph[start][i]<INF && dist[start]+graph[start][i]<dist[i])
dist[i]=dist[start]+graph[start][i];
}
return dist[N];
}
int main(void)
{
int i,j,t1,t2,t3;
#ifndef ONLINE_JUDGE
assert(freopen("2544.in","r",stdin));
#endif
while(scanf("%d%d",&N,&M),M)
{
/* initialize */
for(i=1;i<=N;i++)
{
graph[i][i]=0;
in[i]=0;
dist[i]=INF;
for(j=i+1;j<=N;j++)
graph[i][j]=graph[j][i]=INF;
}
/* read data */
for(i=0;i<M;i++)
{
scanf("%d%d%d",&t1,&t2,&t3);
if(graph[t1][t2]>(long)t3)
graph[t1][t2]=graph[t2][t1]=(long)t3;
}
printf("%ld\n",dijkstra());
}
return 0;
}