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个喜欢的妹子,每个王子只能和喜欢的妹子结婚.现有一个匹配表,将每个王子都与一个自己 ...
随机推荐
- MySQL高可用解决方案(MySQL HA Solution)
http://blog.sina.com.cn/s/blog_7e89c3f501012vtr.html 什么是高可用性?很多公司的服务都是24小时*365天不间断的.比如Call Center.这就 ...
- SQLServer 2008数据库查看死锁、堵塞的SQL语句
--每秒死锁数量 SELECT * FROM sys.dm_os_performance_counters WHERE counter_name LIKE 'Number of Deadlocks ...
- CocoaLumberjack+XcodeColor(输出带有颜色的日志)在安装过程中遇到的问题
在安装的时候遇到了各种坑,(在这里用到的pch文件的使用以及解决无法引入的问题,可以参考上午的文章) 一(XcodeColor的安装).在github上下载XcodeClolor的插件,并且安装,Xc ...
- C# 预处理指令
导读 1.什么是预处理 2.预处理的好处 3.C#中的常见预处理指令 4.总结 什么是预处理 在计算机科学中,预处理通常是指利用某一程序(通常是预处理器)对某一格式的源码(如.cs C ...
- springmvc(六)——视图和视图解析器
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAoIAAAGrCAIAAADb2WEhAAAgAElEQVR4nOzdaVhTd78vfF8/z772c9 ...
- MFC下拉框使用方法
Combo Box (组合框)控件很简单,可以节省空间.从用户角度来看,这个控件是由一个文本输入控件和一个下拉菜单组成的.用户可以从一个预先定义的列表里选择一个选项,同时也可以直接在文本框里面输入文本 ...
- ssh连接失败解决方法
执行如下命令: ssh-keygen -t dsa -P '' -f /etc/ssh/ssh_host_dsa_key ssh-keygen -t rsa -P '' -f /etc/ssh/ssh ...
- 关于解决JQuery发送Ajax请求后,IE缓存数据不更新的问题
http://www.cnblogs.com/lys_013/archive/2013/08/07/3243435.html 今天在做ajax页面无刷新请求后台服务器数据的时候,IE下遭遇Ajax缓存 ...
- javascript学习笔记3
一 判断下列数值中哪些于false相等? 0, 0.0, 0.000, -0, -0.0, 000, "0", "0.0", "0.000" ...
- MIT 2012分布式课程基础源码解析-事件管理封装
这部分的内容主要包括Epoll/select的封装,在封装好相应函数后,再使用一个类来管理相应事件,实现的文件为pollmgr.{h, cc}. 事件函数封装 可看到pollmgr.h文件下定一个了一 ...