• 图论--差分约束--POJ 3169 Layout(超级源汇建图)


    Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate).Description


    Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated.

    Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.

    Input

    Line 1: Three space-separated integers: N, ML, and MD.

    Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.

    Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.

    Output

    Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.

    Sample Input

    4 2 1
    1 3 10
    2 4 20
    2 3 3

    Sample Output

    27

    Hint

    Explanation of the sample:

    There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart.

    The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.

    因为做差分约束的题目不能保证图的联通,所以要建立超级源点,也可以直接将每一个点放入队列中,因为若图中有两个联通分量,只能便利第一个不能访问第二个,不能保证图的另一部分不存在负环。

    所以这个题目要先跑一遍D(0)就是超级源点,然后若存在负环即无解就不求1——N的距离了,有解再求,两遍SPFA。

    但是在看RQ的博客的时候发现了一个特殊的建图方式,因为编号小的一定在编号大的左边,我没有考虑,所以这个题数据比较弱,然后下面的代码。

    AC代码1:

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #define INF 1e9
    using namespace std;
    const int maxn=1000+10;
    const int maxm=50000+10;
    
    struct Edge
    {
        int from,to,dist;
        Edge(){}
        Edge(int f,int t,int d):from(f),to(t),dist(d){}
    };
    
    struct BellmanFord
    {
        int n,m;
        int head[maxn],next[maxm];
        Edge edges[maxm];
        bool inq[maxn];
        int cnt[maxn];
        int d[maxn];
    
        void init(int n)
        {
            this->n=n;
            m=0;
            memset(head,-1,sizeof(head));
        }
    
        void AddEdge(int from,int to,int dist)
        {
            edges[m]=Edge(from,to,dist);
            next[m]=head[from];
            head[from]=m++;
        }
    
        int bellmanford(int s)
        {
            memset(inq,0,sizeof(inq));
            memset(cnt,0,sizeof(cnt));
            queue<int> Q;
            for(int i=1;i<=n;i++) d[i]= i==s?0:INF;
            Q.push(s);
    
            while(!Q.empty())
            {
                int u=Q.front(); Q.pop();
                inq[u]=false;
                for(int i=head[u];i!=-1;i=next[i])
                {
                    Edge &e=edges[i];
                    if(d[e.to] > d[u]+e.dist)
                    {
                        d[e.to] = d[u]+e.dist;
                        if(!inq[e.to])
                        {
                            inq[e.to]=true;
                            Q.push(e.to);
                            if(++cnt[e.to]>n) return -1;
                        }
                    }
                }
            }
            return d[n]==INF?-2:d[n];
        }
    }BF;
    
    int main()
    {
        int n,ml,md;
        while(scanf("%d%d%d",&n,&ml,&md)==3)
        {
            BF.init(n);
            while(ml--)
            {
                int u,v,d;
                scanf("%d%d%d",&u,&v,&d);
                BF.AddEdge(u,v,d);
            }
            while(md--)
            {
                int u,v,d;
                scanf("%d%d%d",&u,&v,&d);
                BF.AddEdge(v,u,-d);
            }
            for(int i=1;i<=n;i++)
                BF.AddEdge(0,i,0);
            if(BF.bellmanford(0)!=-1) printf("%d
    ",BF.bellmanford(1));
            else puts("-1");
        }
        return 0;
    }
    
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #define INF 1e9
    using namespace std;
    const int maxn=1000+10;
    const int maxm=50000+10;
     
    struct Edge
    {
        int from,to,dist;
        Edge(){}
        Edge(int f,int t,int d):from(f),to(t),dist(d){}
    };
     
    struct BellmanFord
    {
        int n,m;
        int head[maxn],next[maxm];
        Edge edges[maxm];
        bool inq[maxn];
        int cnt[maxn];
        int d[maxn];
     
        void init(int n)
        {
            this->n=n;
            m=0;
            memset(head,-1,sizeof(head));
        }
     
        void AddEdge(int from,int to,int dist)
        {
            edges[m]=Edge(from,to,dist);
            next[m]=head[from];
            head[from]=m++;
        }
     
        int bellmanford()
        {
            memset(inq,0,sizeof(inq));
            memset(cnt,0,sizeof(cnt));
            queue<int> Q;
            for(int i=1;i<=n;i++) d[i]= i==1?0:INF;
            Q.push(1);
     
            while(!Q.empty())
            {
                int u=Q.front(); Q.pop();
                inq[u]=false;
                for(int i=head[u];i!=-1;i=next[i])
                {
                    Edge &e=edges[i];
                    if(d[e.to] > d[u]+e.dist)
                    {
                        d[e.to] = d[u]+e.dist;
                        if(!inq[e.to])
                        {
                            inq[e.to]=true;
                            Q.push(e.to);
                            if(++cnt[e.to]>n) return -1;
                        }
                    }
                }
            }
            return d[n]==INF?-2:d[n];
        }
    }BF;
     
    int main()
    {
        int n,ml,md;
        while(scanf("%d%d%d",&n,&ml,&md)==3)
        {
            BF.init(n);
            while(ml--)
            {
                int u,v,d;
                scanf("%d%d%d",&u,&v,&d);
                BF.AddEdge(u,v,d);
            }
            while(md--)
            {
                int u,v,d;
                scanf("%d%d%d",&u,&v,&d);
                BF.AddEdge(v,u,-d);
            }
            for(int i=2;i<=n;i++)
                BF.AddEdge(i,i-1,0);
            printf("%d
    ",BF.bellmanford());
        }
        return 0;
    }
  • 相关阅读:
    OGG实验:喂奶间隔数据表通过OGG配置同步
    Oracle Exadata 学习笔记之核心特性Part1
    js 表格指定列,根据相同值实现跨行合并
    tr td同时添加点击事件
    Oracle 分页查询
    tomcat启动时运行指定的java类
    application/x-www-form-urlencoded与multipart/form-data与application/json的区别 精析
    nodejs中thiskeyword的问题
    AlphaGo 开源项目研究(1)
    LeetCode -- Best Time to Buy and Sell Stock II
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798585.html
Copyright © 2020-2023  润新知