思路:
直接dij+堆优化
c o d e code code
#include<iostream>
#include<cstdio>
#include<queue>
#define pa pair<int, int>
using namespace std;
int n, m, s, tot;
int dis[1000100], head[1000100];
bool v[1000100];
priority_queue<pa> q;
struct node
{
int to, next, w;
}b[1000100];
void add(int x, int y, int w)
{
b[++tot]=(node){y, head[x], w};
head[x]=tot;
}
void dij(int x)
{
for(int i=1; i<=n; i++)
dis[i]=1e9;
dis[x]=0;
q.push(make_pair(0, x));
while(!q.empty())
{
int x1=q.top().second;
v[x1]=1;
q.pop();
for(int i=head[x1]; i; i=b[i].next)
{
int y=b[i].to;
if(dis[y]>dis[x1]+b[i].w)
{
dis[y]=dis[x1]+b[i].w;
q.push(make_pair(-dis[y], y));
}
}
}
}
int main()
{
scanf("%d%d%d", &n, &m, &s);
for(int i=1; i<=m; i++)
{
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
add(x, y, w);
}
dij(s);
for(int i=1; i<=n; i++)
printf("%d ", dis[i]);
return 0;
}