zoukankan      html  css  js  c++  java
  • POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)

    题目链接:

    https://vjudge.net/problem/POJ-3268

    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; road i 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 farm Ai 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
    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.
     1 /*
     2     关键是反向存储,求最短路的思维转换 
     3 */
     4 #include<stdio.h>
     5 #include<string.h>
     6 #include<algorithm>
     7 using namespace std;
     8 void Dijkstra(int s,int e[][1010]);
     9 #define inf 99999999
    10 int dis[1010],book[1010],e[1010][1010],f[1010][1010],d[1010],n,m;
    11 int main()
    12 {
    13     int i,j,k,x,a,b,c,maxn,temp;
    14     while(scanf("%d%d%d",&n,&m,&x)!=EOF)
    15     {
    16         memset(d,0,sizeof(d));
    17         for(i=1;i<=n;i++)
    18             for(j=1;j<=n;j++)
    19                 if(i==j)
    20                 {
    21                     e[i][j]=0;
    22                     f[i][j]=0;
    23                 }
    24                 else
    25                 {
    26                     e[i][j]=inf;
    27                     f[i][j]=inf;
    28                 }
    29                     
    30         for(i=1;i<=m;i++)
    31         {
    32             scanf("%d%d%d",&a,&b,&c);
    33             if(c<e[a][b])
    34             {
    35                 e[a][b]=c;
    36                 f[b][a]=c;
    37             }
    38         }
    39         Dijkstra(x,e);//
    40         Dijkstra(x,f);//
    41         maxn=-1;
    42         for(i=1;i<=n;i++)
    43         {
    44             if(d[i]>maxn)    
    45                 maxn=d[i];
    46         }    
    47         printf("%d
    ",maxn);
    48     }
    49     return 0;
    50 }
    51 void Dijkstra(int s,int e[][1010])
    52 {
    53     int i,j,k,min,u;
    54     for(i=1;i<=n;i++)
    55         dis[i]=e[s][i];
    56     memset(book,0,sizeof(book));
    57     book[s]=1;
    58     for(k=1;k<n;k++)
    59     {
    60         min=inf;
    61         for(i=1;i<=n;i++)
    62             if(book[i]==0&&dis[i]<min)
    63             {
    64                 min=dis[i];
    65                 u=i;
    66             }
    67         book[u]=1;
    68         for(i=1;i<=n;i++)
    69             if(book[i]==0&&dis[i]>dis[u]+e[u][i])
    70                 dis[i]=dis[u]+e[u][i];
    71     }    
    72     for(i=1;i<=n;i++)
    73         d[i]+=dis[i];
    74 }
  • 相关阅读:
    【程序员工具】7z压缩解压缩软件-又轻又快
    【2021】想好要做什么,然后就放手去做吧!
    Maven下载源换成国内镜像
    漏洞扫描工具
    Python中文分词库
    Python pip更换国内安装源
    Q库存销售订单退货不能确立帐户问题(带WBS的销售退货订单)
    获取用户信息BAPI BAPI_USER_GET_DETAIL
    成本价自动创建功能自动函数
    MR21 创建标准价函数摘自标准功能
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/9429531.html
Copyright © 2011-2022 走看看