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的边。
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<cstring> 6 #include<cstdlib> 7 #include<cctype> 8 #include<vector> 9 #include<stack> 10 #include<queue> 11 using namespace std; 12 #define enter puts("") 13 #define space putchar(' ') 14 #define Mem(a, x) memset(a, x, sizeof(a)) 15 #define rg register 16 typedef long long ll; 17 typedef double db; 18 const int INF = 0x3f3f3f3f; 19 const db eps = 1e-8; 20 const int maxn = 105; 21 inline ll read() 22 { 23 ll ans = 0; 24 char ch = getchar(), last = ' '; 25 while(!isdigit(ch)) {last = ch; ch = getchar();} 26 while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();} 27 if(last == '-') ans = -ans; 28 return ans; 29 } 30 inline void write(ll x) 31 { 32 if(x < 0) x = -x, putchar('-'); 33 if(x >= 10) write(x / 10); 34 putchar(x % 10 + '0'); 35 } 36 37 int n, m; 38 char s[2]; 39 struct Edge 40 { 41 int to, w, nxt; 42 }e[maxn << 1]; 43 int head[maxn], ecnt = 0; 44 void addEdge(int x, int y, int w) 45 { 46 e[++ecnt] = (Edge){y, w, head[x]}; 47 head[x] = ecnt; 48 } 49 50 bool in[maxn]; 51 int dis[maxn], cnt[maxn]; 52 bool spfa(int s) 53 { 54 Mem(in, 0); Mem(cnt, 0); 55 Mem(dis, 0x3f); dis[s] = 0; 56 queue<int> q; q.push(s); 57 while(!q.empty()) 58 { 59 int now = q.front(); q.pop(); 60 in[now] = 0; 61 for(int i = head[now]; i; i = e[i].nxt) 62 { 63 if(dis[e[i].to] > dis[now] + e[i].w) 64 { 65 dis[e[i].to] = dis[now] + e[i].w; 66 if(!in[e[i].to]) 67 { 68 in[e[i].to] = 1; 69 if(++cnt[e[i].to] > n + 1) return 0; 70 q.push(e[i].to); 71 } 72 } 73 } 74 } 75 return 1; 76 } 77 78 void init() 79 { 80 Mem(head, 0); 81 ecnt = 0; 82 } 83 84 int main() 85 { 86 while(scanf("%d", &n) && n) 87 { 88 m = read(); 89 init(); 90 for(int i = 1; i <= m; ++i) 91 { 92 int x = read() + 2, y = read(); 93 scanf("%s", s); int t = read(); 94 if(s[0] == 'g') addEdge(x + y, x - 1, - t - 1); 95 else addEdge(x - 1, x + y, t - 1); 96 } 97 for(int i = 1; i <= n + 2; ++i) addEdge(0, i, 0); 98 printf("%s\n", spfa(0) ? "lamentable kingdom" : "successful conspiracy"); 99 } 100 return 0; 101 }
View Code
转载于:https://www.cnblogs.com/mrclr/p/9791816.html
UVA515 King相关推荐
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- poj 1904 King's Quest
King's Quest 题意:有N个王子和N个妹子;(1 <= N <= 2000)第i个王子喜欢Ki个妹子:(详见sample)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚:问每 ...
- 新概念英语(1-73)The way to King Street
The way to King Street 到国王街的走法 Why did the man need a phrasebook? Last week Mrs. Mills went to Londo ...
- POJ 1364:King(差分约束)
题目大意:判断是否存在一个长度为n的序列满足给出的不等关系. 分析: 将序列和转化成用两个前缀和之差来表示即可变为差分约束系统. 需要注意的是不能忘记n要加+1,因为还有一个特殊源点,自己因为n:=n ...
- hdu5643 King's Game(约瑟夫环+线段树)
Problem Description In order to remember history, King plans to play losephus problem in the parade ...
- POJ2669不错的最大流 竞赛问题(枚举King的个数)
题意: 有n个人,任意两个人都比一次赛(一共比了n*(n-1)/2场),赢一场得到一分,最后的时候如果得分最高,或者是自己打败了所有比自己得分都高的人就算是King,给你每个人的最后得分, ...
- BZOJ 1087: [SCOI2005]互不侵犯King
二次联通门 : BZOJ 1087: [SCOI2005]互不侵犯King /*BZOJ 1087: [SCOI2005]互不侵犯King状压dp将每一行棋子的存在状态压成一个整数f[i][j][k] ...
- a king读后感 love of the_读后感kinglear
Lear King. "And Othello" together, and called Shakespeare's "four tragedies" &qu ...
- King of the Ether
An Ethereum contract, living on the blockchain, that will make you a King or Queen, might grant you ...
- poj1364 King
地址:http://poj.org/problem?id=1364 题目: King Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
最新文章
- des解密 given final_真相解密创新Aurvana Live SE评测怎么样?【使用一个月后感受实情爆料!!!...
- 校园网站服务器配置参数,校园网服务器性能 配置及分布
- matlab中的nlinfit,matlab中nlinfit问题!!!!!!!!!!!!
- 怎样用Java 8优雅的开发业务
- Interrupted Exception异常可能没你想的那么简单!
- php微博发布时间,PHP格式化显示时间函数,用于微博、社交媒体等,类似豆瓣
- HTML5实践 -- 使用css装饰你的图片画廊
- [转]文本编辑软件UltraEdit v16.20官方简/繁体中文版下载+注册码和破解方法
- 【分享】学长的安利来了~~O(∩_∩)O
- 拓端tecdat|R语言模拟ARCH过程模型分析时间序列平稳性、波动性
- mac上iphone4刷机与越狱(二)
- 【hadoop权威指南第四版】第四章hadoop的IO【笔记+代码】
- 请教一下水卡校验算法
- mongoose 入门(四)使用aggregate 聚合管道、使用 populate 实现关联查询
- 谷歌云端硬盘下载文件_如何使用Google云端硬盘中的Microsoft Office文件
- 讯飞离线语音合成接入
- ZYNQ基础----裸机USB的使用
- 深度特征融合---高低层(多尺度)特征融合
- 乐视更新APP,图标显示欠122亿,反向营销?贾跃亭将回国造梦?
- tcp_tw_reuse对客户端的作用