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为终点的边的权 ...
随机推荐
- Linux下Mysql主从复制(Master-Slave)与读写分离(Amoeba)实践
一.为什么要做Mysql的主从复制(读写分离)?通俗来讲,如果对数据库的读和写都在同一个数据库服务器中操作,业务系统性能会降低.为了提升业务系统性能,优化用户体验,可以通过做主从复制(读写分离)来减轻 ...
- Knockout.Js官网学习(text绑定)
前言 text 绑定到DOM元素上,使得该元素显示的文本值为你绑定的参数.该绑定在显示<span>或者<em>上非常有用,但是你可以用在任何元素上. 简单绑定 Today's ...
- 多屏判断css改写
function replaceBodyClass(){ for(var i in map){ if(map[i](width)){ document.body.className = documen ...
- zabbix介绍
zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix组件主要分两个: zabbix-server和zabbix-agent.支持的监控协议有ICM ...
- 获取枚举的title
public class StringValue : System.Attribute { private readonly string _value; public StringValue(str ...
- Tostring记录,方便自己查看
C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...
- 在CentOS6.5下安装Memcached
CentOS 6.5 安装软件非常方便, yum install memcached
- delphi 博客地址收藏
博客地址: Delphi XE5 for Android系列,值得入门者一读http://www.cnblogs.com/ChinaEHR/p/3346867.html http://hi.baidu ...
- python之ftplib库
检测ftp是否可用 #!/usr/bin/python #coding:utf-8 from ftplib import FTP def ftp_open(ip,user,passwd): try: ...
- LaTeX让公式跟随章节编号
正常公式编号会是(1)(2)这种,想要编号成(3.1) (3.2)怎么办呢? \usepackage{amsmath} \numberwithin{equation}{section} 在导言区加入以 ...