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 ...
随机推荐
- bootstrap日历插件地址
http://www.bootcss.com/p/bootstrap-datetimepicker/
- (转)Saltstack系列
Saltstack系列1:安装配置 Saltstack系列2:Saltstack远程执行命令 Saltstack系列3:Saltstack常用模块及API Saltstack系列4:Saltstack ...
- CentOS 7安装zabbix3.0
CentOS 7安装zabbix3.0 一.环境介绍 # systemctl stop firewalld # setenforce 0 # yum -y install unzip vim ne ...
- PHP unlink删除本地中文名称的文件
由于编码不一样,用unlink()方法删除本地中文名称的材料之前,必须先转码,才能删除成功. 核心代码如下: //删除本地的议题材料(本地上传的材料) if($local_ma ...
- 安卓获取输入法高度与ViewTreeObserver讲解
目录 安卓获取输入法高度 前言 清单 开始 ViewTreeObserver讲解 获取输入法高度原理 思路 实现 关于ViewTreeObserver 定义 继承 摘要 获取View高度的三种方法 源 ...
- unity消息队列
解决一些当一些消息事件需要处理时,但是 相应的系统还没有初始化来解决的问题 每个系统执行层也有一个消息队列,这样系统没有做好初始化,不执行就好了. 参考:http://blog.csdn.net/ws ...
- jqgrid 增删改页面快速构建
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InvitationRout ...
- SQL Exists 的用法 转载
比如在Northwind数据库中 有一个查询为 SELECT c.CustomerId, CompanyName FROM Customers c WHERE EXISTS( SELECT O ...
- 阿里巴巴国际站 网站和PC客户端都登录不了,其他电脑或手机可以
背景 昨天晚上,我还能打开阿里巴巴国际站,PC客户端也可以登录 今天早上起床打开电脑,发现国际站的网站打开不了,客户端也登录不了,提示了错误信息,但是其他电脑或手机就可以登录 原因分析 1.是不是本机 ...
- 重构指南 - 尽快返回(Return ASAP )
尽快返回就是如果方法中的条件判断可以得到结果,则尽快返回该结果. 1. 检查条件,如果不满足就立即返回,不执行下面的逻辑. 2. 当包含大量的if else嵌套,代码可读性变差,也容易出现异常. 3. ...