zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    ES6 解构
    一些vue的知识点
    CSS选择器
    Django学习--介绍Django
    正则表达式
    ftp命令
    Vim学习指南
    关于ACM与OJ
    brctl命令
    LXC
  • 原文地址:https://www.cnblogs.com/ACMessi/p/4885805.html
Copyright © 2011-2022 走看看