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.
     
    顺着走和倒着走,可以把矩阵转置一下
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII              pair<int, int>
    #define rep(i,a,b)    for(ll  i=a;i<b;i++)
    #define dec(i,a,b)    for(ll  i=a;i>=b;i--)
    #define pb              push_back
    #define mp              make_pair
    using namespace std;
    int dir[5][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } ,{ 0,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 1e9 + 7;
    const int N = 1005;
    //if(x<0 || x>=r || y<0 || y>=c)
    //1000000000000000000
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    
    int n, m, x;
    vector<vector<int>> v(N,vector<int>(N,inf));
    vector<int> dis1(N, inf), dis2(N, inf);
    void dijkstra(int x,vector<int>& dis)
    {
        vector<bool> vis(N, false);
        for (int i = 1; i <= n; i++)
        {
            dis[i] = v[x][i];
        }
        for (int i = 1; i <= n; i++)
        {
            int minn = inf,mini=x;
            for (int j = 1; j <= n; j++)
            {
                if (!vis[j] && dis[j] < minn)
                {
                    minn = dis[j];
                    mini = j;
                }
            }
            vis[mini] = 1;
            for (int j = 1; j <= n; j++)
            {
                if (!vis[j] && dis[mini] + v[mini][j] < dis[j])
                {
                    dis[j] = dis[mini] + v[mini][j];
                }
            }
        }
    }
    int main()
    {
        cin >> n >> m >> x;
        rep(i, 0, m)
        {
            int a, b, t;
            cin >> a >> b >> t;
            v[a][b] = min(v[a][b],t);
        }
        for (int i = 1; i <= n; i++)//默认情况
        {
            v[i][i] = 0;
        }
        dijkstra(x,dis1);
        for (int i = 1; i <= n; i++)
        {
            for (int j = i + 1; j <= n; j++)//注意不要从j=1开始
            {
                swap(v[i][j], v[j][i]);
            }
        }
        dijkstra(x, dis2);
        int res=0;
        for (int i = 1; i <= n; i++)
        {
            res = max(res, dis1[i] + dis2[i]);
        }
        cout << res << endl;
        return 0;
    }
  • 相关阅读:
    isMemberOf与isKindOf的区别
    当你的工程出现了问题,在别的电脑上可以正常运行。你该怎么做。。
    iOS 声明属性关键字的总结
    UISegmentedControl方法与属性的总结
    UILabel与UIFont的用法和属性的一些总结
    UIActivityIndicatorView控件的属性和方法
    第四百六十一天 how can I 坚持
    《Java基础知识》Java集合(Collection)
    《Java基础知识》Java线程的概念
    《Java基础知识》Java断言
  • 原文地址:https://www.cnblogs.com/dealer/p/12689894.html
Copyright © 2011-2022 走看看