zoukankan      html  css  js  c++  java
  • 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).

    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.

    题目大意 

    给你ML条信息 A B之间距离不得超过D;MD条信息A B 之间的距离不小于D

    线性差分约束的模板:

    于是我们将所有的不等式都转化成d[x] - d[y] <= z的形式,如下:
          1、d[Bx]  -  d[Ax]    <=    Cx
          2、d[Ay]  -  d[By]    <=  -Cy
          3、d[x-1] -    d[x]    <=    -1
         对于d[x] - d[y] <= z,令z = w(y, x),那么有 d[x] <= d[y] + w(y, x),所以当d[x] > d[y] + w(y, x),我们需要更新d[x]的值,这对应了最短路的松弛操作,于是问题转化成了求1到N的最短路。如果1到N不可达,说明最短路无限长,输出-2。如果某个点进入队列大于等于N次,则必定存在一条负环,即没有最短路,输出-1。

    差分约束讲解: https://blog.csdn.net/whereisherofrom/article/details/78922648

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    
    #define mem(a,b) memset(a,b,sizeof(a))
    #define inf 0x3f3f3f3f
    
    const int maxn=1e4+5;
    
    using namespace std;
    
    struct edge{
        int u,v,w,next;
    }e[2*maxn];
    
    int g[2*maxn],vis[maxn],d[maxn],n,m,k,tot,cnt[maxn],ans;
    
    void creat_edge(int u,int v,int w)
    {
        e[++tot]=(edge){u,v,w,g[u]};
        g[u]=tot;
    }
    
    void spfa(int st)
    {
        queue<int>q;
        mem(d,inf);
        mem(vis,0);
        d[st]=0;
        vis[st]=1;
        q.push(st);
        while(!q.empty())
        {
            int now=q.front();
            q.pop();
            if(cnt[now]>n) {ans=-1;break;}
            vis[now]=0;
            for(int i=g[now];i>0;i=e[i].next)
            {
                int u=e[i].u,v=e[i].v,w=e[i].w;
                if(d[v]>d[u]+w)
                {
                    d[v]=d[u]+w;
                    if(!vis[v])
                    {
                        vis[v]=1;
                        cnt[v]++;
                        q.push(v);
                    }
                }
            }
        }
    }
    
    int main(){
        cin>>n>>m>>k;
        ans=0;
        for(int i=1;i<=m;i++)
        {
            int u,v,w;
            cin>>u>>v>>w;
            creat_edge(u,v,w);
        }
        for(int j=1;j<=k;j++)
        {
            int u,v,w;
            cin>>u>>v>>w;
            creat_edge(v,u,-w);
        }
        spfa(1);
        if(d[n]==inf) cout<<-2<<endl;
        else if(ans==-1) cout<<-1<<endl;
        else cout<<d[n]<<endl;
        return 0;
    }
    
  • 相关阅读:
    Leetcode [654] 最大二叉树 &[105] 从前序与中序遍历序列构造二叉树 & [106] 从中序与后序遍历序列构造二叉树
    Leetcode [226] 翻转二叉树 & [116] 填充每个节点的下一个右侧节点指针 & [114] 二叉树展开为链表
    Leetcode 链表&二叉树刷题总结
    Leetcode 动态规划刷题总结
    Leetcode [1312] 让字符串成为回文串的最少插入次数 动态规划
    Leetcode [234] 回文链表 回文 链表
    动态规划之 KMP 算法详解(转)
    Leetcode [99] 恢复二叉搜索树 二叉树
    统计代码行数
    二叉树遍历(递归、非递归、mirror)转
  • 原文地址:https://www.cnblogs.com/minun/p/10473782.html
Copyright © 2011-2022 走看看