嘟嘟嘟

题目翻译:有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的更多相关文章

  1. BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3336  Solved: 1936[Submit][ ...

  2. [bzoj1087][scoi2005]互不侵犯king

    题目大意 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上 左下右上右下八个方向上附近的各一个格子,共8个格子. 思路 首先,搜索可以放弃,因为这是一 ...

  3. King's Quest —— POJ1904(ZOJ2470)Tarjan缩点

    King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...

  4. 【状压DP】bzoj1087 互不侵犯king

    一.题目 Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上.下.左.右,以及左上.左下.右上.右下八个方向上附近的各一个格子,共8个格子. I ...

  5. ZOJ 2334 Monkey King

    并查集+左偏树.....合并的时候用左偏树,合并结束后吧父结点全部定成树的根节点,保证任意两个猴子都可以通过Find找到最厉害的猴子                       Monkey King ...

  6. 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 ...

  7. BZOJ-1087 互不侵犯King 状压DP+DFS预处理

    1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2337 Solved: 1366 [Submit][ ...

  8. POJ1364 King

    Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen p ...

  9. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

随机推荐

  1. 算法市场 Algorithmia

    算法市场 官网:(需要***,fan qiang,不然可能访问不了或登录不了) https://algorithmia.com/ 官方的例子: 我不用 curl 发请求,把 curl 命令粘贴给你们用 ...

  2. 在Spark shell中基于Alluxio进行wordcount交互式分析

    Spark是一个分布式内存计算框架,可部署在YARN或者MESOS管理的分布式系统中(Fully Distributed),也可以以Pseudo Distributed方式部署在单个机器上面,还可以以 ...

  3. xampp安装步骤及启动

    1  chmod 755 xampp-linux-*-installer.run 2 sudo ./xampp-linux-*-installer.run 启动停止 3 sudo /opt/lampp ...

  4. 【ExtJS】关于constructor、initComponent、beforeRender

    ExtJS提供的组件非常丰富,不过当原生的组件无法满足要求时,就需要扩展原生自定义组件了. initComponent 和 constructor 就是Extjs 提供用来实现继承和扩展的方式. 在E ...

  5. PlayMaker入门介绍

    http://www.jianshu.com/p/ce791bef66bb   PlayMaker是什么? PlayMaker是Unity3D的一款 可视化 的 有限元状态机(Finite-state ...

  6. ssh免密码登录快速配置方法

    环境需求: 两台Linux主机   A (192.168.3.101)和 B(192.168.3.102),如何使用主机 A 免密码登录 主机B ? 配置步骤: 首页登录主机 A ,在主机A上生成自己 ...

  7. Linux文本处理工具

    Linux文本处理工具 Linux中熟练的使用文本处理工具非常的重要, 因为Linux在设计的时候是采用一切皆文件的哲学的, 甚至连计算机中的配置也都使用伪文件系统来表示, 要查询里面的内容就是对文件 ...

  8. hash扩展攻击本地实验

    记录一下自己对hash扩展攻击的一些理解,hash扩展攻击主要应用于身份认证,比如对于成功登录的用户可以赋予其一个采用hsah算法加密的cookie值,其中含有未知的密钥. 此后每次在服务器端验证此c ...

  9. js跑步算法

    <!DOCTYPE html> <html> <head> <title>JavaScript</title> <style> ...

  10. sp_addlinkedserver创建远程服务器查询

    远程服务器查询可以分两步完成: 1.建立连接服务器 exec sp_addlinkedserver @server='Test_Server', --被访问的服务器别名 @srvproduct='', ...