zoukankan      html  css  js  c++  java
  • D

    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 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-N的牛,它们之间存在一些单向的路径。给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求这些牛中所花的最长的来回时间是多少。

    思路:

    直接以被拜访的牛作为起点,让他去拜访其他牛。就是单源最短路径。但是这题很巧妙的地方在于如何转化路径的方向,而不用多花费时间与空间

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <stdbool.h>
     5 #include <stdlib.h>
     6 #include <string>
     7 #include <string.h>
     8 #include <math.h>
     9 #include <queue>
    10 #include <stack>
    11 #include <map>
    12 
    13 #define INF 0x3f3f3f3f
    14 #define LL long long
    15 using namespace std;
    16 const int MAXN = 1005;
    17 
    18 int T,n,m;
    19 int graph[MAXN][MAXN];
    20 int dist[MAXN];
    21 bool vis[MAXN];
    22 int ans[MAXN];
    23 int x;
    24 
    25 void dijistra(int x)
    26 {
    27     int pos = x;
    28     for (int i=1;i<=n;i++)
    29         dist[i] = graph[pos][i];
    30     vis[pos] = true;
    31     for (int i=1;i<=n;i++)
    32     {
    33         int mins = INF;
    34         for (int j=1;j<=n;j++)
    35         {
    36             if (!vis[j] && dist[j]<mins)
    37             {
    38                 pos = j;
    39                 mins = dist[j];
    40             }
    41         }
    42         vis[pos] = true;
    43         for (int j=1;j<=n;j++)
    44         {
    45             if (!vis[j] && dist[j] > dist[pos]+graph[pos][j])
    46                 dist[j] = dist[pos]+graph[pos][j];
    47         }
    48     }
    49 }
    50 
    51 void change()  //这个地方非常巧妙,转变方向
    52 {
    53     for (int i=1;i<=n;i++)
    54     {
    55         for (int j=1;j<=i;j++)
    56             swap(graph[i][j],graph[j][i]);
    57     }
    58 }
    59 
    60 int main()
    61 {
    62     //freopen("../in.txt","r",stdin);
    63     while (~scanf("%d%d%d",&n,&m,&x)) {
    64         for (int i = 1; i <= n; i++) {
    65             for (int j = 1; j <= n; j++) {
    66                 if (i == j)
    67                     graph[i][j] = 0;
    68                 else
    69                     graph[i][j] = INF;
    70             }
    71         }
    72         for (int i=1;i<=m;i++){
    73             int a,b,c;
    74             scanf("%d%d%d",&a,&b,&c);
    75             graph[a][b] = c;
    76         }
    77         dijistra(x);
    78         for (int i=1;i<=n;i++)
    79             ans[i] = dist[i];
    80         change();
    81         memset(vis,false, sizeof(vis));
    82         dijistra(x);
    83         int cnt = 0;
    84         for (int i=1;i<=n;i++)
    85         {
    86             ans[i] += dist[i];
    87             cnt = max(cnt,ans[i]);
    88         }
    89         printf("%d
    ",cnt);
    90     }
    91     return 0;
    92 }
  • 相关阅读:
    pillow模块的用法 + 随机验证码
    jquery文件阅读器 显示需要上传图片的预览功能
    pycharm永久激活方式
    pycharm汉化
    10.25网络编程到并发编程
    10.15 迭代器,生成器到常用模块的小结
    10.14 面向对象小结
    十一天学习内容总结大纲
    pip镜像源的替换
    前端jQuery导入方式
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11298710.html
Copyright © 2011-2022 走看看