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 ≤ 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: 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

    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.
     
     
    题意:cow很懒。。他们要参加一个party,想知道从最短路往返走哪头cow用时最多,所有的cow都会卡着点到达并返回。。已知一点,求其他点到这点再返回各点沿最短路走的最大用时。
    思路:这次练了练SPFA+SLF(双向队列优化)。之前做的单源最短路是从一个点出发,到各点的最短路,这道题加上了从所有点出发到一个点的最短路,只需将有向图倒着存储,正着找就可以了。
     
    #include<stdio.h>
    #include<string.h>
    #include<deque>
    #include<vector>
    #define MAX 1005
    #define INF 0x3f3f3f3f
    using namespace std;
    
    struct Node{
        int v,w;
    }node;
    vector<Node> edge[MAX],redge[MAX];
    int dis[MAX],diss[MAX],b[MAX];
    int n;
    void spfa(int k)
    {
        int i;
        deque<int> q;    //双向队列
        for(i=1;i<=n;i++){
            dis[i]=INF;
            diss[i]=INF;
        }
        memset(b,0,sizeof(b));
        b[k]=1;
        dis[k]=0;
        q.push_back(k);
        while(q.size()){
            int u=q.front();
            for(i=0;i<edge[u].size();i++){
                int v=edge[u][i].v;
                int w=edge[u][i].w;
                if(dis[v]>dis[u]+w){
                    dis[v]=dis[u]+w;
                    if(b[v]==0){
                        b[v]=1;
                        if(dis[v]>dis[u]) q.push_back(v);   //SLF
                        else q.push_front(v);
                    }
                }
            }
            b[u]=0;
            q.pop_front();
        }
        b[k]=1;
        diss[k]=0;
        q.push_back(k);
        while(q.size()){
            int u=q.front();
            for(i=0;i<redge[u].size();i++){
                int v=redge[u][i].v;
                int w=redge[u][i].w;
                if(diss[v]>diss[u]+w){
                    diss[v]=diss[u]+w;
                    if(b[v]==0){
                        b[v]=1;
                        if(diss[v]>diss[u]) q.push_back(v);  //SLF
                        else q.push_front(v);
                    }
                }
            }
            b[u]=0;
            q.pop_front();
        }
    }
    int main()
    {
        int m,x,u,v,w,i;
        scanf("%d%d%d",&n,&m,&x);
        for(i=1;i<=n;i++){
            edge[i].clear();
            redge[i].clear();
        }
        for(i=1;i<=m;i++){
            scanf("%d%d%d",&u,&v,&w);
            node.v=v;
            node.w=w;
            edge[u].push_back(node);
            node.v=u;
            redge[v].push_back(node);
        }
        spfa(x);
        int max=0;
        for(i=1;i<=n;i++){
            if(dis[i]+diss[i]>max&&dis[i]!=INF&&diss[i]!=INF) max=dis[i]+diss[i];
        }
        printf("%d
    ",max);
        return 0;
    }
  • 相关阅读:
    jmeter录制rabbitmq消息-性能测试
    plsqll连接Oracle的两种方式
    Badboy录制脚本时,提示脚本错误的解决方法
    Decorator
    PyObject and PyTypeObject
    Python LEGB (Local, Enclosing, Global, Build in) 规则
    Python Namespace
    Method Resolve Order (MRO)
    Source Code Structure
    Bound Method and Unbound Method
  • 原文地址:https://www.cnblogs.com/yzm10/p/7273890.html
Copyright © 2011-2022 走看看