zoukankan      html  css  js  c++  java
  • (简单) POJ 3268 Silver Cow Party,Dijkstra。

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

      正向边一次最短路,反向边一次就好了。。。

    代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    
    using namespace std;
    
    const int INF=10e8;
    const int MaxN=1010;
    
    struct Node
    {
        int v,val;
    
        Node(int _v=0,int _val=0):v(_v),val(_val) {}
        bool operator < (const Node &a) const
        {
            return val>a.val;
        }
    };
    
    struct Edge
    {
        int v,cost;
    
        Edge(int _v=0,int _cost=0):v(_v),cost(_cost) {}
    };
    
    vector <Edge> E[2][MaxN];
    
    void Dijkstra(int type,int lowcost[],int n,int start)
    {
        priority_queue <Node> que;
        Node qtemp;
        int u,v,c,len;
    
        for(int i=1;i<=n;++i)
        {
            lowcost[i]=INF;
        }
        lowcost[start]=0;
    
        que.push(Node(start,0));
    
        while(!que.empty())
        {
            qtemp=que.top();
            que.pop();
    
            u=qtemp.v;
    
            len=E[type][u].size();
    
            for(int i=0;i<len;++i)
            {
                v=E[type][u][i].v;
                c=E[type][u][i].cost;
    
                if(lowcost[u]+c<lowcost[v])
                {
                    lowcost[v]=lowcost[u]+c;
                    que.push(Node(v,lowcost[v]));
                }
            }
        }
    }
    
    inline void addEdge(int type,int u,int v,int c)
    {
        E[type][u].push_back(Edge(v,c));
    }
    
    int ans1[MaxN],ans2[MaxN];
    int maxans;
    
    int main()
    {
        int N,M,X;
        int a,b,c;
    
        while(~scanf("%d %d %d",&N,&M,&X))
        {
            for(int i=1;i<=M;++i)
            {
                scanf("%d %d %d",&a,&b,&c);
    
                addEdge(0,a,b,c);
                addEdge(1,b,a,c);
            }
    
            for(int i=1;i<=N;++i)
                ans1[i]=ans2[i]=0;
            maxans=-1;
    
            Dijkstra(0,ans1,N,X);
            Dijkstra(1,ans2,N,X);
    
            for(int i=1;i<=N;++i)
                if(ans1[i]+ans2[i]>maxans)
                    maxans=ans1[i]+ans2[i];
    
            cout<<maxans<<endl;
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    Netty源码分析之ByteBuf引用计数
    GitHub git push大文件失败(write error: Broken pipe)完美解决
    Windows10 Docker安装详细教程
    全面的Docker快速入门教程
    十本你不容错过的Docker入门到精通书籍推荐
    CentOS 8.4安装Docker
    postgres之一条sql查询总数及部分数据
    neo4j相关操作
    git上传大文件
    分布式文件系统fastdfs安装以及python调用
  • 原文地址:https://www.cnblogs.com/whywhy/p/4338565.html
Copyright © 2011-2022 走看看