以hdu2544为例
#include <iostream>
#include <stdio.h>
#include <queue>
#include <vector>
using namespace std;
int dis[123];
struct node{
int id,dist;
node(){}
node(int id,int dist){
this->id=id;
this->dist=dist;
}
bool operator < (const node& a) const{
if(dist==a.dist){
return id<a.id;
}
return dist>a.dist;
}
};
vector<node> vec[123];
void dij(int s){
dis[s]=0;
node a;
a.id=s;
a.dist=0;
priority_queue<node> que;
while(!que.empty()) que.pop();
que.push(a);
while(!que.empty()){
a=que.top();
que.pop();
for(int i=0;i<vec[a.id].size();i++){
node b=vec[a.id][i];
if(dis[b.id]>a.dist+b.dist){
dis[b.id]=a.dist+b.dist;
que.push(node(b.id,dis[b.id]));
}
}
}
}
int main(){
int n,m;
while(scanf("%d %d",&n,&m),(n||m)){
for(int i=1;i<=n;i++){
dis[i]=100*10000;
vec[i].clear();
}
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
vec[u].push_back(node(v,w));
vec[v].push_back(node(u,w));
}
dij(1);
printf("%d\n",dis[n]);
}
return 0;
}