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. rabbitmq使用命令创建交换机、队列及绑定

    http://www.jusene.me/2018/02/20/rabbitmq-2/

  2. 【51NOD-0】1137 矩阵乘法

    [算法]简单数学 [题解] 对于A*B=C C中第i行第j列的数字由A中第i行和B中的j列的数字各自相乘后相加得到. 所以两个矩阵能相乘要求A的列数等于B的行数,复杂度为O(n3). #include ...

  3. 基本控件文档-UISlider属性---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址  //转载请注明出处--本文永久链接:http://www.cnblogs.com/C ...

  4. WordPress在nginx服务器伪静态

    server { listen 80; root /var/www/xxx; server_name www.xxx.com; access_log /var/log/www/xxx.log main ...

  5. POJ 2456 Aggressive cows ( 二分搜索)

    题目链接 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The ...

  6. 边缘检测:Canny算子,Sobel算子,Laplace算子

    1.canny算子 Canny边缘检测算子是John F.Canny于 1986 年开发出来的一个多级边缘检测算法.更为重要的是 Canny 创立了边缘检测计算理论(Computational the ...

  7. Tomcat8配置默认项目

    <!-- 配置默认访问项目 --> <Host name="localhost" appBase="webapps" unpackWARs=& ...

  8. 64_m2

    mimetic-devel-0.9.8-7.fc26.i686.rpm 12-Feb-2017 05:40 288474 mimetic-devel-0.9.8-7.fc26.x86_64.rpm 1 ...

  9. GLIBCXX_3.4.9' not found - 解决办法

    GLIBCXX_3.4.9' not found - 解决办法 http://blog.csdn.net/u012425536/article/details/26559653 https://koj ...

  10. python设计模式之单例模式(二)

    上次我们简单了解了一下什么是单例模式,今天我们继续探究.上次的内容点这 python设计模式之单例模式(一) 上次们讨论的是GoF的单例设计模式,该模式是指:一个类有且只有一个对象.通常我们需要的是让 ...