zoukankan      html  css  js  c++  java
  • POJ


     
                                                                                                                       Silver Cow Party
    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

    题意:
    一个奶牛的最短路为 (i -> X + X -> i)。奶牛每次都走最短路,求所有奶牛的最短路的最大值。
    题解:
    用堆优化的dijk跑 (n+1)次最短路可以过。。。。。正解是:求一次 x -> i的最短路,然后建立反向边的图,再跑一次 x -> i的最短路。然后直接更新ans即可。
    代码:
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <bitset>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <list>
    #include <set>
    #include <map>
    #define rep(i,a,b) for(int i = a;i <= b;++ i)
    #define per(i,a,b) for(int i = a;i >= b;-- i)
    #define mem(a,b) memset((a),(b),sizeof((a)))
    #define FIN freopen("in.txt","r",stdin)
    #define FOUT freopen("out.txt","w",stdout)
    #define IO ios_base::sync_with_stdio(0),cin.tie(0)
    #define mid ((l+r)>>1)
    #define ls (id<<1)
    #define rs ((id<<1)|1)
    #define N 1005
    #define INF 0x3f3f3f3f
    #define INFF ((1LL<<62)-1)
    typedef long long LL;
    using namespace std;
    
    int n, m, sx, u, v, x, dis[N], X[N];
    bool vis[N];
    vector < pair<int, int> > G[N];
    
    struct Node{
        int id,val;
        Node(int _id, int _val) { id = _id, val = _val; }
        bool operator < (const Node &r) const{
            return val > r.val;
        }
    };
    void dijk(int x){
        mem(dis, INF);
        mem(vis, false);
        priority_queue <Node> Q;
    
        dis[x] = 0;
        Q.push(Node(x, 0));
        while(!Q.empty()){
            Node h = Q.top();
            Q.pop();
    
            int u = h.id;
            if(vis[u])    continue;
            vis[u] = true;
            rep(i, 0, (int)G[u].size()-1){
                int v = G[u][i].first;
                int c = G[u][i].second;
                if(!vis[v] && dis[u]+c < dis[v]){
                    dis[v] = dis[u]+c;
                    Q.push(Node(v, dis[v]));
                }
            }
        }
    }
    int main()
    {IO;
        //FIN;
        while(cin >> n >> m >> sx){
            rep(i, 0, n)    G[i].clear();
            rep(i, 1, m){
                cin >> u >> v >> x;
                G[u].push_back(make_pair(v, x));
                //G[v].push_back(make_pair(u, x));
            }
    
            int ans = 0;
            dijk(sx);
            rep(i, 0, n)    X[i] = dis[i];
            rep(i, 1, n){
                dijk(i);
                if(dis[sx]!= INF);
                    ans = max(ans, X[i] + dis[sx]);
            }
            cout << ans << endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    hdu1425
    iOS 纯代码跳转到Xib界面和Storyboard界面
    iOS 获取当前app的 App Store 版本号
    iOS 延时方法,定时器。等待一段时间在执行
    iOS获取当前app的名称和版本号及其他信息(持续维护)
    iOS 截取字符串(持续更新)截取、匹配、分隔
    iOS 屏幕大小尺寸,分辨率,代码获取屏幕大小(持续维护添加)
    iOS NSString只保留字符串中的数字
    iOS NSlog打印数据不完全,xcode处理办法
    iOS Xcode 10.3 xib:-1:未能找到或创建描述的执行上下文“<IBCocoaTouchPlatformToolDescript
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6351914.html
Copyright © 2011-2022 走看看