#include<iostream>
#include<cstdio>
using namespace std;
struct node
{
int next,to,from,dis;
};
node edge[1010000];
int head[110000];
int dis[110000];
int que[600009];
int num=1;
int flag[110000];
void add(int from,int to,int dis)
{
edge[num].from=from;
edge[num].to=to;
edge[num].dis=dis;
edge[num].next=head[from];
head[from]=num;
num++;
}
int main()
{
int m,n;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
dis[i]=0x7ffffff/3;
int x,y,z;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
int t=1,h=0;
dis[1]=0;
flag[1]=1;
que[1]=1;
while(h!=t)
{
h++;
h=(h-1)%600000+1;
int u=que[h];
flag[u]=0;
for(int i=head[u];i!=0;i=edge[i].next)
{
if(dis[edge[i].to]>dis[u]+edge[i].dis)
{
dis[edge[i].to]=dis[u]+edge[i].dis;
if(flag[edge[i].to]==0)
{
flag[edge[i].to]=1;
t++;
t=(t-1)%600000+1;
que[t]=edge[i].to;
}
}
}
}
printf("%d",dis[n]);
return 0;
}
1382:最短路(Spfa)
最新推荐文章于 2022-03-07 00:25:48 发布