poj3169 Layout

Posted zbtrs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj3169 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.
题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0。这些牛的距离存在着一些约束关系:1.有ml组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 <= w。2.有md组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 >= w。问如果这n头无法排成队伍,则输出-1,如果牛[1]和牛[n]的距离可以无限远,则输出-2,否则则输出牛[1]和牛[n]之间的最大距离。
分析:差分约束系统的模板题,如果a-b <= c,那么从b连一条权值为c的边到a,如果a-b >= c,那么从a连一条权值为-c的边到b,这样如果从某一个点跑最短路,那么所得到的结果就是每个点与这个点的差的最大值,如果是跑最长路,则为最小值。不过对于本题而言需要判断负环,即如果一个点入队的次数>=n,则存在负环,输出-1.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

int n, m, head[10010], to[20020], nextt[20020], w[20020], tot = 1, vis[10010], d[10010], cnt[10010],md,ml;
bool flag;

void add(int x, int y, int c)
{
    w[tot] = c;
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;
}

void spfa(int u)
{
    queue <int> q;
    for (int i = 1; i <= n; i++)
        d[i] = 1000000000;
    d[u] = 0;
    vis[u] = 1;
    q.push(u);
    while (!q.empty())
    {
        int t = q.front();
        q.pop();
        vis[t] = 0;
        for (int i = head[t]; i; i = nextt[i])
        {
            int v = to[i];
            if (d[v] > d[t] + w[i])
            {
                d[v] = d[t] + w[i];
                if (!vis[v])
                {
                    cnt[v]++;
                    if (cnt[v] > n)
                    {
                        flag = 0;
                        return;
                    }
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
}

int main()
{
    while (~scanf("%d%d%d",&n,&ml,&md))
    {
        flag = true;
        tot = 1;
        memset(head, 0, sizeof(head));
        memset(cnt, 0, sizeof(cnt));
        memset(vis, 0, sizeof(vis));
        for (int i = 1; i <= ml; i++)
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            add(a, b, c);   //b - a <= c
        }
        for (int i = 1; i <= md; i++)
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            add(b, a, -c); // b - a >= c
        }
        spfa(1);
        if (flag == false)
            printf("-1\n");
        else
            if (d[n] == 1000000000)
                printf("-2\n");
            else
                printf("%d\n", d[n]);
    }

    return 0;
}

 

以上是关于poj3169 Layout的主要内容,如果未能解决你的问题,请参考以下文章

Layout POJ - 3169

Layout(POJ 3169)

poj3169 Layout

POJ 3169 Layout

POJ 3169 Layout

POJ 3169 Layout (图论-差分约束)