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;
}
代码

 

转载于:https://www.cnblogs.com/zxhl/p/5136166.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值