zoukankan      html  css  js  c++  java
  • POJ3268 Silver Cow Party (建反图跑两遍Dij)

    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

    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,最后遍历两个d数组,更新答案。
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <vector> 
    #include <cstring>
    #include <queue>
    using namespace std;
    const int N=1005,M=100010;
    int head1[N],ver1[M],edge1[M],Next1[M],head2[N],ver2[M],edge2[M],Next2[M],d1[N],d2[N];//建一个原图,一个反图 
    bool v1[N],v2[N];
    int n,m,tot1=0,tot2=0,p;
    priority_queue<pair<int,int> >q1;
    priority_queue<pair<int,int> >q2;
    void add1(int x,int y,int z)
    {
        ver1[++tot1]=y;
        edge1[tot1]=z;
        Next1[tot1]=head1[x];
        head1[x]=tot1;
    }
    void add2(int x,int y,int z)
    {
        ver2[++tot2]=y;
        edge2[tot2]=z;
        Next2[tot2]=head2[x];
        head2[x]=tot2;
    }
    void dijkstra1()
    {
        memset(d1,0x3f,sizeof(d1));//d数组的初始化一定要根据题意,有时要初始化为0,有时初始化为正无穷(最短路)有时初始化为负无穷( dij或者floyd变式求某条最长边) 
        memset(v1,0,sizeof(v1));
        d1[p]=0;
        q1.push(make_pair(0,p));
        while(q1.size())
        {
            int x=q1.top().second;q1.pop();
            if(v1[x])continue;
            v1[x]=1;
            int i;
            for(i=head1[x];i;i=Next1[i])
            {
                int y=ver1[i],z=edge1[i];
                if(d1[y]>d1[x]+z)
                {
                    d1[y]=d1[x]+z;
                    q1.push(make_pair(-d1[y],y));
                 } 
            }
        } 
    }
    void dijkstra2()
    {
        memset(d2,0x3f,sizeof(d2));
        memset(v2,0,sizeof(v2));
        d2[p]=0;
        q2.push(make_pair(0,p));
        while(q2.size())
        {
            int x=q2.top().second;q2.pop();
            if(v2[x])continue;
            v2[x]=1;
            int i;
            for(i=head2[x];i;i=Next2[i])
            {
                int y=ver2[i],z=edge2[i];
                if(d2[y]>d2[x]+z)
                {
                    d2[y]=d2[x]+z;
                    q2.push(make_pair(-d2[y],y));
                 } 
            }
        } 
    }
    int main()
    {
        scanf("%d%d%d",&n,&m,&p);
        int i;
        for(i=1;i<=m;i++)
        {
            int t1,t2,t3;
            scanf("%d%d%d",&t1,&t2,&t3);
            add2(t2,t1,t3);
            add1(t1,t2,t3);
        }
        dijkstra1();
        dijkstra2();
        int ans=0;
        for(i=1;i<=n;i++)
        {
            if(i!=p)ans=max(ans,d1[i]+d2[i]);
        }
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    每天一个linux命令(1):man命令
    安卓名词积累
    每天一个Linux命令:目录
    ubuntu下Python的安装和使用
    嵌入式基础知识(1):存储
    2月份学习笔记
    AndroidStudio 中的坑Error:(1, 0) Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRID
    AndroidStudio开发出现Warning:Gradle version 2.10 is required. Current version is 2.8. If u
    Git 问题
    判断年 月的值
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12319809.html
Copyright © 2011-2022 走看看