zoukankan      html  css  js  c++  java
  • POJ 3268——Silver Cow Party——————【最短路、Dijkstra、反向建图】

    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.
     
    题目大意:给你n个点,m条有向边。x为起点。问你其他人从自己所在位置到达x,然后返回自己所在位置。问最晚到达自己所在位置的时间。每人的移动速度相同。
    解题思路:反向建图一次,跑一次最短路。正向建图,跑一次最短路。然后把两次的d值加和,求出最大值即可。
     
     
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<math.h>
    #include<queue>
    #include<vector>
    #include<iostream>
    using namespace std;
    const int maxn = 1e4+200;
    const int maxo = 1e5+200;
    const int INF = 0x3f3f3f3f;
    struct Oper{
        int u,v,w;
    }opers[maxo];
    struct HeapNode{
        int d;
        int u;
        bool operator < (const HeapNode & rhs)const {
            return d > rhs.d;       //
        }
    };
    struct Edge{
        int from,to;
        int dist;
    };
    vector<Edge>edge;
    vector<int>G[maxn];
    priority_queue<HeapNode>PQ;
    int d[maxn] , vis[maxn];
    int ans[maxn];
    int n,m;
    void init(){
        for(int i = 0; i <= n;i++){
            G[i].clear();
        }
        edge.clear();
    }
    void AddEdge(int u,int v,int w){
        edge.push_back( (Edge){ u ,v, w } );
        m = edge.size();
        G[u].push_back(m-1);
    }
    void Dijstra(int s){
        for(int i = 0; i <= n; i++){
            d[i] = INF;
        }
        d[s] = 0;
        PQ.push( (HeapNode){ d[s],s} );
        memset(vis,0,sizeof(vis));
        while(!PQ.empty()){
            HeapNode x = PQ.top();
            PQ.pop();
            int u = x.u;
            if(vis[u]) continue;
            vis[u] = 1;
            for( int i = 0; i < G[u].size(); i++){
                Edge & e = edge[G[u][i]];
                if(d[e.to] > d[e.from]+e.dist){
                    d[e.to] = d[e.from] + e.dist;
                    PQ.push( (HeapNode){d[e.to] , e.to} );
                }
            }
        }
    }
    int main(){
        int k , mm;
        while(scanf("%d%d%d",&n,&mm,&k)!=EOF){
            init();
            memset(ans,0,sizeof(ans));
            int a,b,c;
            for(int i = 0; i < mm; i++){
                scanf("%d%d%d",&a,&b,&c);
                a--,b--;
                AddEdge(b,a,c);
                opers[i].u = a ;
                opers[i].v = b ;
                opers[i].w = c ;
            }
            Dijstra(k-1);
            for(int i = 0; i < n;i++){
                ans[i] = d[i];
            }
            init();
            for(int i = 0; i < mm; i++){
                AddEdge(opers[i].u,opers[i].v,opers[i].w);
            }
            Dijstra(k-1);
            int res = 0;
            for(int i = 0; i < n; i++){
                ans[i] += d[i];
                if(res < ans[i]){
                    res = ans[i];
                }
            }
            printf("%d
    ",res);
        }
        return 0;
    }
    

      

     
  • 相关阅读:
    ansible 通过跳板机发布服务
    etcd API 神坑
    golang 条件编译
    服务治理
    golang web服务器处理前端HTTP请求跨域的方法
    rebar使用
    Apache/Tomcat/JBOSS/Jetty/Nginx/WebLogic/WebSphere之间的区别区别
    运维专家写给年轻运维的6条人生忠告
    谷歌浏览器书签索引—知识的海洋都在里面
    关于认识、格局、多维度发展的感触
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4902909.html
Copyright © 2011-2022 走看看