UVA515 King
题目翻译:有n个数,m个限制条件。每一个限制条件形如:1.x y gt c:表示ax + ax+1 + … +ay > c。2.x y It c:表示ax + ax+1 + …… +ay < c。有解输出“lamentable kingdom”,否则输出“successful conspiracy”。
对于每一个限制条件,用前缀和的思想,就变成了Sy - Sx-1 > c 和 Sy - Sx-1 < c。于是就是一个典型的差分约束模型。不过差分约束必须满足 ’<=',于是<x就转换成<= x - 1,> x就转换成>= x + 1。
建好图后跑spfa判负环即可。
但还有一个问题,就是应该从哪个节点开始?思考一下,我们只用判断图中是否存在负环,因此从那一个点开始都行,那么不放建一个超级源点,向所有点连一条边权为0的边。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-;
const int maxn = ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, m;
char s[];
struct Edge
{
int to, w, nxt;
}e[maxn << ];
int head[maxn], ecnt = ;
void addEdge(int x, int y, int w)
{
e[++ecnt] = (Edge){y, w, head[x]};
head[x] = ecnt;
} bool in[maxn];
int dis[maxn], cnt[maxn];
bool spfa(int s)
{
Mem(in, ); Mem(cnt, );
Mem(dis, 0x3f); dis[s] = ;
queue<int> q; q.push(s);
while(!q.empty())
{
int now = q.front(); q.pop();
in[now] = ;
for(int i = head[now]; i; i = e[i].nxt)
{
if(dis[e[i].to] > dis[now] + e[i].w)
{
dis[e[i].to] = dis[now] + e[i].w;
if(!in[e[i].to])
{
in[e[i].to] = ;
if(++cnt[e[i].to] > n + ) return ;
q.push(e[i].to);
}
}
}
}
return ;
} void init()
{
Mem(head, );
ecnt = ;
} int main()
{
while(scanf("%d", &n) && n)
{
m = read();
init();
for(int i = ; i <= m; ++i)
{
int x = read() + , y = read();
scanf("%s", s); int t = read();
if(s[] == 'g') addEdge(x + y, x - , - t - );
else addEdge(x - , x + y, t - );
}
for(int i = ; i <= n + ; ++i) addEdge(, i, );
printf("%s\n", spfa() ? "lamentable kingdom" : "successful conspiracy");
}
return ;
}
UVA515 King的更多相关文章
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- [bzoj1087][scoi2005]互不侵犯king
题目大意 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上 左下右上右下八个方向上附近的各一个格子,共8个格子. 思路 首先,搜索可以放弃,因为这是一 ...
- King's Quest —— POJ1904(ZOJ2470)Tarjan缩点
King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...
- 【状压DP】bzoj1087 互不侵犯king
一.题目 Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上.下.左.右,以及左上.左下.右上.右下八个方向上附近的各一个格子,共8个格子. I ...
- ZOJ 2334 Monkey King
并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子 Monkey King ...
- ACM ICPC 2015 Moscow Subregional Russia, Moscow, Dolgoprudny, October, 18, 2015 K. King’s Rout
K. King's Rout time limit per test 4 seconds memory limit per test 512 megabytes input standard inpu ...
- BZOJ-1087 互不侵犯King 状压DP+DFS预处理
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...
- POJ1364 King
Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
随机推荐
- MySQL 常用show 语句
1. show tables或show tables from database_name; -- 显示当前数据库中所有表的名称. 2. show databases; -- 显示mysql中所有数据 ...
- Oracle 单实例数据库安装和real application clusters数据库安装的区别
在想了解Oracle单实例数据可和RAC数据库前,请确保你已经知道了数据库和实例的关系,如果不了解,请参考Oracle 数据库实例和数据库. 单实例数据库模式 单实例模式下,一个数据库只能通过一个实例 ...
- 第十九章:UTC time和local time的互换
通常我们服务器端的时间使用UTC格式,避免服务器的时区对最终的时间产生影响.而客户端需要根据具体的时区显示local time,本文将介绍如何将服务器的UTC time(基于asp.net web a ...
- oracle客户端中文乱码问题的解决
1 查看服务器端编码 select userenv('language') from dual; 我实际查看到的结果为: USERENV('LANGUAGE') ------------------- ...
- lua-遍历集合-ipairs和pairs的区别
--ipairs和pairs的区别arr = {1,3,[5]=5,name="kaikai",age=12, 89}--arr[4]= 23--ipairs--ipairs仅仅遍 ...
- flex buider 4.6 打开设计模式(designer)时提示内存不足错误的解决办法
先申明,此方法只适用于flex builder 4.6及以下版本, flex builder 4.7以后已经完全取消了designer功能,是没有办法补救的. 1. 首先下载APE文件,这个文件好像是 ...
- 深入理解JavaScript系列(16):闭包(Closures)
介绍 本章我们将介绍在JavaScript里大家经常来讨论的话题 —— 闭包(closure).闭包其实大家都已经谈烂了.尽管如此,这里还是要试着从理论角度来讨论下闭包,看看ECMAScript中的闭 ...
- JS && || 陷阱 javascript 逻辑与、逻辑或 【转】
通常来说逻辑运算a&&b和a||b分别是逻辑与运算和逻辑或运算,返回的是一个布尔值,要么为true,要么为false. 比如在PHP里面a&&b返回类型永远是布尔值,非 ...
- 富文本编辑框和防止xss攻击
一.后台管理页面构建 1.创建后台管理url urlpatterns = [ ... # 后台管理url re_path("cn_backend/$", views.cn_back ...
- bootstrap模态框实现相对定位拖拽
1.正常的拖拽是用绝对定位absolute来实现的,可是bootstrap的模态框是用relative,为了统一更改方便,就照着相对定位来实现拖拽效果. $(".modal .modal-h ...