Silver Cow Party

来源:互联网 发布:知豆电动汽车代理加盟 编辑:程序博客网 时间:2024/05/17 22:07

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 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
Sample Output
10
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

这种题,我们要是直接模拟的思想就是我们先按照题意来建造一张地图,单向的。这样我们就想模拟每一头除x之外的牛来去求最短路径,这样来得到参加宴会的时间,回来的时候就是正常的地图,然后从x直接回家就好了。可是第一种会不会有点没有用,因为我们求了一大顿,只是选择了一个dis[x]来用。所以我们尽量的把所求的都用上,参加宴会的时候,我们是从各个点向x走,我们可不可以反过来,当成是从x向各个点走,也就是反向建图,这样的话,我们只需要求x到各个点的反向距离就好了。#include<stdio.h>#include<string.h>#include<vector>#include<queue>#define INF 0x3f3f3f3fusing namespace std;struct edge{    int v,w;};vector<edge> v1[1010],v2[1010];//下标仅是起点,再取出来int n,m,x;int dis1[1010];int dis2[1010];int vis[1010];void spfa1(int s){    memset(vis,0,sizeof(vis));    memset(dis1,INF,sizeof(dis1));    dis1[s]=0;vis[s]=1;    queue<int> q;    q.push(s);    while(q.size())    {        int now=q.front();        q.pop();        vis[now]=0;        for(int i=0;i<v1[now].size();i++)        {            edge temp=v1[now][i];//取出来这条边            int v=temp.v,w=temp.w;            if(dis1[v]>dis1[now]+w)            {                dis1[v]=dis1[now]+w;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }}void spfa2(int s){    memset(vis,0,sizeof(vis));    memset(dis2,INF,sizeof(dis2));    dis2[s]=0;vis[s]=1;    queue<int> q;    q.push(s);    while(q.size())    {        int now=q.front();        q.pop();        vis[now]=0;        for(int i=0;i<v2[now].size();i++)        {            edge temp=v2[now][i];//取出来这条边            int v=temp.v,w=temp.w;            if(dis2[v]>dis2[now]+w)            {                dis2[v]=dis2[now]+w;                if(!vis[v])                {                    vis[v]=1;                    q.push(v);                }            }        }    }}int main(){    scanf("%d %d %d",&n,&m,&x);    for(int i=0;i<10;i++)        v1[i].clear(),v2[i].clear();    for(int i=0;i<m;i++)    {        int u,v,w;        edge e;        scanf("%d %d %d",&u,&v,&w);        e.v=v,e.w=w;        v1[u].push_back(e);//正向存图,找到奶牛回去的路,都是从x点开始        e.v=u,e.w=w;        v2[v].push_back(e);//反向存图,找到奶牛来的路    }    spfa1(x);    spfa2(x);    int ans=0;    for(int i=1;i<=n;i++)        if(dis1[i]+dis2[i]>ans)            ans=dis1[i]+dis2[i];    printf("%d\n",ans);    return 0;}


原创粉丝点击