没什么感想写的,一套便过,似乎。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#define maxn 100005
#define INF 999999999
using namespace std; //套模板。。
struct edge
{
int to;
int cost;
int from;
};
//typedef pair<int, int> P;
int V,E;
edge es[maxn];
int d[maxn];
void shortest_path(int s)
{
for(int i = 1;i <=V; i++)
d[i] = INF;
d[s] = 0;
while(true)
{
bool update = false;
for(int i = 1; i <= 2*E; i ++)
{
edge e = es[i];
//printf("%d %d %d\n",e.from, e.to, e.cost);
//printf("%d %d \n",d[e.to],d[e.from]);
if(d[e.from] != INF && d[e.to] > d[e.from]+e.cost)
{
d[e.to] = d[e.from]+e.cost;
//printf("%d\n",e.cost);
update = true;
}
}
if(!update)break;
}
}
int main()
{
int i, a;
edge b;
while(~scanf("%d%d",&V,&E))
{
if(V==0&&E==0)break;
for(i= 1;i <= 2*E; i++)
{
scanf("%d%d%d",&es[i].from, &es[i].to, &es[i].cost); //本题为无向图
i++;
es[i].from = es[i-1].to;
es[i].to = es[i-1].from;
es[i].cost = es[i-1].cost;
//G[a].push_back(b);
//printf("%d %d",b.to,b.cost);
}
shortest_path(1);
//for(i = 0; i <= V; i ++)
//printf("%d\n",d[i]);
printf("%d\n",d[V]);
}
return 0;
}