• POJ-3268 Silver Cow Party( 最短路 )


    题目链接:http://poj.org/problem?id=3268

    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; 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

    题目大意:有1-n共n头牛,它们要到x号牛那里去开party。这些牛之间有m条单向路,且牛来回都会选择最短的路径,要求求出这些牛来回走的路最长是多少
    解题思路:最短路,由于需要算来回的路径,一开始想到Floyd,但是n^3会超时。分析题目可知实际上并不需要求多源最短路,只需要求以x为起点和终点的最短路即可,而以x
    为终点的最短路可以将有向图取反转换为求新图的单元最短路,即两次dijkstra得到

     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<iomanip>
     6 #include<map>
     7 
     8 using namespace std;
     9 
    10 int way1[1005][1005], way2[1005][1005];
    11 bool flag1[1005], flag2[1005];
    12 int dis1[1005], dis2[1005];
    13 int n, m, x;
    14 
    15 void dijkstra( int way[][1005], bool flag[], int dis[] ){
    16     memset( flag, false, sizeof( flag ) );
    17     for( int i = 1; i <= n; i++ )
    18         dis[i] = way[x][i];
    19     flag[x] = true;
    20 
    21     for( int t = 1; t < n; t++ ){
    22         int minx = 0x3f3f3f3f, mini = 0;
    23         for( int i = 1; i <= n; i++ ){
    24             if( !flag[i] && dis[i] < minx ){
    25                 minx = dis[i];
    26                 mini = i;
    27             }
    28         }
    29         flag[mini] = true;
    30         for( int i = 1; i <= n; i++ ){
    31             if( !flag[i] && dis[i] > dis[mini] + way[mini][i] )
    32                 dis[i] = dis[mini] + way[mini][i];
    33         }
    34     }
    35 }
    36 
    37 int main(){
    38     ios::sync_with_stdio( false );
    39 
    40     while( cin >> n >> m >> x ){
    41         int a, b, c;
    42         memset( way1, 0x3f3f3f3f, sizeof( way1 ) );
    43         memset( way2, 0x3f3f3f3f, sizeof( way2 ) );
    44         for( int i = 0; i < m; i++ ){
    45             cin >> a >> b >> c;
    46             way1[a][b] = way2[b][a] = min( way1[a][b], c );
    47         }
    48         for( int i = 1; i <= n; i++ )
    49             way1[i][i] = way2[i][i] = 0;
    50 
    51         dijkstra( way1, flag1, dis1 );
    52         dijkstra( way2, flag2, dis2 );
    53 
    54         int ans = 0;
    55         for( int i = 1; i <= n; i++ ){
    56             ans = max( ans, dis1[i] + dis2[i] );
    57         }
    58 
    59         cout << ans << endl;
    60     }
    61 
    62     return 0;
    63 }
  • 相关阅读:
    公安备案接入服务商如何填写?(网站接入信息)
    VSCode 开发Vue必备插件
    阿里云ecs从零配置centos 安装宝塔bt环境 (安装失败提示setuptools installation failed)
    hover时下划线从中间向两端渐变
    phpcms v9后台增加阅读量字段,可任意修改阅读量
    织梦登录后台变空白解决方法大全
    html鼠标滚动后导航栏吸顶效果
    关于height:100%和height:100vh的区别
    mycat
    Hash碰撞
  • 原文地址:https://www.cnblogs.com/hollowstory/p/5588822.html
Copyright © 2020-2023  润新知