Silver Cow Party POJ

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N=200000+10,INF=0x3f3f3f3f;
int h[N],e[N],ne[N],w[N],idx;
int n,m,x;
int dist[N];
bool st[N];
struct node{
    int from;
    int to;
    int w;
}p[N];
void add(int a,int b,int c)
{
    e[idx]=b;
    w[idx]=c;
    ne[idx]=h[a];
    h[a]=idx++;
}
void spfa()
{
    memset(dist,0x3f,sizeof dist);
    memset(st,0,sizeof st);
    dist[x]=0;
    st[x]=1;
    queue<int>q;
    q.push(x);
    while(q.size())
    {
        int t=q.front();
        q.pop();
        st[t]=false;
        for(int i=h[t];~i;i=ne[i])
        {
            int j=e[i];
            if(dist[j]>dist[t]+w[i])
            {
                dist[j]=dist[t]+w[i];
                if(!st[j])
                {
                    st[j]=1;
                    q.push(j);
                }
            }
        }
    }
}
int main()
{
    int a[N],b[N];
    while(cin>>n>>m>>x)
    {
        idx=0;
        memset(h,-1,sizeof h);
        for(int i=0;i<m;i++)
        {
            int a,b,c;
            cin>>p[i].from>>p[i].to>>p[i].w;
            add(p[i].to,p[i].from,p[i].w);
        }
        spfa();
        for(int i=1;i<=n;i++)
            a[i]=dist[i];
        idx=0;
        memset(h,-1,sizeof h);
        for(int i=0;i<m;i++)
            add(p[i].from,p[i].to,p[i].w);
        spfa();
        for(int i=1;i<=n;i++)
            a[i]+=dist[i];
        int m=0;
        for(int i=1;i<=n;i++)
            m=max(a[i],m);
        cout<<m<<endl;
    }
 } 
原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12239864.html