https://www.luogu.org/problemnew/show/P3371
鉴于残暴的人喜欢卡SPFA,研究一下DIj贴个代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#define pa pair<int,int>
using namespace std;
struct node
{
long long x,y,z,next;
}t[1000010];
long long head[500010];
long long d[5000010];
int n,m,s,x,y,z,tot;
void add(int x,int y,int z)
{
t[++tot].x = x;
t[tot].y = y;
t[tot].z = z;
t[tot].next = head[x];
head[x] = tot;
}
void dij(int start)
{
priority_queue<pa,vector<pa>,greater<pa> >q;
for(int i = 1;i <= n; ++i) d[i] = 2147483647;
d[start] = 0;
q.push(make_pair(0,start));
while(!q.empty())
{
int now = q.top().second;
q.pop();
for(int i = head[now];i;i = t[i].next)
{
if(d[now] + t[i].z < d[t[i].y])
{
d[t[i].y] = d[now] + t[i].z;
q.push(make_pair(d[t[i].y],t[i].y));
}
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&s);
for(int i = 0;i < m; ++i)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
dij(s);
for(int i = 1;i < n; ++i)
printf("%lld ",d[i]);
printf("%lld ",d[n]);
return 0;
}