UVA - 11478 Halum 二分+差分约束
题目链接:
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651
题意:
给定一个有向图,每一条边都有一个权值,每次你可以选择一个节点v和一个整数d,把所有以v结尾的边权值减小d,把所有以v为起点的边的权值增加d,最后要让所有边权的最小值大于0且尽量大。
题解:
最小值最大,可以用二分,这样可以得到一个差分约束系统,然后每次都用最短路跑。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std; const int maxn = ;
const int maxm = ; struct Edge {
int v, w;
Edge(int v, int w) :v(v), w(w) {}
Edge() {}
}; vector<Edge> egs;
vector<int> G[maxn];
int tot = ;
int n, m; void addEdge(int u, int v, int w) {
egs.push_back(Edge(v, w));
tot = egs.size();
G[u].push_back(tot - );
} bool inq[maxn];
int d[maxn];
int cnt[maxn];
bool spfa(int x) {
bool ret = true; for (int i = ; i < n; i++) {
for (int j = ; j < G[i].size(); j++) {
Edge& e = egs[G[i][j]];
e.w -= x;
}
}
memset(inq, , sizeof(inq));
memset(cnt, , sizeof(cnt));
memset(d, 0x3f, sizeof(d)); queue<int> Q;
d[] = ; inq[] = true; Q.push();
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = false;
for (int i = ; i < G[u].size(); i++) {
Edge& e = egs[G[u][i]];
if (d[e.v] > d[u] + e.w) {
d[e.v] = d[u] + e.w;
if (!inq[e.v]) {
Q.push(e.v); inq[e.v] = true;
if (++cnt[e.v] > n) {
ret = false; break;
}
}
}
}
if (ret == false) break;
//printf("u:%d\nx:%d\n", u,x);
//printf("in circle\n");
} for (int i = ; i < n; i++) {
for (int j = ; j < G[i].size(); j++) {
Edge& e = egs[G[i][j]];
e.w += x;
}
} return ret;
} void init() {
for (int i = ; i < n; i++) G[i].clear();
egs.clear();
} int main() {
while (scanf("%d%d", &n, &m) == && n) {
n++;
init();
int l = , r = -;
for (int i = ; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
r = max(r, w);
addEdge(u, v, w);
}
for (int i = ; i < n; i++) {
addEdge(, i, );
}
r++;
if (!spfa()) {
printf("No Solution\n");
}
else if (spfa(r)) {
printf("Infinite\n");
}
else {
while (l + < r) {
int mid = l + (r - l) / ;
if (spfa(mid)) l = mid;
else r = mid;
//printf("here!\n");
}
printf("%d\n", l);
} }
return ;
} /*
1
2 10
1
2 -10
3
2 4
3 2
1 5
5
3 4
2 5
4 2
1 0
2 -1
*/
UVA - 11478 Halum 二分+差分约束的更多相关文章
- UVA 11478 Halum(差分约束)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34651 [思路] 差分约束系统. 设结点u上的操作和为sum[u] ...
- 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)
layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...
- UVA - 11478 - Halum(二分+差分约束系统)
Problem UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...
- UVA 11478 Halum(用bellman-ford解差分约束)
对于一个有向带权图,进行一种操作(v,d),对以点v为终点的边的权值-d,对以点v为起点的边的权值+d.现在给出一个有向带权图,为能否经过一系列的(v,d)操作使图上的每一条边的权值为正,若能,求最小 ...
- UVA 11478 Halum
Halum Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 114 ...
- POJ1275 Cashier Employment 【二分 + 差分约束】
题目链接 POJ1275 题解 显然可以差分约束 我们记\(W[i]\)为\(i\)时刻可以开始工作的人数 令\(s[i]\)为前\(i\)个时刻开始工作的人数的前缀和 每个时刻的要求\(r[i]\) ...
- UVA11478 Halum (差分约束)
每次操作是独立的,而且顺序并不影响,作用在同一个结点上的d可以叠加,所以令x(u) = sigma(dui). 最后就是要确定所有的x(u). 因为m越大,满足条件的边就越少,二分答案m. 对于一条边 ...
- UVA 11478 Halum (差分约束)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Uva 11478 Halum操作
题目链接:http://vjudge.net/contest/143318#problem/B 题意:给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权 ...
随机推荐
- Aspose插件
Eclipse安装地址: http://apps.aspose.com/marketplace/eclipse/asposewizardrepo
- 第一章 Collections 类、泛型类和Timing类概述
摘抄<数据结构与算法(C#语言描述)> 删除很多废话 1.1群集(collection)的定义 群集是一种结构化的数据类型.存储数据,并且提供数据的添.删.改操作,以及对群集不同属性值的设 ...
- C# 获取web.config配置文件内容
1.web.config提供对客户端应用程序配置文件的访问. 其有两个属性1.ConnectionStrings 获取当前应用程序默认配置的 ConnectionStringsSection 数据. ...
- PHP preg_replace() 正则替换所有符合条件的字符串示例
PHP preg_replace() 正则替换,与Javascript 正则替换不同,PHP preg_replace() 默认就是替换所有符号匹配条件的元素. 需要用程序处理的数据并不总是预先以数 ...
- HOOK API 在多线程时应该注意的问题点
在使用INLINE HOOK API实现对系统API的拦截时,正常情况下并没有太大问题,但一旦涉及到多线程,不管是修改IAT还是JMP,2种方法均会出现不可预料的问题,特别是在HOOK一些复杂的大型系 ...
- 做个伪IE浏览器?!【来自官网】
原文:docwiki.embarcadero.com/RADStudio/Seattle/en/Building_a_VCL_Forms_Web_Browser_Application 选择File ...
- SQLServer2005中查询语句的执行顺序
SQLServer2005中查询语句的执行顺序 --1.from--2.on--3.outer(join)--4.where--5.group by--6.cube|rollup--7.havin ...
- python SocketServer 源码分析
附上原文链接: http://beginman.cn/python/2015/04/06/python-SocketServer/
- WPF动画 storyboard
<Window x:Class="StoryBoard.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...
- 如何在Eclipse中配置Tomcat
1.Eclipse EE 配置Tomcat Eclipse EE 主要用于Java Web开发和J2EE项目开发.Eclipse EE中配置Tomcat比较简单,新建一个Tomcat Server即可 ...