首页 > 代码库 > Did Pong Lie? (差分系统 判负环)

Did Pong Lie? (差分系统 判负环)

Did Pong Lie?

时间限制: 5 Sec  内存限制: 128 MB
提交: 68  解决: 15
[提交][状态][讨论版]

题目描述

Doctor Pong has two arrays of integers : a1 , a2 , ......, aN and b1 , b2 , ......, bM . His student Rong wants to know what these numbers are, but Pong won’t tell him the numbers directly. So, Rong asks Pong a series of questions of the form "How big is ai+ bj ?", but his answers either "It’s at least c" or "It’s at most c". After getting Pong’s
responses, Rong tries to guess the numbers, but he cannot ?gure them out no matter how hard he tries. He starts to wonder if Pong has lied while answering some of the questions. Write a program to help Rong.

输入

There are multiple test cases.
The ?rst line of input contains an integer T(1 ≤ T ≤ 100), indicating the number of test cases.
Each test case begins with a line containing three positive integers N, M, and Q, which denote the lengths of the Pong’s arrays and the number of questions that Rong asked. These numbers satisfy 2 ≤ N + M ≤ 2, 000 and 1 ≤ Q ≤ 10, 000.
Each of the next Q lines is of the form "i j <= c" or "i j >= c". The former represents ai + bj ≤ c, and the latter represents ai + bj ≥ c. It is guaranteed that c ≤ 100000.

输出

 

样例输入

2
2 1 3
1 1 <= 3
2 1 <= 5
1 1 >= 4
2 2 4
1 1 <= 3
2 1 <= 4
1 2 >= 5
2 2 >= 7

样例输出

Pong has lied!
Honest Pong!
【题意】给你两个数组,有Q个不等式,a[i]+b[j]<=(>=)x,然后问你有没有矛盾。
【分析】一看就是差分系统,但这里是加法,我们不妨令c数组=-b数组,那么a[i]+b[j]==a[i]-c[j],若,a,c合法,则a,b合法。
然后建图,对于每个不等式,转化成u-v<=x的形式,然后v-->u建边,然后判负环,跑个最短路,若某个节点被加入队列>(n+m)次,
则存在负环。
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define inf 1e9+7
using namespace std;
const int N = 2e3+50;
int n,m,k;
char str[5];
int dis[N],inq[N],cnt[N],q[N*N];
vector<pair<int,int> >edg[N];
void init(){
    for(int i=0;i<N;i++){
        dis[i]=inq[i]=0;
        edg[i].clear();
    }
}
bool spfa(){
    int r=0;
    for(int i=1;i<=n+m;i++){
        q[++r]=i;inq[i]=true;
        cnt[i]=1;
    }
    while(r){
        int u=q[r--];
        inq[u]=false;
        for(int i=0;i<edg[u].size();i++){
            int v=edg[u][i].first;
            int w=edg[u][i].second;
            if(dis[u]+w<dis[v]){
                dis[v]=dis[u]+w;
                if(!inq[v]){
                    q[++r]=v;
                    inq[v]=true;
                    cnt[v]++;
                    if(cnt[v]>n+m)return false;
                }
            }
        }
    }
    return true;
}
int main()
{
    int x,y,w,T;
    scanf("%d",&T);
    while(T--){
        init();
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=k;i++){
            scanf("%d%d%s%d",&x,&y,str,&w);
            if(str[0]==<){
                edg[y+n].pb(mp(x,w));
            }
            else {
                edg[x].pb(mp(y+n,-w));
            }
        }
        if(spfa())puts("Honest Pong!");
        else puts("Pong has lied!");
    }
    return 0;
}

 

Did Pong Lie? (差分系统 判负环)