Did Pong Lie?

时间限制: 5 Sec  内存限制: 128 MB
提交: 68  解决: 15
[提交][状态][讨论版]

题目描述

Doctor Pong has two arrays of integers : a1 , a2 , ......, aN and b1 , b2 , ......, bM . His student Rong wants to know what these numbers are, but Pong won’t tell him the numbers directly. So, Rong asks Pong a series of questions of the form "How big is ai+ bj ?", but his answers either "It’s at least c" or "It’s at most c". After getting Pong’s
responses, Rong tries to guess the numbers, but he cannot figure them out no matter how hard he tries. He starts to wonder if Pong has lied while answering some of the questions. Write a program to help Rong.

输入

There are multiple test cases.
The first line of input contains an integer T(1 ≤ T ≤ 100), indicating the number of test cases.
Each test case begins with a line containing three positive integers N, M, and Q, which denote the lengths of the Pong’s arrays and the number of questions that Rong asked. These numbers satisfy 2 ≤ N + M ≤ 2, 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 ai + bj ≤ c, and the latter represents ai + bj ≥ c. It is guaranteed that c ≤ 100000.

输出

 

样例输入

2
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

样例输出

Pong has lied!
Honest Pong!
【题意】给你两个数组,有Q个不等式,a[i]+b[j]<=(>=)x,然后问你有没有矛盾。
【分析】一看就是差分系统,但这里是加法,我们不妨令c数组=-b数组,那么a[i]+b[j]==a[i]-c[j],若,a,c合法,则a,b合法。
然后建图,对于每个不等式,转化成u-v<=x的形式,然后v-->u建边,然后判负环,跑个最短路,若某个节点被加入队列>(n+m)次,
则存在负环。
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define inf 1e9+7
using namespace std;
const int N = 2e3+;
int n,m,k;
char str[];
int dis[N],inq[N],cnt[N],q[N*N];
vector<pair<int,int> >edg[N];
void init(){
for(int i=;i<N;i++){
dis[i]=inq[i]=;
edg[i].clear();
}
}
bool spfa(){
int r=;
for(int i=;i<=n+m;i++){
q[++r]=i;inq[i]=true;
cnt[i]=;
}
while(r){
int u=q[r--];
inq[u]=false;
for(int i=;i<edg[u].size();i++){
int v=edg[u][i].first;
int w=edg[u][i].second;
if(dis[u]+w<dis[v]){
dis[v]=dis[u]+w;
if(!inq[v]){
q[++r]=v;
inq[v]=true;
cnt[v]++;
if(cnt[v]>n+m)return false;
}
}
}
}
return true;
}
int main()
{
int x,y,w,T;
scanf("%d",&T);
while(T--){
init();
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++){
scanf("%d%d%s%d",&x,&y,str,&w);
if(str[]=='<'){
edg[y+n].pb(mp(x,w));
}
else {
edg[x].pb(mp(y+n,-w));
}
}
if(spfa())puts("Honest Pong!");
else puts("Pong has lied!");
}
return ;
}

Did Pong Lie? (差分系统 判负环)的更多相关文章

  1. King 差分约束 判负环

    给出n个不等式 给出四个参数第一个数i可以代表序列的第几项,然后给出n,这样前面两个数就可以描述为ai+a(i+1)+...a(i+n),即从i到n的连续和,再给出一个符号和一个ki当符号为gt代表‘ ...

  2. 【10.9校内练习赛】【搜索】【2-sat】【树链剖分】【A_star k短路】【差分约束+判负环】

    在洛谷上复制的题目! P3154 [CQOI2009]循环赛 题目描述 n队伍比赛,每两支队伍比赛一次,平1胜3负0. 给出队伍的最终得分,求多少种可能的分数表. 输入输出格式 输入格式: 第一行包含 ...

  3. BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)

    BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...

  4. POJ——1364King(差分约束SPFA判负环+前向星)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11946   Accepted: 4365 Description ...

  5. poj 1364 King(线性差分约束+超级源点+spfa判负环)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14791   Accepted: 5226 Description ...

  6. poj 3621 二分+spfa判负环

    http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...

  7. Poj(3259),SPFA,判负环

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  8. UVA 11090 Going in Cycle!!(二分答案+判负环)

    在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...

  9. POJ 1860 Currency Exchange (bellman-ford判负环)

    Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...

随机推荐

  1. mysql 数据库备份与还原,用户的创建与删除,用户的密码修改

    1.备份数据库 要退出mysql rimideiMac-23:~ rimi$    mysqldump -u root -p pro >pro.sql ls 查看路径 2.恢复数据库 2.1直接 ...

  2. 【hdu5217-括号序列】线段树

    题意:给一串括号,有2个操作,1.翻转某个括号.2.查询某段区间内化简后第k个括号是在原序列中的位置.1 ≤ N,Q ≤ 200000. 题解: 可以知道,化简后的序列一定是)))((((这种形式的. ...

  3. 【BZOJ】1500: [NOI2005]维修数列

    [算法]splay [题解]数据结构 感谢Occult的模板>_<:HYSBZ 1500 维修数列 #include<cstdio> #include<cctype> ...

  4. ios应用里面进入app store 下载界面

    转自:http://blog.csdn.net/diyagoanyhacker/article/details/6654838 在IOS应用里直接打开app store 评论页面的方法: [[UIAp ...

  5. 【洛谷 P4051】 [JSOI2007]字符加密(后缀数组)

    题目链接 两眼题.. 第一眼裸SA 第二眼要复制一倍再跑SA. 一遍过.. #include <cstdio> #include <cstring> #include < ...

  6. CSS 竖线 点 时间节点

    效果如图 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  7. HDU 4757 可持久化trie树

    首先如果给定一些数,询问这些数中哪个数^给定的数的值最大的话,我们可以建立一颗trie树,根连接的两条边分别为0,1,表示二进制下第15位,那么我们可以建立一颗trie树,每一条从根到叶子节点的链表示 ...

  8. VMware Workstation Pro 14 序列号

    VMware Workstation Pro 14 序列号: AA702-81D8N-0817Y-75PQT-Q70A4 YC592-8VF55-M81AZ-FWW5T-WVRV0 FC78K-FKE ...

  9. js_开发小技巧记录(一)

    (一) 生成从minNum到maxNum的随机数 <!DOCTYPE html> <html> <head> <meta charset="UTF- ...

  10. linux系统引导流程

    1.固件:硬件和软件结合.加电自检是我们按下电源会检测硬件像cpu.内存.网卡等信息.(从硬件层次检测硬件是否可用) 固件设置实例:设置时间 软件时间和硬件时间: [root@VM_0_12_cent ...