Did Pong Lie? (差分系统 判负环)
Did Pong Lie?
时间限制: 5 Sec 内存限制: 128 MB
提交: 68 解决: 15
[提交][状态][讨论版]
题目描述
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.
输入
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? (差分系统 判负环)的更多相关文章
- King 差分约束 判负环
给出n个不等式 给出四个参数第一个数i可以代表序列的第几项,然后给出n,这样前面两个数就可以描述为ai+a(i+1)+...a(i+n),即从i到n的连续和,再给出一个符号和一个ki当符号为gt代表‘ ...
- 【10.9校内练习赛】【搜索】【2-sat】【树链剖分】【A_star k短路】【差分约束+判负环】
在洛谷上复制的题目! P3154 [CQOI2009]循环赛 题目描述 n队伍比赛,每两支队伍比赛一次,平1胜3负0. 给出队伍的最终得分,求多少种可能的分数表. 输入输出格式 输入格式: 第一行包含 ...
- BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)
BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...
- POJ——1364King(差分约束SPFA判负环+前向星)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11946 Accepted: 4365 Description ...
- poj 1364 King(线性差分约束+超级源点+spfa判负环)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14791 Accepted: 5226 Description ...
- poj 3621 二分+spfa判负环
http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...
- Poj(3259),SPFA,判负环
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- UVA 11090 Going in Cycle!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
- POJ 1860 Currency Exchange (bellman-ford判负环)
Currency Exchange 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/E Description Several c ...
随机推荐
- 「模板」 01 Trie实现平衡树功能
不想多说什么了.费空间,也不算太快,唯一的好处就是好写吧. #include <cstdio> #include <cstring> const int MAXN=100010 ...
- [Luogu 1160] 队列安排
Luogu 1160 队列安排 链表H2O H2O H2O模板. 太久不写链表,忘干净了,竟调了半个晚上. 保留备用. #include <cstdio> #include <cst ...
- Asp.net Web Api 2 FORM Authentication Demo
最近看了一点 web api 2方面的书,对认证都是简单介绍了下,所以我在这里做个简单Demo,本文主要是FORM Authentication,顺带把基本认证也讲了. Demo 一.FORM Aut ...
- 【比赛】STSRM 09
第一题 题意:n个点,每个点坐标pi属性ai,从右往左将遇到的点向左ai范围内的点消除,后继续扫描. 现可以在扫描开始前提前消除从右往左任意点,问最少消除数(提前+扫描). n,pi,ai<=1 ...
- 【设计模式】原型模式(Prototype)
摘要: 1.本文将详细介绍原型模式的原理和实际代码中特别是Android系统代码中的应用. 纲要: 1. 引入原型模式 2. 原型模式的概念及优缺点介绍 3. 原型模式对拷贝的使用 4. 原型模式在A ...
- c#上传文件时,当选择的文件为0kb,会验证不通过
FileUpload1.HasFile 当FileUpload1控件选择的文件为0KB时,FileUpload1.HasFile返回false
- 一个朋友 js图表开发问题 用 c和 js 解决
引言 不求知道一切, 只求发现一件 -- 乔治·西蒙·欧姆 附注:那些存在于梦幻中的事迹,那些儿时梦中的人物,每每看起,都觉得 .哎 .... 岁月 ... 一直在努力 ... ...
- ASPxCheckBoxList控件获取selected项的text和value的方法
设ASPxCheckBoxList的ClientInstanceName为list_ var needtext; for (var i = 0; i < list_.GetSelectedIte ...
- ArcGIS Server配置端口
写在前面,GIS服务器必须连通到外网,基于某些情况,可能一个机组有多态服务器,担任不同的角色,有Web服务器.数据库服务器和GIS服务器等,但是可能购买时只有一个外网IP,这样是不行的.JS脚本运行在 ...
- C# 笔记——排序
首先,一张图看懂8中排序之间的关系: 平均速度最快:快速排序 所需辅助空间最多:归并排序 所需辅助空间最少:堆排序 不稳定:快速排序,希尔排序,堆排序. 1. 直接插入排序 基本思想:在要排序的一组数 ...