hunnu 11127 gaussing game 差分约束

来源:互联网 发布:linux awk 时间函数 编辑:程序博客网 时间:2024/05/01 04:21
Jaehyun has two lists of integers, namely a1, . . . , aN and b1, . . . , bM. Jeffrey wants to know what these
numbers are, but Jaehyun won’t tell him the numbers directly. So, Jeffrey asks Jaehyun a series of questions
of the form “How big is ai + bj?” Jaehyun won’t even tell him that, though; instead, he answers either
“It’s at least c,” or “It’s at most c.” (Right, Jaehyun simply doesn’t want to give his numbers for whatever
reason.) After getting Jaehyun’s responses, Jeffrey tries to guess the numbers, but he cannot figure them out
no matter how hard he tries. He starts to wonder if Jaehyun has lied while answering some of the questions.
Write a program to help Jeffrey.
G.1 Input
The input consists of multiple test cases. Each test case begins with a line containing three positive integers
N, M, and Q, which denote the lengths of the Jaehyun’s lists and the number of questions that Jeffrey
asked. These numbers satisfy 2  N + M  1,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 −1,000  c  1,000. The input terminates with a line with N = M = Q = 0. For example:
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
0 0 0
G.2 Output
For each test case, print a single line that contains “Possible” if there exist integers a1, . . . , aN and b1, . . . , bM
that are consistent with Jaehyun’s answers, or “Impossible” if it can be proven that Jaehyun has definitely
lied (quotes added for clarity). The correct output for the sample input above would be:
Impossible
Possible


/**把问题转化为找满足条件的a 和 -b,则加号就改成了减号,差分约束解决*/#include<stdio.h>#include<queue>using namespace std;const int V = 1100,          INF = 100000000;struct node{    int u,v,w;    node(int a=0,int b=0,int c=0)    {        u=a;        v=b;        w=c;    }}ver[12000];int n,m,q,nv,numEdge,d[V];int relax(int i){    if(d[ver[i].v]>d[ver[i].u]+ver[i].w)    {        d[ver[i].v]=d[ver[i].u]+ver[i].w;        return 1;    }    return 0;}int bf(){    int i,j;    for(i=0;i<nv;++i)        for(j=0;j<numEdge;++j)            relax(j);    for(j=0;j<numEdge;++j)        if(relax(j))            return 0;    return 1;}int main(){    while(scanf("%d%d%d",&n,&m,&q)!=EOF && n)    {        int i;        nv=n+m;        numEdge=n+m+q;        for(i=0;i<nv;++i)        {            ver[i]=node(0,i,0);            d[i+1]=INF;        }        d[0]=0;        int u,v,c;        char sign[4];        while(q--)        {            scanf("%d%d%s %d",&u,&v,sign,&c);            if(sign[0]=='<')                ver[i++]=node(v+n,u,c);            else                ver[i++]=node(u,v+n,-c);        }        printf(bf()?"Possible\n":"Impossible\n");    }    return 0;}/**2 1 31 1 <= 32 1 <= 51 1 >= 42 2 41 1 <= 32 1 <= 41 2 >= 52 2 >= 7*/


原创粉丝点击