POJ3169 Layout(差分约束)

来源:互联网 发布:网络协议的分析 编辑:程序博客网 时间:2024/06/06 01:37

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 11 3 102 4 202 3 3
Sample Output
27

题目的大意是有n头奶牛按照编号从小到大站成一排等待喂食,两头牛可以站在同一个坐标点上,然而有些牛必须要和另一些牛的距离不能超过D,有一些牛和另一些牛的距离不能小于D。

用dis[i]表示第i头牛到第一头牛的距离

对于一些牛距离不超过D的:dis[j] - dis[i] <= D

对于一些牛距离不小于D的:dis[j] - dis[i] >= D


1.对于差分不等式,a - b <= c ,建一条 b 到 a 的权值为 c 的边,求的是最短路,得到的是最大值(本题求的就是最大值),对于不等式 a - b >= c ,建一条 b 到 a 的权值为 c 的边,求的是最长路,得到的是最小值。
2.如果有负环,那么无解。
3.如果dis[n]为inf,那么可以是任意解。

这题直接用SPFA就可以了。


#include<queue>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define endl '\n'using namespace std;const int inf = 1e9;int n,ml,md;int dis[1100],b[1100],cnt[1100];struct edge{    int to,cost;    edge(int a,int b)    {        to = a;        cost = b;    }};vector<edge> e[2200];int SPFA(int s){    memset(b,0,sizeof(b));    memset(cnt,0,sizeof(cnt));    queue<int> q;    for(int i=1;i<=n;i++)        dis[i] = inf;    dis[s] = 0;    q.push(s);    b[s] = 1;    while(!q.empty())    {        int t = q.front();        q.pop();        b[t] = 0;        for(int i=0;i<e[t].size();i++)        {            int x = e[t][i].to;            if(dis[x] > dis[t] + e[t][i].cost)            {                dis[x] = dis[t] + e[t][i].cost;                if (b[x] == 0)                {                    q.push(x);                    b[x] = 1;                    cnt[x]++;                    if(cnt[x] > n)                        return -1;                }            }        }    }    return dis[n];}int main(void){    int i,j;    while(scanf("%d%d%d",&n,&ml,&md)==3)    {        for(i=1;i<=n;i++)            e[i].clear();        for(i=1;i<=ml;i++)        {            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            if(x > y)                swap(x,y);            e[x].push_back(edge(y,z));        }        for(i=1;i<=md;i++)        {            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            if(x > y)                swap(x,y);            e[y].push_back(edge(x,-z));        }        int ans = SPFA(1);        if(ans == -1)            printf("-1\n");        else if(ans == inf)            printf("-2\n");        else            printf("%d\n",ans);    }    return 0;}



0 0
原创粉丝点击