Layout POJ - 3169 (差分约束+spfa)

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
差分约束系统:
求解差分约束系统,可以转化成图论的单源最短路径(或最长路径)问题。
观察xj-xi<=bk,会发现它类似最短路中的三角不等式d[v]<=d[u]+w[u,v],即d[v]-d[u]<=w[u,v] [1] 。因此,以每个变量xi为结点,对于约束条件xj-xi<=bk,连接一条边(i,j),边权为bk。我们再增加一个源点s,s与所有定点相连,边权均为0。对这个图,以s为源点运行Bellman-ford算法(或SPFA算法),最终{d[ i]}即为一组可行解。
例如,考虑这样一个问题,寻找一个5维向量x=(xi)以满足:
这一问题等价于找出未知量xi,i=1,2,…,5,满足下列8个差分约束条件:x1-x2≤0
x1-x5≤-1
x2-x5≤1
x3-x1≤5
x4-x1≤4
x4-x3≤-1
x5-x3≤-3
x5-x4≤-3
该问题的一个解为x=(-5,-3,0,-1,-4),另一个解y=(0,2,5,4,1),这2个解是有联系的:y中的每个元素比x中相应的元素大5。
AC的C++程序如下:

#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 1005;
const int inf = 0x3f3f3f3f;
int n, ml, md;
int visit[maxn], dist[maxn], cnt[maxn];
struct edge
{
    int  dest, cost;
    edge(int d, int c)
    {
        dest = d;
        cost = c;
    }
};
vector<edge> g[maxn];
bool spfa()
{
    queue<int> q;
    for (int i = 1; i <= n; i++)
    {
        dist[i] = inf, visit[i] = 0;
    }
    dist[n] = 0, visit[n] = 1;
    q.push(n);
    memset(cnt, 0, sizeof(cnt));
    while (!q.empty())
    {
        int front = q.front();
        q.pop();
        visit[front] = 0;
        for (int i = 0; i < (int)g[front].size(); i++)
        {
            int v = g[front][i].dest;
            if (dist[v] > dist[front] + g[front][i].cost)
            {
                dist[v] = dist[front] + g[front][i].cost;
                if (!visit[v])
                {
                    q.push(v);
                    visit[v] = 1;
                    if (++cnt[v] > n) return false;
                }
            }
        }
    }
    return true;
}
int main()
{
    while (cin>>n>>ml>>md)
    {
        int src, dest, cost;
        for (int i = 1; i <= ml; i++)
        {
            cin >> src >> dest >> cost;
            if (src < dest) swap(src, dest);
            g[src].push_back(edge(dest, cost));
        }
        for (int i = 1; i <= md; i++)
        {
            cin >> src >> dest >> cost;
            if (src > dest) swap(src, dest);
            g[src].push_back(edge(dest, -cost));
        }
        if (spfa())
        {
            if (dist[1] == inf) cout << -2 << endl;
            else cout << dist[1] << endl;
        }
        else cout << -1 << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jinduo16/article/details/81774412
今日推荐