UVA 515 King
差分约束系统的第一个题目,看了落花大神的博客后,对差分约束有了一定了解,关键在于建图,然后就是判断是否存在负权回路。
关于差分约束系统的解释详见维基百科:http://zh.wikipedia.org/wiki/%E5%B7%AE%E5%88%86%E7%BA%A6%E6%9D%9F%E7%B3%BB%E7%BB%9F
利用spfa判断时,当图中有顶点出队次数多于图中顶点数目时说明存在负环。
其实我自己敲上去的时候改了一点点。
大神的time[g[x][i].v]当达到n+1时就返回false,这是不对的,因为点包括0嘛,所以最终应该有n+1个点,判断就应该改为达到n+2,而且这一点也从uva上得到证明,
直接交n+1的版本是过不了的,n+2,才能过。
以下是我改过大神的代码:
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <vector>
#define N 110
using namespace std; struct edgeType{
int v, w;
edgeType(int a, int b):v(a), w(b){}
};
int n, m, d[N], time[N], inq[N];
char op[]; vector<edgeType> g[N];
bool spfa(void)
{
queue<int> q;
time[n] = ;
inq[n] = ;
q.push(n);
while(!q.empty())
{
int x = q.front();
q.pop();
inq[x] = ;
for(int i = ; i < g[x].size(); i++)
if(d[g[x][i].v] > d[x] + g[x][i].w)
{
d[g[x][i].v] = d[x] + g[x][i].w;
time[g[x][i].v]++;
if(time[g[x][i].v] == n+)
return false;
if(!inq[g[x][i].v])
{
q.push(g[x][i].v);
inq[g[x][i].v] = ;
}
}
}
return true;
} int main(void)
{
int a, b, c;
while(scanf("%d%d",&n, &m)&&n)
{
n++;
memset(d, 0x0f, sizeof(d));
memset(inq, , sizeof(inq));
memset(time, , sizeof(time));
d[n] = ;
g[n].clear();
for(int i = ; i < n;i++)
g[n].push_back(edgeType(i, )), g[i].clear();
for(int i = ; i < m; i++)
{
scanf("%d%d%s%d", &a, &b, op, &c);
if(op[] == 'g')
g[a + b].push_back(edgeType(a - , -c - ));
else g[a - ].push_back(edgeType(a + b, c - ));
}
if(spfa())
puts("lamentable kingdom");
else puts("successful conspiracy");
}
return ;
}
然后我利用bellman—ford重新写了一遍,速度慢了将近一半,不知道是不是我写的姿势不对。
来自维基百科的bellman-ford的伪代码:
# initialization
for each v in V do
d[v] ← ∞;
d[source] ← 0
# Relaxation
for i =1,...,|V|-1 do
for each edge (u,v) in E do
d[v] ← min{d[v], d[u]+w(u,v)}
# Negative cycle checking
for each edge (u, v) in E do
if d[v]> d[u] + w(u,v) then
no solution
其实就是普通bellman-ford做完之后,再检查所有的边一次,看看能不能继续松弛,如果可以的话,松弛变数会超过:|V|-1,说明必须存在负权环。
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <vector>
#define N 110
using namespace std; struct edgeType
{
int v, w;
edgeType(int a, int b):v(a), w(b) {}
};
int n, m, d[N], inq[N];
char op[];
vector<edgeType> g[N]; bool bellman_ford(void)
{
for(int k = ; k < n; k++)
for(int x = ; x <= n; x++)
for(int i = ; i < (int)g[x].size(); i++)
if(d[g[x][i].v] > d[x] + g[x][i].w)
d[g[x][i].v] = d[x] + g[x][i].w;
for(int i = ; i <= n; i++)
{
for(int k = ; k < (int)g[i].size(); k++)
if(d[g[i][k].v] > d[i] + g[i][k].w)
return false;
}
return true;
} int main(void)
{
int a, b, c;
while(scanf("%d",&n)&&n)
{
n++;
scanf("%d", &m);
memset(d, 0x0f, sizeof(d));
memset(inq, , sizeof(inq));
d[n] = ;
g[n].clear();
for(int i = ; i < n; i++)
g[n].push_back(edgeType(i, )), g[i].clear();
for(int i = ; i < m; i++)
{
scanf("%d%d%s%d", &a, &b, op, &c);
if(op[] == 'g')
g[a + b].push_back(edgeType(a - , -c - ));
else g[a - ].push_back(edgeType(a + b, c - ));
}
if(bellman_ford())
puts("lamentable kingdom");
else puts("successful conspiracy");
}
return ;
}
UVA 515 King的更多相关文章
- POJ 1364 King (UVA 515) 差分约束
http://poj.org/problem?id=1364 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...
- UVA 515 差分约束 SPFA判负
第一次看这个题目,完全不知道怎么做,看起来又像是可以建个图进行搜索,但题目条件就给了你几个不等式,这是怎么个做法...之后google了下才知道还有个差分约束这样的东西,能够把不等式化成图,要求某个点 ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- UVa 109 - SCUD Busters(凸包计算)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- UVA它11292 - Dragon of Loowater
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- UVA 11292 Dragon of Loowater(简单贪心)
Problem C: The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance tur ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
- cogs 1405. 中古世界的恶龙[The Drangon of Loowater,UVa 11292]
1405. 中古世界的恶龙[The Drangon of Loowater,UVa 11292] ★ 输入文件:DragonUVa.in 输出文件:DragonUVa.out 简单对比时间 ...
- UVA1327 && POJ1904 King's Quest(tarjan+巧妙建图+强连通分量+缩点)
UVA1327 King's Quest POJ1904 King's Quest 题意: 有n个王子,每个王子都有k个喜欢的妹子,每个王子只能和喜欢的妹子结婚.现有一个匹配表,将每个王子都与一个自己 ...
随机推荐
- 基于mvc结构的前端页面框架搭建
前端开发一年了,向大家交流下自己实践总结下来的一点点开发心得.人生难免磕磕碰碰,前进的道路很多,在学习工作上我们都得学会如何让自己过的更高效,代码亦是如此. 下面,开始介绍自己总结的前端框架搭建(布局 ...
- mysql 5.5 win7安装最后一步总是停止响应
今天刚开始安装了64位版本的mysql5.5 ,安装很顺利,后来发现库不兼容的问题,于是卸载,安装mysql-5.5.27-win32的32位版本,奇怪了,怎么安装,怎么卸载都不行,就是到最 后一步停 ...
- Linux - 查看系统的版本信息
在 Linux 中,有多种方法可以查看系统的版本信息. uname 命令 huey@huey-K42JE:~$ uname -a Linux huey-K42JE 3.5.0-43-generic # ...
- make fontconfig 时出现No package ‘libxml-2.0′ found的解决方法
这里显示一个错误信息:checking for LIBXML2… configure: error: Package requirements (libxml-2.0 >= 2.6) were ...
- 第八篇、UITableView常用功能(左滑出现多个按钮,多选删除等)
1.左滑动出现多个按钮 /** * 只要实现了这个方法,左滑出现按钮的功能就有了 (一旦左滑出现了N个按钮,tableView就进入了编辑模式, tableView.editing = YES) */ ...
- 用 CSS 隐藏页面元素的 5 种方法
原文链接:用 CSS 隐藏页面元素的 5 种方法,转载请注明来源! 用 CSS 隐藏页面元素有许多种方法.你可以将 opacity 设为 0.将 visibility 设为 hidden.将 disp ...
- 折腾ghost。。。
1.启动 NODE_ENV=production node index.js 如果出现启动不了的情况,在该命令加sudo sudo NODE_ENV=production node index.js ...
- NodeJs随心学习(一)之UEditor开源项目部署
作为NodeJs的初学者+6年的码农,我觉得从基础看起太慢了,还是直接上Demo好玩一些. 嗯,直接开玩..今天在GitHub上看到了UEditor之NodeJs版的开源项目激起了我浓厚的兴趣,随后决 ...
- linux命令行下命令参数前的一横(-)和两横(--)的区别
原文转自:http://blog.csdn.net/songjinshi/article/details/6816776 在解释这些区别之前我们先了解一下有关linux的背景知识,这个需要大家先认真看 ...
- RX学习笔记:在FreeCodeCamp的学习
FreeCodeCamp https://www.freecodecamp.com 2016-07-03 前几日在Github浏览时,偶然看到一个叫FreeCodeCamp的开源项目,进去该网站之后感 ...