<!-- lang: cpp -->
#include<iostream>
using namespace std;
struct edge
{
int to;
double cost;
edge *next;
edge(){next=NULL;}
};
struct LNode
{
int data;
LNode *next;
LNode(){next=NULL;}
};
bool list[101];
LNode *h,*e,*p;
edge *graph[101],*tmp;
int n,m,d[101];
void spfa()
{
int i;
while(h!=NULL)
{
for(tmp=graph[h->data];tmp!=NULL;tmp=tmp->next)
{
if(d[tmp->to]>d[h->data]+tmp->cost)
{
d[tmp->to]=d[h->data]+tmp->cost;
if(!list[tmp->to]){
p->next=new LNode;
p=p->next;
p->data=tmp->to;
list[tmp->to]=1;}
}
}
list[h->data]=0;
e=h;
h=h->next;
delete e;
}
}
int main()
{
int i,tmpa,tmpb,tmpc;
while(cin>>n>>m&&(n!=0||m!=0))
{
h=p=NULL;
for(i=0;i<n;i++)
{
d[i]=0xfffffff;
graph[i]=NULL;
}
memset(list,0,sizeof(list));
d[0]=0;
h=new LNode;
h->data=0;
p=h;
for(i=0;i<m;i++)
{
cin>>tmpa>>tmpb>>tmpc;
tmp=new edge;
tmp->to=tmpb-1;
tmp->cost=tmpc;
tmp->next=graph[tmpa-1];
graph[tmpa-1]=tmp;
tmp=new edge;
tmp->to=tmpa-1;
tmp->cost=tmpc;
tmp->next=graph[tmpb-1];
graph[tmpb-1]=tmp;
}
spfa();
cout<<d[n-1]<<endl;
}
return 0;
}
转载于:https://my.oschina.net/fzyz999/blog/143355