Guessing Game

题目连接:

http://codeforces.com/gym/100015/attachments

Description

Jaehyun has two lists of integers, namely a1,...,aN and b1,...,bM.Je!rey wants to know what these

numbers are, but Jaehyun won’t tell him the numbers directly. So, Je!rey 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, Je!rey 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 Je!rey.

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 Je!rey

asked. These numbers satisfy 2 ! N + M ! 1,000 and 1 ! Q ! 10,000. Each of the next Q lines is of the

form ij<=c or ij>=c.Theformerrepresents 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:

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:

Sample Input

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

Sample Output

Impossible

Possible

Hint

题意

a数组有n个数,b数组有m个数

然后告诉你一些不等式表示a[i]+b[j]<=C之类的

问你能否找到一组解

题解:

差分约束的裸题

我们建边之后,跑最短路,看是否有负环,如果存在负环的话,就说明这个不等式显然是不成立的

就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
struct node
{
int x,y;
};
vector<node> E[2005];
int n,m,q;
int inq[2005],dis[2005];
int flag=0;
void solve(int x)
{
if(flag)
return;
inq[x]=1;
for(int i=0;i<E[x].size();i++)
{
node v = E[x][i];
if(dis[v.x]>dis[x]+v.y)
{
dis[v.x]=dis[x]+v.y;
if(inq[v.x])
{
flag=1;
return;
}
if(!inq[v.x])
{
dis[v.x]=dis[x]+v.y;
solve(v.x);
}
}
}
inq[x]=0;
}
int main()
{
//freopen("1.in","r",stdin);
while(scanf("%d%d%d",&n,&m,&q)!=EOF)
{
if(n==0&&m==0&&q==0)
break;
flag = 0;
memset(inq,0,sizeof(inq));
memset(dis,0,sizeof(dis));
for(int i=0;i<=n+m;i++)
E[i].clear();
for(int i=0;i<=n;i++)
dis[i]=inf;
for(int i=0;i<q;i++)
{
int x,y,z;
string s;
scanf("%d%d",&x,&y);cin>>s;
scanf("%d",&z);
if(s==">=")
E[x].push_back((node){n+y,-z});
if(s=="<=")
E[y+n].push_back((node){x,z});
}
for(int i=1;i<=n+m;i++)
dis[i]=0,solve(i);
if(flag)printf("Impossible\n");
else printf("Possible\n");
}
}

Codeforces Gym 100015G Guessing Game 差分约束的更多相关文章

  1. Gym 100096D Guessing game

    Gym 100096D Guessing game 题面 Problem Description Byteman is playing a following game with Bitman. Bi ...

  2. Candies-POJ3159差分约束

    Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...

  3. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  4. ZOJ 2770火烧连营——差分约束

    偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...

  5. POJ 2983 Is the Information Reliable? 差分约束

    裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...

  6. 2014 Super Training #6 B Launching the Spacecraft --差分约束

    原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...

  7. POJ 1364 King --差分约束第一题

    题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...

  8. [USACO2005][POJ3169]Layout(差分约束)

    题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...

  9. ShortestPath:Layout(POJ 3169)(差分约束的应用)

                布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...

随机推荐

  1. Sciter使用心得

    1. div双击事件  $(div).onMouse = function(evt) {       switch(evt.type) {          case Event.MOUSE_DCLI ...

  2. HDU5804 Price List (BestCoder Round #86 A)水题

    分析:大于总和输出1 #include <cstdio> #include <cstring> #include <algorithm> using namespa ...

  3. angularJS+requireJS并集成karma测试实践

    最近在为下一个项目做前端技术选型,Angular是必须要用的(BOSS指定,个人感觉也不错,开发效率会很高).由于需要加载的JS很多,所以打算看看angular和requirejs一起用会怎么样.在g ...

  4. Solaris系统管理(一)

    最近需要将一个项目从Linux平台迁移到Solaris,对Solaris进行了一点研究,总结如下. 一句话介绍: Solaris 是Sun Microsystems研发的计算机操作系统.它被认为是UN ...

  5. (转载)高速ADC的关键指标:量化误差、offset/gain error、DNL、INL、ENOB、分辨率、RMS、SFDR、THD、SINAD、dBFS、TWO-TONE IMD

    (一)一个基本概念 分贝(dB):按照对数定义的一个幅度单位.对于电压值,dB以20log(VA/VB)给出:对于功率值,以10log(PA/PB)给出.dBc是相对于一个载波信号的dB值:dBm是相 ...

  6. 【LeetCode】96 - Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  7. JDK Environment Variable And Change default JDK

    Environment Variable : change(import) /etc/bashrc export JAVA_HOME=/software/jdk1.8.0 export PATH=$J ...

  8. servicestack操作redis

    tatic void Main(string[] args) { );//redis服务IP和端口 #region =insert= var storeMembers = new List<st ...

  9. Python进程和线程

    引入进程和线程的概念及区别 1.线程的基本概念 概念 线程是进程中执行运算的最小单位,是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点在运行中必不可少的资源,但 ...

  10. HW7.17

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...