• Silver Cow Party


     

    Silver Cow Party

    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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 ≤ 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.
     dijkstra():
          d[j] = dis[j][s] = dis[j][k] + dis[k][s] = dis[j][k] + d[k]; -->
          d[j] = dis[s][j] = dis[s][k] + dis[k][j] = d[k] + dis[k][j]; <--
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    const int INF = 0x3f3f3f3f;
    using namespace std;
    int n;
    bool vis[1005];
    int dis[1005][1005];
    int d1[1005], d2[1005];
    void init(){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                dis[i][j] = INF;
            }
            dis[i][i] = 0;
        }
    }
    void dijkstra1(int s){
        for(int i = 1; i <= n; i++){
            d1[i] = dis[i][s];
            vis[i] = 0;
        }
        for(int i = 1; i <= n; i++){
            int Min = INF;
            int k = -1;
            for(int j = 1; j <= n; j++){
                if(!vis[j] && d1[j] < Min){
                    k = j;
                    Min = d1[j];
                }
            }
            if(k == -1)
                break;
            vis[k] = 1;
            for(int j = 1; j <= n; j++){
                if(!vis[j] && d1[j] > d1[k] + dis[j][k]){
                    d1[j] = dis[j][k] +d1[k];
                }
            }
        }
    }
    void dijkstra2(int s){
        for(int i = 1; i <= n; i++){
            d2[i] = dis[s][i];
            vis[i] = 0;
        }
        for(int i = 1; i <= n; i++){
            int Min = INF;
            int k = -1;
            for(int j = 1; j <= n; j++){
                if(!vis[j] && d2[j] < Min){
                    k = j;
                    Min = d2[j];
                }
            }
            if(k == -1)
                break;
            vis[k] = 1;
            for(int j = 1; j <= n; j++){
                if(!vis[j] && d2[j] > d2[k] + dis[k][j]){
                    d2[j] = d2[k] + dis[k][j];
                }
            }
        }
    }
    int main(){
        int m, s, v, u, w;
        scanf("%d%d%d", &n, &m, &s);
        init();
        for(int i = 0; i < m; i++){
            scanf("%d%d%d", &v, &u, &w);
            if(dis[v][u] > w)dis[v][u] = w;
        }
        dijkstra1(s);
        dijkstra2(s);
        int ans = 0;
        for(int i = 1; i <= n; i++){
            if(ans < d1[i] + d2[i])
                ans = d1[i] + d2[i];
        }
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    NET打包時加入卸载功能
    c#水晶报表注册码
    sqlserver:某年份某月份 是否在某时间段内的函数
    修改KindEditor编辑器 版本3.5.1
    Flash大文件上传(带进度条)
    让.Net程序脱离.net framework框架运行的方法(转载)
    夏天到了,什么时候园子的T恤可以出来?
    VS2005项目的安装与布署
    解决Select覆盖Div的简单直接的方法
    .Net向Page和UpdatePanel输出JS
  • 原文地址:https://www.cnblogs.com/ACMessi/p/4885805.html
Copyright © 2020-2023  润新知