HDU King (非连通图的差分约束,经典好题)
King
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2216 Accepted Submission(s): 999
Unfortunately, as it used to happen in royal families, the son was a little retarded. After many years of study he was able just to add integer numbers and to compare whether the result is greater or less than a given integer number. In addition, the numbers had to be written in a sequence and he was able to sum just continuous subsequences of the sequence.
The old king was very unhappy of his son. But he was ready to make everything to enable his son to govern the kingdom after his death. With regards to his son's skills he decided that every problem the king had to decide about had to be presented in a form of a finite sequence of integer numbers and the decision about it would be done by stating an integer constraint (i.e. an upper or lower limit) for the sum of that sequence. In this way there was at least some hope that his son would be able to make some decisions.
After the old king died, the young king began to reign. But very soon, a lot of people became very unsatisfied with his decisions and decided to dethrone him. They tried to do it by proving that his decisions were wrong.
Therefore some conspirators presented to the young king a set of problems that he had to decide about. The set of problems was in the form of subsequences Si = {aSi, aSi+1, ..., aSi+ni} of a sequence S = {a1, a2, ..., an}. The king thought a minute and then decided, i.e. he set for the sum aSi + aSi+1 + ... + aSi+ni of each subsequence Si an integer constraint ki (i.e. aSi + aSi+1 + ... + aSi+ni < ki or aSi + aSi+1 + ... + aSi+ni > ki resp.) and declared these constraints as his decisions.
After a while he realized that some of his decisions were wrong. He could not revoke the declared constraints but trying to save himself he decided to fake the sequence that he was given. He ordered to his advisors to find such a sequence S that would satisfy the constraints he set. Help the advisors of the king and write a program that decides whether such a sequence exists or not.
InputThe input consists of blocks of lines. Each block except the last corresponds to one set of problems and king's decisions about them. In the first line of the block there are integers n, and m where 0 < n <= 100 is length of the sequence S and 0 < m <= 100 is the number of subsequences Si. Next m lines contain particular decisions coded in the form of quadruples si, ni, oi, ki, where oi represents operator > (coded as gt) or operator < (coded as lt) respectively. The symbols si, ni and ki have the meaning described above. The last block consists of just one line containing 0.OutputThe output contains the lines corresponding to the blocks in the input. A line contains text successful conspiracy when such a sequence does not exist. Otherwise it contains text lamentable kingdom. There is no line in the output corresponding to the last ``null'' block of the input.Sample Input
4 2
1 2 gt 0
2 2 lt 2
1 2
1 0 gt 0
1 0 lt 0
0
Sample Output
lamentable kingdom
successful conspiracy
题目很长,意思是:有一个长度为n的序列和它的m个子序列。每一个子序列的和都有一个k来约束。gt代表大于k,lt代表小于k。问是否存在这个长度为n的序列。
让我们来思考这道题,首先对于给出的每个子序列我们可以看成子序列尾部到原序列起点的和减去子序列起点前一位到原序列起点的和。
例如:原序列为a[1]+a[2]+.....a[s]+...a[n]+a[n+1]+...... 子序列a[s]+....+a[n]=S[n]-S[s-1];
这样我们可以把S[n], S[s-1]看成两点,约束值k就可以看成对这两点间权大小和方向的约束。 按照上述过程可以建立起表示S[]点间关系的图。则就把问题转化为了最短路径问题。
若这个序列存在,则S[i]到图中任意一点都有最短路径,则图中不存在负环。所以判断是否存在序列就是判断是否存在负环。
好,具体思路清楚了,我们来研究如何建立起图。题中给出的约束条件有 > 和 < 。我们要做的事是将所有的约束条件全部转化为<= 。 即建立差分约束系统。
分析:
我们令S[i]= a1+a2+..+ai. 所以对于每一条约束条件比如:
a[si]+a[si+1]+…+a[si+ni]< ki . 我们可以转化为 S[si+ni] – S[si-1] <= ki-1.
这样就可以转化为了差分约束系统了.
该系统具有点的集合为0, 1,… n.其中对于S[si+ni] – S[si-1] <= ki-1条件我们可以得到 si-1 到 si+ni 的权值为ki-1的边.
对于a[si]+a[si+1]+…+a[si+ni] >ki 即 S[si+ni] – S[si-1] >=ki+1 我们可以得到 si+ni 到 si-1 的权值为-ki-1 的边.
而在判断差分约束系统是否有解时,建立的路径图可能不是连通的。因此我们还需要虚构一个超级源n+1号点.使得从n+1号点有边出来到0,1,…n号点且权值为0.
注意:图中原有的点是0到n共n+1个点
第一次讨论差分约束系统问题,若叙述有误,请各位指出。
具体代码如下:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define maxn 110
#define maxm 10010
#define inf 0x3f3f3f
using namespace std;
int n,m;
struct node
{
int to;
int next;
int c;
}edge[maxn];
int head[maxn];
int mark[maxn];
int cnt=;
void add(int u,int v,int c)
{
edge[cnt].next=head[u];
edge[cnt].to=v;
edge[cnt].c=c;
head[u]=cnt++;
}
int dis[maxn];
bool visit[maxn];//记录是否在队列中
void spfa()
{
queue<int>q;
for(int i=;i<=n+;i++)
{
dis[i]=inf;
visit[i]=;
mark[i]=;
}
dis[n+]=;
visit[n+]=;
mark[n+]++;
q.push(n+);
while(!q.empty())
{
int u=q.front();
q.pop();
visit[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].c)
{
dis[v]=dis[u]+edge[i].c;
if(!visit[v])
{
q.push(v);
visit[v]=;
mark[v]++;
if(mark[v]>n+)
{
printf("successful conspiracy\n");
return;
}
}
}
}
}
printf("lamentable kingdom\n");
} int main()
{
while(cin>>n&&n)
{
cin>>m;
char s[];
int u,v,c;
cnt=;
memset(head,-,sizeof(head));
for(int i=;i<=m;i++)
{
scanf("%d%d%s%d",&u,&v,&s,&c);
if(s[]=='g')
{
add(u+v,u-,-(c+));
}
else if(s[]=='l')
{
add(u-,u+v,c-);
}
}
for(int i=;i<=n;i++)
{
add(n+,i,);
}
spfa();
}
return ;
}
HDU King (非连通图的差分约束,经典好题)的更多相关文章
- poj 3159(差分约束经典题)
题目链接:http://poj.org/problem?id=3159思路:题目意思很简单,都与给定的条件dist[b]-dist[a]<=c,求dist[n]-dist[1]的最大值,显然这是 ...
- HDU 3666 THE MATRIX PROBLEM (差分约束)
题意:给定一个最大400*400的矩阵,每次操作可以将某一行或某一列乘上一个数,问能否通过这样的操作使得矩阵内的每个数都在[L,R]的区间内. 析:再把题意说明白一点就是是否存在ai,bj,使得l&l ...
- hdu1529 差分约束(好题)
题意: 超市在每个时间都有需要的人数(24小时)比如 1 0 0 0 0 ....也就是说在第0个小时的时候要用一个人,其他的时间都不用人,在给你一些人工作的起始时间,如果雇佣了这个人,那 ...
- hdu 1529 Cashier Employment(差分约束)
Cashier Employment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 3592 World Exhibition (差分约束,spfa,水)
题意: 有n个人在排队,按照前后顺序编号为1~n,现在对其中某两人的距离进行约束,有上限和下限,表示dis[a,b]<=c或者dis[a,b]>=c,问第1个人与第n个人的距离最多可能为多 ...
- HDU——2647Reward(DFS或差分约束)
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 1232 (畅通工程) 并查集经典模板题
Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可以实现交通 ...
- 【转】最短路&差分约束题集
转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...
- 转载 - 最短路&差分约束题集
出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★ ...
随机推荐
- Python学习札记(二十八) 模块1
参考:模块 NOTE 1.模块:一个.py文件称为一个模块. 2.代码模块化的意义:a.提升程序的可维护性 b.不用重复造轮子 3.避免模块冲突,解决方法:引入了按目录来组织模块的方法,称为包(Pac ...
- 51Nod 1070 Bash游戏 V4(斐波那契博弈)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1070 题意: 思路: 这个是斐波那契博弈,http://blog.csd ...
- 使用javascript模拟常见数据结构(三)
六.字典和散列表 我们已经知道,集合表示一组互不相同的元素(不重复元素).在字典中,存储的是键值对,其中键值是用来查询特定的元素的.字典和集合是很相似的,集合采用[值,值]的方式存储,而字典则是以[键 ...
- python 字典输出键值对
d = {, , } for dict_key, dict_value in d.items(): print(dict_key,'->',dict_value)
- Java字符串分割(转)
java.lang.String 的 split() 方法, JDK 1.4 or later public String[] split(String regex,int limit) 示例代码 p ...
- android------引导页两种实现方式(原生和WebView网页实现)
有的App当你第一次打开的是和常常会有引导页来描述一些App信息(功能,特点),当然也要做验证,验证第二次进入不进入引导页,直接进入App,此博客借助ViewPager来实现引导页, ViewPage ...
- HDU 5186 zhx's submissions 模拟,细节 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=5186 题意是分别对每一位做b进制加法,但是不要进位 模拟,注意:1 去掉前置0 2 当结果为0时输出0,而不是全 ...
- web 常用富文本编辑器
1. 百度家的 UEditor 官网地址http://ueditor.baidu.com/website/;在线演示地址:http://ueditor.baidu.com/website/onlin ...
- [置顶]
VS 2017 众多重构插件
孙广东 2017.7.22 http://blog.csdn.NET/u010019717 1.没有任何插件的情况下: (就是Ctrl + .) 注意:这个. 要是英文的才行! 右键菜单也是 ...
- c++的关联容器入门(map and set)
目录 std::map std::set C++的关联容器主要是两大类map和set 我们知道谈到C++容器时,我们会说到 顺序容器(Sequence containers),关联容器(Associa ...