【HDOJ1531】【差分约束+添加超级源点】
http://acm.hdu.edu.cn/showproblem.php?pid=1531
King
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1992 Accepted Submission(s): 928
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.
1 2 gt 0
2 2 lt 2
1 2
1 0 gt 0
1 0 lt 0
0
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct edge{
int to;
int next;
int len;
}qwq[];
queue<int>qaq;
int edge_cnt,n,m,in[],stk[],dist[],head[];
void add(int x,int y,int z)
{
qwq[edge_cnt].len = z;
qwq[edge_cnt].to = y;
qwq[edge_cnt].next=head[x];
head[x]=edge_cnt++;
}
bool spfa()
{
while(!qaq.empty())
{
qaq.pop();
}
dist[n+]=;
qaq.push(n+);
stk[n+]=;
in[n+]++;
while(!qaq.empty())
{
int orz=qaq.front();qaq.pop();
stk[orz]=;
for(int i = head[orz] ; i != - ; i=qwq[i].next)
{
int v=qwq[i].to;
if(dist[v]<dist[orz]+qwq[i].len)
{
dist[v]=dist[orz]+qwq[i].len;
if(!stk[v])
{
in[v]++;
qaq.push(v);
stk[v]=;
if(in[v]>n+)
return false;
}
}
}
}
return true;
}
int main()
{
// int n,m;
scanf("%d",&n);
while(n)
{
edge_cnt = ;
memset(dist,-,sizeof(dist));
memset(head,-,sizeof(head));
memset(in,,sizeof(in));
memset(stk,,sizeof(stk));
edge_cnt=;
scanf("%d",&m);
while(m--)
{
int a,b,c;
char d,e;
scanf("%d %d %c%c %d",&a,&b,&d,&e,&c);
if(d=='g')
{
add(a-,a+b,c+);
}
else
{
add(a+b,a-,-c+);
} }
for(int i = ; i <= n ; i++)
{
add(n+,i,);
}
if(spfa())printf("lamentable kingdom\n");
else printf("successful conspiracy\n");
scanf("%d",&n);
}
return ;
}
【HDOJ1531】【差分约束+添加超级源点】的更多相关文章
- HDU 2680 最短路 迪杰斯特拉算法 添加超级源点
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- POJ 1364 King (差分约束)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8660 Accepted: 3263 Description ...
- [poj 1364]King[差分约束详解(续篇)][超级源点][SPFA][Bellman-Ford]
题意 有n个数的序列, 下标为[1.. N ], 限制条件为: 下标从 si 到 si+ni 的项求和 < 或 > ki. 一共有m个限制条件. 问是否存在满足条件的序列. 思路 转化为差 ...
- poj 1364 King(线性差分约束+超级源点+spfa判负环)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14791 Accepted: 5226 Description ...
- 图论--差分约束--POJ 3169 Layout(超级源汇建图)
Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 < ...
- spfa+差分约束系统(D - POJ - 1201 && E - POJ - 1364&&G - POJ - 1)+建边的注意事项+超级源点的建立
题目链接:https://cn.vjudge.net/contest/276233#problem/D 具体大意: 给出n个闭合的整数区间[ai,bi]和n个整数c1,-,cn. 编写一个程序: 从标 ...
- Is the Information Reliable?(差分约束)
Description The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years a ...
- 鉴于spfa基础上的差分约束算法
怎么搞? 1. 如果要求最大值 想办法把每个不等式变为标准x-y<=k的形式,然后建立一条从y到x权值为k的边,变得时候注意x-y<k =>x-y<=k ...
- hdu 1531(差分约束)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1531 差分约束的题之前也碰到过,刚好最近正在进行图论专题的训练,就拿来做一做. ①:对于差分不等式,a ...
随机推荐
- POJ 2243 Knight Moves(BFS)
POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...
- mv
mv命令是move的缩写,可以用来移动文件或者将文件改名,这也是个常用命令,经常用来备份文件或者目录. 1.命令格式: mv [选项] 源文件或目录 目标文件或目录 2.命令功能: 视mv命令中第 ...
- bzoj1096
题解: 斜率优化dp 代码: #include<bits/stdc++.h> typedef long long ll; ; using namespace std; int n,l,r, ...
- 理解JavaScript的运行
JavaScript可以运行在head和body标签中! HTML的脚本必须放在<script></script>标签中间! 浏览器会解释并执行位于script标签中的脚本! ...
- Android开发---网格布局案例
Android开发---网格布局案例 效果图: 1.MainActivity.java package com.example.android_activity; import android.ap ...
- htmlayout 最简单的实践,用于理解实现原理。
/ testHtmlayout.cpp : 定义应用程序的入口点. // #include "stdafx.h" #include "testHtmlayout.h&qu ...
- Linux文件系统命令 lsof
命令名:lsof 功能:查看某个端口被占用情况,在配置服务器端口的时候,为了避免冲突,可以通过这个命令查看将要被使用的端口是否被占用. 用法:加-i 参数 eg: renjg@renjg-HP-Com ...
- innodb mvcc,事务隔离级别,读写锁
mvcc其实和copyonwritelist的思路差不多:读不加锁,写加锁,事务提交之后释放锁,并且延伸的是,在UNdolog里面保存了几个版本,实现不同的隔离级别.如果读数据页里面最新的数据,那么就 ...
- Android:getContext().getSystemService()
一.介绍 getSystemService是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象 二.语法 Windo ...
- Oracle create tablespace 、create user and so on
1.创建临时表空间 CREATE TEMPORARY TABLESPACE test_tempTEMPFILE 'C:\oracle\product\10.1.0\oradata\orcl\test_ ...