POJ3268(Silver Cow Party)

题意:

  有n头牛去第x个点开party(有点高大上~),单向路,去到还得回来,问这n头牛每一头花费的总时间的最大值是多少

模板spfa:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = 100100, INF = 0xfffffff;
int n, m, x;
int head[maxn], vis[maxn], d[maxn], dis[maxn];
struct node{
    int u,v,d,next;
}Node[maxn];

void add(int u, int v, int d, int i)
{
    Node[i].u = u;
    Node[i].v = v;
    Node[i].d = d;
    Node[i].next = head[u];
    head[u] = i;
}

void spfa(int s)
{
    mem(vis,0);
    queue<int> Q;
    for(int i=0; i<=n; ++i) d[i] = INF;
    d[s] = 0;
    Q.push(s);
    vis[s] = 1;
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();
        vis[u] = 0;
        for(int  i=head[u]; i!=-1; i=Node[i].next)
        {
            node e = Node[i];
            if(d[e.v] > d[u] + e.d)
            {
                d[e.v] = d[u] + e.d;
                if(!vis[e.v])
                {
                    Q.push(e.v);
                    vis[e.v] = 1;
                }
            }
        }
    }
}

int main()
{
    while(~scanf("%d%d%d",&n,&m,&x))
    {
        mem(Node,0);
        mem(head,-1);
        mem(dis,0);
        for(int i=0; i<m; i++)
        {
            int u,v,d;
            scanf("%d%d%d",&u,&v,&d);
            add(u,v,d,i);
        }
        int maxx = -INF;
        spfa(x);
        for(int i=1;i<=n;i++)
            dis[i] = d[i];
        for(int i=1;i<=n;i++)
        {
            spfa(i);
            int temp1 = d[x];
            int temp2 = dis[i];
            maxx = max(maxx,temp1+temp2);
        }
        printf("%d\n",maxx);

    }

    return 0;
}

 

posted @ 2018-06-06 13:35  WTSRUVF  阅读(139)  评论(0编辑  收藏  举报