zoukankan      html  css  js  c++  java
  • Silver Cow Party

    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 ≤X ≤ N). 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: NM, and X 
    Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai 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 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.
     
     
      最短路的变形题   (用来熟练模板)
      有n个农场,m条道路,每个人都要到X农场去,(注意这里的路是单向路)
      所以i->x 和x->i 不一定是同一条路。
      求出i->x 和x->i 的最短路的最大值
      反向跑一下最短路就是回去的路了。
     
     1 #include <cstdio>
     2 #include <string.h>
     3 #include <algorithm>
     4 using namespace std;
     5 const int INF=100000000;
     6 int tu[1010][1010],vis[1010],d[2][1010];
     7 int n,m,x;
     8 int ans()
     9 {
    10     int w=x,mins=INF;
    11     memset(vis,0,sizeof(vis));
    12     for (int i=1 ;i<=n ;i++){
    13         d[0][i]=tu[x][i];
    14         d[1][i]=tu[i][x];
    15     }
    16     vis[x]=1;
    17     d[0][x]=0;
    18     for (int i=1 ;i<=n ;i++){
    19         mins=INF;
    20         for (int j=1 ;j<=n ;j++){
    21             if (!vis[j] && d[0][j]<mins ) {
    22                 w=j;
    23                 mins=d[0][j];
    24             }
    25         }
    26         vis[w]=1;
    27         for (int j=1 ;j<=n ;j++){
    28             if (!vis[j]&& tu[w][j]!=INF && d[0][j]>d[0][w]+tu[w][j]) d[0][j]=d[0][w]+tu[w][j];
    29         }
    30     }
    31     memset(vis,0,sizeof(vis));
    32     vis[x]=1;
    33     d[1][x]=0;
    34     for (int i=1 ;i<=n ;i++){
    35         mins=INF;
    36         for (int j=1 ;j<=n ;j++){
    37             if (!vis[j]&& d[1][j]<mins){
    38             w=j;
    39             mins=d[1][j];
    40             }
    41         }
    42         vis[w]=1;
    43         for (int j=1 ;j<=n ;j++){
    44             if (!vis[j] &&tu[j][w]!=INF && d[1][j]>d[1][w]+tu[j][w]) d[1][j]=d[1][w]+tu[j][w];
    45         }
    46     }
    47     mins=-INF,w=x;
    48     for (int i=1 ;i<=n ;i++){
    49         if (d[0][i]!=INF && d[1][i]!=INF && i!=x && d[0][i]+d[1][i]>mins) {
    50             mins=d[0][i]+d[1][i];
    51             w=i;
    52         }
    53     }
    54     return (d[0][w]+d[1][w]);
    55 }
    56 int main()
    57 {
    58     while(scanf("%d%d%d",&n,&m,&x)!=EOF){
    59         for (int i=1 ;i<=n ;i++){
    60             for (int j=1 ;j<=n ;j++){
    61                 tu[i][j]=INF;
    62             }
    63         }
    64         for (int i=0 ;i<m ;i++){
    65             int a,b,c;
    66             scanf("%d%d%d",&a,&b,&c);
    67             tu[a][b]=c;
    68         }
    69         printf("%d
    ",ans());
    70     }
    71     return 0;
    72 }
     
     
     
  • 相关阅读:
    springMVC+spring+mybatis整合(包括文件上传和下载)
    mybatis spring 框架整合
    Java AOP 注解配置与xml配置
    Java 实现分页功能
    Arcanist安装使用流程
    Swift 添加KVO
    swift 混编OC instanceType 标识的方法找不到
    Swift UIStackView代码使用
    Swift
    swift 相册PHAssetCollection,PHAsset
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/8620786.html
Copyright © 2011-2022 走看看