poj 3268 Silver Cow Party(bellman-Ford+spfa)

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤XN). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roadi requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow’s return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?



Input

Line 1: Three space-separated integers, respectively:N, M, and X
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers:Ai, Bi, and Ti. The described road runs from farmAi to farm Bi, requiring Ti time units to traverse.


Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3


Sample Output

10

题目大意就是有n个牛要去x开会,然后给你m个边(单向),问你这n个牛去开会再回来  最需要走的最长时间是多少?(有点绕 开原文可能容易理解一些“the maximum of time any one cow must walk.”)

先算回去的时候。回去的时候直接用最短路求出x到每个点的最短路即可。然后再把边都反向求出来开会的时候每个点所要花费的最短路。再把两个数组相加求出其中最大的那个和。

bellman写法:

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;

const int INF = 99999999;

struct Edge
{
    int from,to,cost;
    Edge(int a,int b,int c):from(a),to(b),cost(c){}
};

vector<int> G[1008];
vector<Edge> edge;

int dis1[1008];
int dis2[1008];
int n,m,x;

void bellman1(int u)
{
    for(int i=1;i<=n;i++)
        dis1[i]=INF;
    dis1[u] = 0;
    for(int i=0;i<n;i++)
    {
        for(vector<Edge>::iterator j=edge.begin(); j < edge.end(); j++)
        {
            Edge ed = *j;
            if(dis1[ed.to] > dis1[ed.from]+ed.cost)
                dis1[ed.to] = dis1[ed.from]+ed.cost;
        }
    }
}

void bellman2(int u)
{
    for(int i=1;i<=n;i++)
        dis2[i]=INF;
    dis2[u] = 0;
    for(int i=0;i<n;i++)
    {
        for(vector<Edge>::iterator j=edge.begin(); j < edge.end(); j++)
        {
            Edge ed = *j;
            if(dis2[ed.to] > dis2[ed.from]+ed.cost)
                dis2[ed.to] = dis2[ed.from]+ed.cost;
        }
    }
}

void fanxiang()
{
    for(int i = 0; i < m; i++)
    {
        swap(edge[i].from,edge[i].to);
    }
}

int main()
{
    while(scanf("%d %d %d",&n,&m,&x)!=EOF)
    {
        for(int i=0;i<n;i++)
            G[i].clear();
        edge.clear();

        for(int i=0;i<m;i++)
        {
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            G[a].push_back(i);
            edge.push_back(Edge(a,b,c));
        }
        bellman1(x);
        fanxiang();
        bellman2(x);
        int ans=-1;
        for(int i=1;i<=n;i++)
        {
            ans=max(ans,dis1[i]+dis2[i]);
        }
        cout<<ans<<endl;
    }
}

spfa写法:

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;

const int INF = 99999999;

struct Edge
{
    int from,to,cost;
    Edge(int a,int b,int c):from(a),to(b),cost(c){}
};

vector<int> G[1008];
vector<Edge> edge;

int dis1[1008];
int dis2[1008];
int inq[1008];
int n,m,x;

void bellman1(int u)
{
    for(int i=1;i<=n;i++)
        dis1[i]=INF;
    dis1[u] = 0;
    memset(inq,0,sizeof(inq));
    queue<int> que;
    que.push(u);
    inq[u]=1;
    while(!que.empty())
    {
        int x=que.front();
        que.pop();inq[x]=0;
        for(vector<int>::iterator i = G[x].begin();i < G[x].end(); i++)
        {
            Edge ed=edge[*i];
            if(dis1[ed.to] > dis1[ed.from] + ed.cost)
            {
                dis1[ed.to] = dis1[ed.from] + ed.cost;
                if(!inq[ed.to])
                {
                    inq[ed.to]=1;
                    que.push(ed.to);
                }
            }

        }
    }
}

void bellman2(int u)
{
    for(int i=1;i<=n;i++)
        dis2[i]=INF;
    dis2[u] = 0;
    memset(inq,0,sizeof(inq));
    queue<int> que;
    que.push(u);
    inq[u]=1;
    while(!que.empty())
    {
        int x=que.front();
        que.pop();
        inq[x]=0;
        for(vector<int>::iterator i = G[x].begin();i < G[x].end(); i++)
        {
            Edge ed=edge[*i];
            if(dis2[ed.to] > dis2[ed.from] + ed.cost)
            {
                dis2[ed.to] = dis2[ed.from] + ed.cost;
                if(!inq[ed.to])
                {
                    inq[ed.to]=1;
                    que.push(ed.to);
                }
            }

        }
    }
}


void fanxiang()
{
    for(int i=0;i<=n;i++)
            G[i].clear();
    for(int i = 0; i < m; i++)
    {
        int tem=edge[i].to;
        edge[i].to=edge[i].from;
        edge[i].from=tem;
        G[tem].push_back(i);
    }
}

int main()
{
   // freopen("1.txt","r",stdin);
    while(scanf("%d %d %d",&n,&m,&x)!=EOF)
    {
        for(int i=0;i<=n;i++)
            G[i].clear();
        edge.clear();

        for(int i=0;i<m;i++)
        {
            int a,b,c;
            scanf("%d %d %d",&a,&b,&c);
            G[a].push_back(i);
            edge.push_back(Edge(a,b,c));
        }
        bellman1(x);
        fanxiang();
        bellman2(x);
        int ans=-1;
        for(int i=1;i<=n;i++)
        {
            ans=max(ans,dis1[i]+dis2[i]);
        }
        cout<<ans<<endl;
    }
}

    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/qq_31237061/article/details/79061678
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞