POJ 3169 差分约束

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

题目大意:

一共有n头牛,有ml个关系好的牛的信息,有md个关系不好的牛的信息,对应输入的第一行的三个元素,接下来ml行,每行三个元素A,B,D,表示A牛和B牛相距不希望超过D,接下来md行,每行三个元素A,B,D表示A牛和B牛的相距至少要有D才行。求1号牛和n号牛的最大距离,如果距离无限大输出-2,如果无解输出-1。

有约束的求路径距离的问题,那就可以考虑到另一种东西----差分约束,差分约束可以理解为有约束条件的一种借助于最短路的方法求解答案,看一看大牛的解析

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=10009;
const int INF=0x3f3f3f3f;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ms(a,b) memset(a,b,sizeof a)
struct node
{
    int next,to,val;
}num[N<<4];
int head[N];
int dis[N];
int cot[N];
bool vis[N];
int n;
int tot;
void add(int u,int v,int w)
{
    num[tot].to=v;
    num[tot].val=w;
    num[tot].next=head[u];
    head[u]=tot++;
}
void spfa()
{
    ms(dis,INF);
    ms(vis,0);
    ms(cot,0);
    cot[1]++;vis[1]=1;dis[1]=0;
    queue<int> q;
    q.push(1);
    while(q.size())
    {
        int now=q.front();
        q.pop();vis[now]=0;
        if(cot[now]>=n)
        {
            cout<<-1<<endl;return;
        }
        for(int i=head[now];i!=-1;i=num[i].next)
        {
            int v=num[i].to;
            int w=num[i].val;
            if(dis[v]>dis[now]+w)
            {
                dis[v]=dis[now]+w;
                if(!vis[v])
                {
                    cot[v]++;vis[v]=1;q.push(v);
                }
            }
        }
    }
    if(dis[n]==INF)//经过约束操作之后发现1-->n没有约束条件,那不就是说明可以无限远了
        cout<<-2<<endl;
    else
    cout<<dis[n]<<endl;
}
int main()
{
    ios::sync_with_stdio(0);cin.tie(0);
    int ml,md;
    tot=1;
    ms(head,-1);
    cin>>n>>ml>>md;
        int x,y,z;
    rep(i,1,ml)//这里是dis[y]-dis[x]<=z;
    {
        cin>>x>>y>>z;
        add(x,y,z);
    }
    //这里是dis[y]-dis[x]>=z,于是可以推出dis[x]-dis[y]<=-z;这样就能和上面的建边的过程相同了
    rep(i,1,md)//题目给出的顺序是x<y,所及就直接建立从x-->y的边就好,不用考虑顺序问题了
    cin>>x>>y>>z,add(y,x,-z);
    spfa();
}

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/82316575
今日推荐