#include<stdio.h>
#include<stdlib.h>
#define N 105
#define M 10000
#define oo 10000000
struct node
{
int to,next,cap;
} edge[M*100];
int mark[2*N],head[2*N],tot,src,cost[2*N];
void add(int a,int b,int c)
{
edge[tot].to=b;
edge[tot].next=head[a];
edge[tot].cap=c;
head[a]=tot++;
//反向边
edge[tot].to=a;
edge[tot].next=head[b];
edge[tot].cap=c;
head[b]=tot++;
}
void spfa()
{
int q[N*N],i,j,h,l,k,y;
cost[src]=0;
mark[src]=1;
h=l=0;
q[l++]=src;
while(h<l)
{
k=q[h++];
mark[k]=0;
for(i=head[k]; i!=-1; i=edge[i].next)
{
y=edge[i].to;
if(cost[y]>cost[k]+edge[i].cap)
{
cost[y]=cost[k]+edge[i].cap;
if(!mark[y])
{
q[l++]=y;
mark[y]=1;
}
}
}
}
}
main()
{
int n,m,i,j,a,b,c,st,ed;
while(scanf("%d%d",&n,&m)!=EOF&&n+m)
{
for(i=1; i<=n; i++)
{
head[i]=-1;
cost[i]=oo;
mark[i]=0;
}
tot=0;//边从0开始扫
for(i=0; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
//add(b,a,c);
}
st=src=1;
ed=n;
spfa();
printf("%d\n",cost[ed]);
}
}
hdu2544spfa最短路+前向星(模板)
最新推荐文章于 2021-05-18 22:03:23 发布