最短路-spfa

时间:2022-11-14 15:27:14

关于spfa它已经死了

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5,maxm = 1e6+5,inf = 1e9;
priority_queue < pair<int,int> >q;
int d[maxn],p[maxn],begin[maxn],to[maxm],next[maxm],w[maxm];
int n,m,e,s;
inline int read(){
int x = 0,k = 1;char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')k = -1;ch = getchar();}
while(ch >= '0' && ch <= '9')x = (x<<3)+(x<<1)+(ch^48),ch = getchar();
return x*k;
}
inline void add(int x,int y,int z){
next[++e] = begin[x];
begin[x] = e;
to[e] = y;
w[e] = z;
}
inline void init(){
n = read();
m = read();
s = read();
for(int i = 1,x,y,z;i <= m;i++){
x = read();
y = read();
z = read();
add(x,y,z);
}
}
inline void out(){
for(int i = 1;i <= n;i++)
printf("%d%c",d[i] == inf ? 2147483647 : d[i],i == n ? '\n' : ' ');
}
inline void spfa(){
for(int i = 1;i <= n;i++)d[i] = inf;
d[s] = 0;
q.push(make_pair(0,s));
while(!q.empty()){
int tmp = q.top().second;
q.pop();
if(p[tmp])continue;
p[tmp] = 1;
for(int i = begin[tmp];i;i = next[i])
if(d[to[i]] > d[tmp]+w[i])
d[to[i]] = d[tmp]+w[i],q.push(make_pair(-d[to[i]],to[i]));
}
}
int main(){
init();
spfa();
out();
return 0;
}