Cows Arrangement_foj1898_差分约束

Problem Description


Like everyone else, cows like to stand close to their friends. Farmer Johnson has N cows numbered from 1 to N. He wants to arrange his cows to stand along the road in a straight line. The cows are standing in the same order as they are numbered. Cow 1 is standing on position 0.The ith cow (2 <= i <= N) is standing on the right of the (i-1)th cow. No two cows stand on the same position.The length of the road is 100000000.So cow N can stand on position 100000000 at most.
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 constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints tells which cows dislike each other and the minimum distance by which they must be separated.

Your job is to compute, if possible, the minimum and maximum possible position of each cow. A possible position x of a cow means that there exists one configuration satisfying the distance constraints that the cow can stand on position x.

Input


Input contains multiple cases.
The first line contains an integer T representing the number of cases. Then following T cases.

Each test case starts with three integers N,ML,MD (1 <= N <= 1000,0 <= ML <= 1000,0 <= MD <= 1000).

Following ML lines, each line contains three integer A,B,D(1 <= A < B <= N,1 <= D <= 10000) ,representing that Cows A and B must be at most D apart.

Following MD lines, each line contains three integer A,B,D(1 <= A < B <= N,1 <= D <= 10000) ,representing that Cows A and B must be at least D apart.

Output


For each test case, first output one line containing “Case #x:”, where x is the case number (starting from 1).If it’s impossible to arrange the cows, output “Not Exist!”,otherwise output “Exist!”,then output N line, each line contain two integers. The integers in the ith line representing the minimum and maximum possible position of cow i.

Analysis


大概就是差分约束的例题,读题就能得出

  • HbHaw
  • HbHaw

这样还不够,显然的可以得出每头牛不一样高(样例)

  • HiHi11

每头牛最高100000000又能得出

  • HnH1100000000

变号连边什么的就不讲了,重点是这道题要求我们求最大和最小,那么就暴力一点建两个图跑两次spfa

临近月考还™刷题,刷你妹啊(掀桌)

Code


/*
ID:wjp13241
PROG:cowsarrangement
LANG:C++
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#define fo(i,a,b) for (int i=a;i<=b;i++)
#define dfo(i,a,b) for (int i=a;i>=b;i--)
#define fore(i,x,e) for (int i=ls[x];i;i=e[i].next)
#define fil(x,t) memset(x,t,sizeof(x))
#define STP system("pause")
#define min(x,y) x<y?x:y
#define max(x,y) x>y?x:y
#define PuB(v,x) v.push_back(x)
#define PoB(v) v.pop_back()
#define ld long double
#define ll long long
#define db double
#define INF 0x3f3f3f3f
#define LIM 100000000
#define EPS 1e-4
#define N 2001
#define E N*10+1
using namespace std;
struct edge{int y,w,next;}e[E];
int vis[N],cnt[N],mnDis[N],mxDis[N],ls[E],x[N],y[N],w[N],maxE=0;
int add(int x,int y,int w){e[++maxE]=(edge){y,w,ls[x]},ls[x]=maxE;}
int mnSpfa(int st,int ed,int n)
{
    queue<int>q;
    fil(mnDis,63);
    fil(cnt,0);
    fil(vis,0);
    q.push(st);
    mnDis[st]=0;
    vis[st]=1;
    while (!q.empty())
    {
        int now=q.front();q.pop();
        if (++cnt[now]>=n)
            return 0;
        fore(i,now,e)
            if (mnDis[now]+e[i].w<mnDis[e[i].y])
            {
                mnDis[e[i].y]=e[i].w+mnDis[now];
                if (!vis[e[i].y])
                {
                    vis[e[i].y]=1;
                    q.push(e[i].y);
                }
            }
        vis[now]=0;
    }
    return 1;
}
int mxSpfa(int st,int ed,int n)
{
    queue<int>q;
    fil(mxDis,-63);
    fil(cnt,0);
    fil(vis,0);
    q.push(st);
    mxDis[st]=0;
    vis[st]=1;
    while (!q.empty())
    {
        int now=q.front();q.pop();
        if (++cnt[now]>=n)
            return 0;
        fore(i,now,e)
            if (mxDis[now]+e[i].w>mxDis[e[i].y])
            {
                mxDis[e[i].y]=e[i].w+mxDis[now];
                if (!vis[e[i].y])
                {
                    vis[e[i].y]=1;
                    q.push(e[i].y);
                }
            }
        vis[now]=0;
    }
    return 1;
}
int main()
{
    ios::sync_with_stdio(false);
    int T,Case=0;
    cin>>T;
    while (T--&&++Case)
    {
        fil(ls,0);
        maxE=0;
        int n,a,b;
        cin>>n>>a>>b;
        fo(i,1,a+b)
            cin>>x[i]>>y[i]>>w[i];
        fo(i,1,a)
            add(x[i],y[i],w[i]);
        fo(i,a+1,a+b)
            add(y[i],x[i],-w[i]);
        fo(i,2,n)
            add(i,i-1,-1);
        add(1,n,LIM);
        if (!mnSpfa(1,n,n))
        {
            cout<<"Case #"<<Case<<": Not Exist!"<<endl;
            continue;
        }
        fil(ls,0);
        maxE=0;
        fo(i,1,a)
            add(y[i],x[i],-w[i]);
        fo(i,a+1,a+b)
            add(x[i],y[i],w[i]);
        fo(i,2,n)
            add(i-1,i,1);
        add(n,1,-LIM);
        if (!mxSpfa(1,n,n))
        {
            cout<<"Case #"<<Case<<": Not Exist!"<<endl;
            continue;
        }
        cout<<"Case #"<<Case<<": Exist!"<<endl;
        fo(i,1,n)
            cout<<mxDis[i]<<" "<<mnDis[i]<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值