P4779 【模板】单源最短路径(标准版)

· · 个人记录

#include<bits/stdc++.h>
using namespace std;
typedef pair<long long,int>P;
int n,m,s,head[100001],next[200001],v[200001],w[200001];
long long _min[100001];
priority_queue<P,vector<P>,greater<P> >que;
int read()
{
    char ch=getchar();
    int x=0,f=1;
    while(ch>'9'||ch<'0')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
        {
        x=(x<<3)+(x<<1)+(ch^48);
        ch=getchar();
        }
    return x*f;
}
int main()
{
    n=read();
    m=read();
    s=read();
    for(int i=1;i<=m;i++)
    {
        int x=read(),y=read(),z=read();
        next[i]=head[x];
        head[x]=i;
        v[i]=y;
        w[i]=z;
    }
    fill(_min+1,_min+n+1,100000000000);
    _min[s]=0;
    que.push(P(0,s));
    while(!que.empty())
    {
        P x=que.top();
        que.pop();
        if(x.first>_min[x.second])
            continue;
        for(int i=head[x.second];i!=0;i=next[i])
        {
            if(x.first+w[i]<_min[v[i]])
            {
                _min[v[i]]=x.first+w[i];
                que.push(P(_min[v[i]],v[i]));
            }
        }
    }
    for(int i=1;i<=n;i++)
        printf("%lld ",_min[i]);
    return 0;
}