Codeforces Gym 100015G Guessing Game 差分约束
Description
Jaehyun has two lists of integers, namely a 1 ,...,a N and b 1 ,...,b M . 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 a i + b j ?” 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.
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 a i + b j ≤ c, and the latter represents a i + b j ≥ 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 a 1 ,...,a N and b 1 ,...,b M
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
题意:
给你一些不等式 ,检查是否有可能让这些不等式都满足
题解:
查分约束, http://www.cnblogs.com/zxhl/p/4777077.html
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> using namespace std ; typedef long long ll; const int N = 20000+50; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; struct ss{int to,v;}; vector<ss > G[N]; int dis[N], f = 0, vis[N]; void dfs(int x) { vis[x] = 1; if(f) return ; for(int i = 0; i < G[x].size(); i++) { if(dis[G[x][i].to] > dis[x] + G[x][i].v) { if(vis[G[x][i].to]) {f = 1;return ;} dis[G[x][i].to] = dis[x] + G[x][i].v; dfs(G[x][i].to); } } vis[x] = 0; } void init() { for(int i = 0; i < N; i++) G[i].clear(),vis[i] = 0,dis[i] = inf; //memset(dis,127,sizeof(dis)); } int main() { int q,n,m,a,b,c; char s[N]; while(~scanf("%d%d%d",&n,&m,&q)) { if(n == 0 && m == 0 && q == 0) break; init(); for(int i = 1; i <= q; i++) { scanf("%d%d%s%d",&a,&b,s,&c); if(s[0] == '>') G[a].push_back(ss{b + n,-c}); else G[b + n].push_back(ss{a,c}); } f = 0;int no = 0; for(int i = 1; i <= n + m; i++) { dis[i] = 0; dfs(i); if(f) {cout<<"Impossible"<<endl;no = 1;break;} } if(!no) cout<<"Possible"<<endl; } return 0; }