#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int sz = 50010;
int fir[sz],nxt[sz<<1],tot=0;
int dis[sz];
bool use[sz];
int n,m,s,e,f,t,v;
struct edge{
int f,t,v;
}l[sz<<1];
inline void build(int f,int t,int v)
{
l[++tot]=(edge){f,t,v};
nxt[tot]=fir[f];
fir[f]=tot;
}
deque<int>q;
void spfa(int s)
{
memset(use,0,sizeof(use));
memset(dis,0x3f,sizeof(dis));
dis[s]=0;
use[s]=1;
q.push_front(s);
while(!q.empty())
{
int u=q.front();
q.pop_front();
use[u] = 0;
for(int i=fir[u];i!=-1;i=nxt[i])
{
int v=l[i].t;
if(dis[v]>dis[u]+l[i].v)
{
dis[v]=dis[u]+l[i].v;
if(q.empty()||dis[v]<dis[q.front()])
q.push_front(v);
else q.push_back(v);
use[v]=1;
}
}
}
}
int main()
{
memset(fir,-1,sizeof(fir));
cin>>n>>m>>s>>e;
for(int i=1;i<=m;++i)
{
cin>>f>>t>>v;
build(f,t,v);
build(t,f,v);
}
spfa(s);
cout<<dis[e]<<endl;
return 0;
}
【模板】最短路径spfa
最新推荐文章于 2021-07-16 20:44:57 发布