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. Oracle Database 11g Express Edition 使用小结(windows)

    如何启动oraclewindows系统服务中有一个服务叫:[OracleService[SID]]SID是你安装oracle xe时候的实例名,如果你没有改默认的是[XE], OracleServic ...

  2. 《Windows程序设计第5版》学习进度备忘

    书签:另外跳过的内容有待跟进 __________________学习资源: <Windows程序设计第5版珍藏版> __________________知识基础支持: _________ ...

  3. Epic - Tic Tac Toe

    N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...

  4. SpringMVC + Spring + MyBatis 学习笔记:在类和方法上都使用RequestMapping如何访问

    系统:WIN8.1 数据库:Oracle 11GR2 开发工具:MyEclipse 8.6 框架:Spring3.2.9.SpringMVC3.2.9.MyBatis3.2.8 先看代码: @Requ ...

  5. asp.net mvc下文件上传

    典型的文件上传表单 <form action="/File" enctype="multipart/form-data" method="pos ...

  6. PySpark调用自定义jar包

    在开发PySpark程序时通常会需要用到Java的对象,而PySpark本身也是建立在Java API之上,通过Py4j来创建JavaSparkContext. 这里有几点是需要注意的 1. Py4j ...

  7. 第三百四十四天 how can I 坚持

    三言诗,把自己的心情,想要说的话用三句话诗意的表达出来.像: 烦===>好想睡一觉,待日落,盼天明. 愁====>待到花开花落,闲庭信步,蹋碎一世忧愁. 三句话,可以表达的很好. 老是感觉 ...

  8. Latex 横排图片

    \begin{figure} \begin{minipage}[t]{0.5\linewidth} \centering \includegraphics[width=2.2in]{figure/an ...

  9. 转】MyEclipse使用总结——MyEclipse去除网上复制下来的来代码带有的行号

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/3544208.html 感谢! 一.正则表达式去除代码行号 作为开发人员,我们经常从网上复制一些代码,有些时候复制 ...

  10. HDU 2516 取石子游戏(FIB博弈)

    取石子游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...