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,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
随机推荐
- yii框架AR详解
虽 然Yii DAO可以处理事实上任何数据库相关的任务,但很可能我们会花费90%的时间用来编写一些通用的SQL语句来执行CRUD操作(创建,读取,更新和删除). 同时我们也很难维护这些PHP和SQL语 ...
- SoapUI Property
1. Test Suite(Case) Property 选择Test Suite(Case),switch to Custom properties 在request中的引用方式: ${[scope ...
- C# 保留2位小数
1.只要求保留N位不四舍5入 float f = 0.55555f; int i =(int)(f * 100); ...
- ansible条件使用--实践
ansible条件使用 1.条件使用最简单的方式 ansible中使用条件最简单的方式如下所示: [root@ansibleserver kel]# cat conditions.yml --- - ...
- Epic - Snake Sequence
You are given a grid of numbers. A snakes equence is made up of adjacent numbers such that for each ...
- 遵守GPL的开源软件能用于商用吗?
遵守GPL的开源软件能用于商用吗? 比较经典的开源协议有 GPL,BSD 等等. GPL 软件可以用于商业用途,甚至说,RMS 撰写 GPL 协议的目的就是为了让自己的 GPL 软件 emacs 可以 ...
- 徐汉彬:亿级Web系统搭建——单机到分布式集群(转载)
文章转载自http://www.csdn.net/article/2014-11-06/2822529/1 当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的 ...
- erlang常用命令
1 erlang启动时就运行odbc erl -s odbc 2 ping 节点 net_adm:ping('rabbit@COMPUTERNAME'). 3 运行cmd命令 os:cmd(" ...
- HttpClient 操作总结
1.HttpClient4.3和之前版本设置超时(set timeout)区别: 参考:http://my.oschina.net/u/577453/blog/173724 解析:如果不设置超时的话, ...
- 实现带有getMin的栈
题目 实现一个特殊的栈,在实现栈的基础上,再实现返回栈中最小的元素的操作. 要求 pop.push.getMin的时间复杂度是O(1) 可以使用现成的栈类型 思路 如下图所示,在栈结构中,每次pop的 ...