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个喜欢的妹子,每个王子只能和喜欢的妹子结婚.现有一个匹配表,将每个王子都与一个自己 ...
随机推荐
- Dremel made simple with Parquet
http://lastorder.me/tag/parquet.html https://blog.twitter.com/2013/dremel-made-simple-with-parquet 对 ...
- 《Pro Git》学习笔记
1.Git远程模型示意图 Remote:远程仓库 Repository:本地仓库 Index:暂存区 workspace:当前工作区 2.取得Git仓库 2.1 初始化新仓库 git init ...
- 20160505-hibernate入门2
基本概念和CURD 开发流程 1由Domain object -> mapping->db.(官方推荐) 2由DB开始,用工具生成mapping和Domain object.(使用较多) ...
- Android 获取天气预报
界面布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...
- Javascript的作用域、作用域链以及闭包
一.javascript中的作用域 ①全局变量-函数体外部进行声明 ②局部变量-函数体内部进行声明 1)函数级作用域 javascript语言中局部变量不同于C#.Java等高级语言,在这些高级语言内 ...
- Microsoft SQL Server Product Samples:Database
从SQL Server 2005 之后示例数据都为AdventureWorks,需要的通过codeplex网站下载.这样设计的目的应该在于是生产库行不必要的用户以及权限分配. 从以下网址访问http: ...
- Java实战之02Hibernate-05检索策略、检索方式
十一.Hibernate的检索策略 1.概述: 查询的时机:什么时候去查? /** * 一张表的检索策略我们称之为: * 类级别的检索策略. * 注意:只要是说类级别的检索策略,就一定不涉及关联对象. ...
- python 安装mysql 客户端遇到的问题
一. Win7 64位编译Python扩展解决”error: Unable to find vcvarsall.bat”问题 系统上安装有Visual Studio 2010以及相应的SDK,然后 ...
- VS2010 error RC2135: file not found
VS2010 C++ win32 DLL 工程, 添加 rc 文件, 编辑 String Table. 默认情况下英文版本的 rc 文件能够顺序编译通过,为了让工程支持多语言,将字符串修改为其他语言时 ...
- iOS 分类思想(1)
1.需求:如果对一个类在不更改代码的基础上要为它再扩充额外的方法可以使用继承和分类 2.分类 作用:可以在不修改原来类代码的基础上,给某一个类扩充一些对象方法或者类方法,因此一个类可以有多个分类 实现 ...