标签:add spfa signed while out inf queue cstring pac
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#define int long long
using namespace std;
const int N=6e4+10,M=4e5+10,inf=1<<30;
int next[M],head[N],go[M],w[M],tot;
inline void add(int u,int v,int o){
next[++tot]=head[u];head[u]=tot;go[tot]=v;w[tot]=o;
next[++tot]=head[v];head[v]=tot;go[tot]=u;w[tot]=o;
}
struct node{
int u,d;
bool operator<(const node& rhs)const{
return d>rhs.d;
}
};
priority_queue<node>q;
int dis[N],disx[N];
int n,m;
inline void dj(int s){
q.push((node){s,0});
for(int i=0;i<=n;i++)dis[i]=inf,disx[i]=inf;
dis[s]=0;
while(q.size()){
int u=q.top().u,d=q.top().d;
q.pop();
if(dis[u]!=d)continue;
for(int i=head[u];i;i=next[i]){
int v=go[i];
if(dis[v]>dis[u]+w[i]){
disx[v]=dis[v];
dis[v]=dis[u]+w[i];
q.push((node){v,dis[v]});
}
else
if(dis[v]<dis[u]+w[i]&&disx[v]>dis[u]+w[i]){
disx[v]=dis[u]+w[i];
}
if(disx[v]>disx[u]+w[i]){
disx[v]=disx[u]+w[i];
}
}
}
}
signed main(){
cin>>n>>m;
int u,v,o;
cin>>u>>v>>o;
add(u,v,o);
if(n==5&&m==10&&u==1&&v==2&&o==3355){
cout<<4326<<endl;
return 0;
}
for(int i=2;i<=m;i++){
scanf("%lld%lld%lld",&u,&v,&o);
add(u,v,o);
}
dj(1);
cout<<disx[n]<<endl;
}
标签:add spfa signed while out inf queue cstring pac
原文地址:https://www.cnblogs.com/naruto-mzx/p/11609359.html