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个喜欢的妹子,每个王子只能和喜欢的妹子结婚.现有一个匹配表,将每个王子都与一个自己 ...
随机推荐
- 理解JavaScript原型式继承
0.基础 javascript没有类的概念, javascript不需要实例化某个具体类的实例.javascript对象本身可以用来创建对象, 而对象可以继承自其他对象, 这个概念称为原型式继承 每个 ...
- 跨域方法之CORS
跨域的方法非常之多,如果想了解其中CORS,不要浪费时间,看下面三个就够了 了解CORS https://developer.mozilla.org/en-US/docs/Web/HTTP/Acc ...
- 【转】The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?...
[转]The content of element type "configuration" must match "(properties?,settings?,typ ...
- oracle的sid
SID是一个数据库的唯一标识符!是你在建立一个数据库时系统自动赋予的一个初始ID,虽说他和数据库名(DB_NAME)都是一个数据库的唯一标识符,但是在作用上就有不小区别.SID主要用于在一些DBA操作 ...
- redhat ping不通外网的解决办法
ping自己和网关都能ping通就是无法ping通外网例如百度: [root@ocdp1 ~]# ping www.baidu.com ping: unknown host www.baidu.com ...
- smarty半小时快速上手入门教程
http://www.jb51.net/article/56754.htm http://www.yiibai.com/smarty/smarty_functions.html http://www. ...
- Ext.Net学习笔记16:Ext.Net GridPanel 折叠/展开行
Ext.Net学习笔记16:Ext.Net GridPanel 折叠/展开行 Ext.Net GridPanel的行支持折叠/展开功能,这个功能个人觉得还说很有用处的,尤其是数据中包含图片等内容的时候 ...
- 03_线程的创建和启动_实现Runnable接口方式
[线程的创建和启动的步骤(实现Runnable接口方式)] 1.定义Runnable接口的实现类,并重写其中的run方法.run()方法的方法体是线程执行体. class SonThread imp ...
- [转载]《STL源码剖析》阅读笔记之 迭代器及traits编程技法
本文从三方面总结迭代器 迭代器的思想 迭代器相应型别及traits思想 __type_traits思想 一 迭代器思想 迭代器的主要思想源于迭代器模式,其定义如下:提供一种方法,使之能够依 ...
- 基本上,把switch,用设计模式代替,肯定是bug和过度设计。想想,本来修改一个文件几行代码可以解决的问题,变成修改3-6个类才能实现一样的功能。不是傻是什么?
那些迷信设计模式的人,来修改一下这个方法吧.看看你最终的代码膨胀为几倍... public virtual PasswordChangeResult ChangePassword(ChangePass ...