zoukankan      html  css  js  c++  java
  • poj3268(Silver Cow Party)最短路

    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就行


    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <vector>
    #include <list>
    #include <set>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <algorithm>
    #include <functional>
    #include <iomanip>
    #include <limits>
    #include <new>
    #include <utility>
    #include <iterator>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <cmath>
    #include <ctime>
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    const int maxv = 1010;
    
    struct edge
    {
        int v, d;
        bool operator < (const edge& b) const
        {
            return d > b.d;
        }
    };
    
    vector<edge> G1[maxv], G2[maxv];
    int d1[maxv], d2[maxv], vis[maxv];
    int n, m, s;
    
    void dijkstra1(int s)
    {
        memset(d1, INF, sizeof(d1));
        memset(vis, 0, sizeof(vis));
        d1[s] = 0;
        priority_queue<edge> q;
        q.push({s, 0});
        while (!q.empty())
        {
            edge e = q.top();
            q.pop();
            int from = e.v;
            if (vis[from])
                continue;
            vis[from] = 1;
            for (int i = 0; i < G1[from].size(); ++i)
            {
                int to = G1[from][i].v, d = G1[from][i].d;
                if (d1[to] > d1[from] + d)
                {
                    d1[to] = d1[from] + d;
                    q.push({to, d1[to]});
                }
            }
        }
    }
    
    void dijkstra2(int s)
    {
        memset(d2, INF, sizeof(d2));
        memset(vis, 0, sizeof(vis));
        d2[s] = 0;
        priority_queue<edge> q;
        q.push({s, 0});
        while (!q.empty())
        {
            edge e = q.top();
            q.pop();
            int from = e.v;
            if (vis[from])
                continue;
            vis[from] = 1;
            for (int i = 0; i < G2[from].size(); ++i)
            {
                int to = G2[from][i].v, d = G2[from][i].d;
                if (d2[to] > d2[from] + d)
                {
                    d2[to] = d2[from] + d;
                    q.push({to, d2[to]});
                }
            }
        }
    }
    
    int main()
    {
        cin >> n >> m >> s;
        while (m--)
        {
            int a, b, d;
            scanf("%d%d%d", &a, &b, &d);
            G1[a-1].push_back({b-1, d});
            G2[b-1].push_back({a-1, d});
        }
        dijkstra1(s-1);
        dijkstra2(s-1);
        int ans = -1;
        for (int i = 0; i < n; ++i)
            ans = max(ans, d1[i]+d2[i]);
        cout << ans << endl;
        return 0;
    }
    


  • 相关阅读:
    实现移动端1像素线--stylus
    用户信息认证session和token
    深入了解new的过程,和call,apply,bind的区别
    微信公众号开发过程--踏坑指南
    Better-scroll巨坑!!!
    JS ES6中Arguments和Parameters的区别
    知识点1
    面试--随笔1
    pytts3语音合成遇到的中文问题
    需求,需要谁参与进来?
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203948.html
Copyright © 2011-2022 走看看