POJ 3169 Layout (差分约束)

Description

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

题意:

一些母牛按序号排成一条直线(可以站在一起)。有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离。如果无法成立输出-1,如果答案不收敛输出-2,否则输出最大的距离。

第一行给出N,ML,MD,分别表示牛的数量,第一种要求数量,第二种要求数量。


参考链接: http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html


思路:

看完上面的博客就比较清晰了。

求最大值,只需转化为 B-A <= ? 这种形式建图求最短路,用spfa求最短路。

存在负环即 不成立 输出-1, dis[N] 为无穷大 即 无约束可无限长 输出-2,否则输出dis[N]。

建图有三种情况:

第一种:i - 1 <= i    即  (i-1) - i  <= 0

第二种:                        v - u <= m

第三种:v - u >= m 即  u - v <= m

类似这种形式 a - b <= c  ,建图令b为起点,a为终点,c为权值


代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

#define For(a,b,c) for(int a = b; a <= c; a++)
#define mem(a,b) memset(a,b,sizeof(a))

int first[1005], tot = 0, dis[1005], cnt[1005];
bool book[1005];

struct Node
{
    int v, w, next;
}e[41005];

void add(int u, int v, int w)
{
    e[tot].v = v;
    e[tot].w = w;
    e[tot].next = first[u];
    first[u] = tot++;
}

int spfa(int u, int n)
{
    queue<int> q;
    q.push(u);
    mem(dis,0x3f);
    mem(book,false);
    mem(cnt,0);
    book[u] = true;
    dis[u] = 0;
    cnt[u]++;

    while(!q.empty())
    {
        u = q.front();
        q.pop();

        for(int i = first[u]; ~i; i = e[i].next)
        {
            if(dis[e[i].v] > dis[u] + e[i].w)
            {
                dis[e[i].v] = dis[u] + e[i].w;
                if(!book[e[i].v])
                {
                    q.push(e[i].v);
                    book[e[i].v] = true;
                    cnt[e[i].v]++;
                    if(cnt[e[i].v] > n) return -1;
                }
            }
        }

        book[u] = false;
    }

    if(dis[n] == 0x3f3f3f3f) return -2;
    return dis[n];
}

int main()
{
    int N, ML, MD, u, v, w;
    scanf("%d%d%d",&N,&ML,&MD);
    mem(first,-1);
    For(i,2,N)
    {
        add(i,i-1,0);
    }
    while(ML--)
    {
        scanf("%d%d%d",&u,&v,&w);
        add(u,v,w);
    }
    while(MD--)
    {
        scanf("%d%d%d",&u,&v,&w);
        add(v,u,-w);
    }
    printf("%d\n",spfa(1,N));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/j2_o2/article/details/79988126
今日推荐