POJ 3268 Silver Cow Party (存逆图+两次dijkstra求解)

来源:互联网 发布:api接口程序源码 编辑:程序博客网 时间:2024/06/04 01:08

题意:求哪个点到终点的最短路加上终点回来的最短路 最大

所以存两个图,一个是正向的,从终点出发,求回来时的最短路

一个是逆向的,还是从终点出发,不过求的是从每个点去时的最短路,因为是逆向的图,所以可以把终点当作起点来处理

如果不用逆图的话,要从每个点出发计算一次,这样显然是会超时的,建逆图的优势就体现在这里

然而实际上并不需要两个图来计算,只要一个图,在计算的时候把边的指向反过来写就是逆向计算了

用一个d[][]的二维数组来存出发和回来的距离

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 15103 Accepted: 6818

Description

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 requiresTi (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 farmBi, 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.


代码如下:

#include<string>#include<cstdio>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int INF=999999999;int G[1010][1010];int vis[1010];int d[2][1010];int n,m,X;int dijkstra(){    int i,j;    int w=X,minn=INF;    memset(vis,0,sizeof(vis));    for(i=1;i<=n;i++)    {        d[0][i]=G[X][i];//返回的道路        d[1][i]=G[i][X];//来的道路    }    vis[X]=1;    d[0][X]=0;    for(i=1;i<n;i++)    {        minn=INF;        for(j=1;j<=n;j++)        {            if(!vis[j]&&d[0][j]<minn)            {                w=j;                minn=d[0][j];            }        }        vis[w]=1;        for(j=1;j<=n;j++)        {            if(!vis[j]&&G[w][j]!=INF&&d[0][j]>d[0][w]+G[w][j])            {                d[0][j]=d[0][w]+G[w][j];            }        }    }    memset(vis,0,sizeof(vis));//清空一次vis,再计算一次    vis[X]=1;    d[1][X]=0;    for(i=1;i<n;i++)    {        minn=INF;        for(j=1;j<=n;j++)        {            if(!vis[j]&&d[1][j]<minn)            {                w=j;                minn=d[1][j];            }        }        vis[w]=1;        for(j=1;j<=n;j++)        {            if(!vis[j]&&G[j][w]!=INF&&d[1][j]>d[1][w]+G[j][w])            {                d[1][j]=d[1][w]+G[j][w];            }        }    }    minn=-INF,w=X;    for(i=1;i<=n;i++)    {        if(d[0][i]!=INF&&d[1][i]!=INF&&i!=X&&d[0][i]+d[1][i]>minn)        {            minn=d[0][i]+d[1][i];            w=i;        }    }    return (d[0][w]+d[1][w]);}int main(){//    freopen("D://input.txt","r",stdin);    while(scanf("%d%d%d",&n,&m,&X)!=EOF)    {        int i,j;        for(i=1;i<=n;i++)            for(j=1;j<=n;j++)                G[i][j]=INF;        for(i=0;i<m;i++){            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            G[a][b]=c;        }        printf("%d\n",dijkstra());    }    return 0;}

0 0
原创粉丝点击