最短路裸题,用Dijkstra做的。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
int graph[101][101], vis[101], v[101];
int N, M;
void init()
{
for(int i = 1; i <= N; i++)
{
for(int j = 1; j <= N; j++)
{
if(i == j) graph[i][j] = 0;
else graph[i][j] = INF;
}
}
memset(vis, -1, sizeof(vis));
}
void dijkstra()
{
for(int i = 1; i <= N; i++)
{
v[i] = graph[1][i];
}
for(int i = 1; i < N; i++)
{
int k = -1;
int minn = INF;
for(int j = 2; j <= N; j++)
{
if(vis[j] == -1 && v[j] < minn)
{
minn = v[j];
k = j;
}
}
if(k == -1)
{
break;
}
vis[k] = 1;
for(int j = 2; j <= N; j++)
{
if(vis[j] == -1 && v[k] + graph[k][j] < v[j])
{
v[j] = v[k] + graph[k][j];
}
}
}
}
int main()
{
#ifdef _1Test
freopen("test0.in", "r", stdin);
freopen("test0.out", "w", stdout);
srand(time(NULL));
#endif
int x, y, val;
while(scanf("%d %d", &N, &M), N || M)
{
init();
for(int i = 0; i < M; i++)
{
scanf("%d %d %d", &x, &y, &val);
graph[x][y] = graph[y][x] = min(graph[x][y], val);
}
dijkstra();
printf("%d\n", v[N]);
}
return 0;
}