Codeforces Gym 100015G Guessing Game 差分约束
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 差分约束的更多相关文章
- Gym 100096D Guessing game
Gym 100096D Guessing game 题面 Problem Description Byteman is playing a following game with Bitman. Bi ...
- Candies-POJ3159差分约束
Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...
- poj3159 差分约束 spfa
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...
- ZOJ 2770火烧连营——差分约束
偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- 2014 Super Training #6 B Launching the Spacecraft --差分约束
原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...
- POJ 1364 King --差分约束第一题
题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...
- [USACO2005][POJ3169]Layout(差分约束)
题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
随机推荐
- selenium + python 自动化测试环境搭建
selenium + python 自动化测试 —— 环境搭建 关于 selenium Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操 ...
- [转]inux之touch命令
转自:http://www.2cto.com/os/201309/242518.html Linux学习之touch命令 Linux的touch命令一般用来更改文档或目录的日期时间,包括存取时间和 ...
- 如何在Docker中部署DzzOffice
一.一些背景 之前研究Docker很久了,并且在公司内部实际使用起来了,目前分两种场景使用Docker 1.作为PAAS,提供一致,统一的编译/测试环境: 2.作为虚拟机,直接分配给新来的开发人员使用 ...
- Google搜索的常用技巧
个人搜索方案 1.选择合适的搜索词,一些行业术语或专家名字可以带来更加高质量的结果. 2.搜索词手动使用空格分隔,先进行第一次搜索,看搜索结果标题是否满足预期,如果不满足,采用更换关键词,添加关键词, ...
- java webservice AXIS
1. eclipse axis 插件下载地址 http://archive.apache.org/dist/ws/axis2/tools/1_4_1/ 一个是代码生成插件 axis2-ecli ...
- 【LeetCode】204 - Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...
- motan源码解读:注册中心zookeeper(1)
Apache ZooKeeper is an effort to develop and maintain an open-source server which enables highly rel ...
- windows7__32位下安装python2.6.6
1.下载windows7__32位的python2.6.6.mis文件,直接运行.默认安装即可 2.设置系统环境变量,目的在cmd下能敲python后能够自动调用到安装目录程序 设计如下:(我的电脑- ...
- extjs笔记
1. ExtJs 结构树.. 2 2. 对ExtJs的态度.. 3 3. Ext.form概述.. 4 4. Ext.TabPanel篇.. 5 5. Functio ...
- ios8 下请求读取通讯录权限 (网上众多资料漏下得一个坑)
读取通讯录权限网上都有,不再叙述. 当第一次授权时用户假如拒绝授权, 第二次就无法再次弹出提示框授权. 刚开始时完全按照网上例子写的,第一次拒绝授权后,在设置里面无法找到对应的更改读取权限的选项. 后 ...