poj 3228 Gold Transportation 二分+网络流
给出n个城市, 每个城市有一个仓库, 仓库有容量限制, 同时每个城市也有一些货物, 货物必须放到仓库中。 城市之间有路相连, 每条路有长度。 因为有些城市的货物量大于仓库的容量, 所以要运到别的城市,求所有货物都放到仓库中时, 走过的路中, 最长的那条路最短的情况, 输出这条路的长度。
很容易想到二分, 如果城市之间路的长度小于二分值x, 那么两个城市之间连边, 权值为inf。 源点和所有城市连边, 权值为一开始的货物量, 每个城市和这个城市的仓库连边, 权值inf, 每个仓库和汇点连边, 权值为仓库的容量。 如果最大流的结果不等于一开始所有城市货物量的和, 那么这种情况不满足, 需要加大x, 反之减小x。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int maxn = 4e5+;
int q[maxn*], head[maxn*], dis[maxn/], s, t, num, m, n;
int a[], b[];
vector <pll> v[];
struct node
{
int to, nextt, c;
node(){}
node(int to, int nextt, int c):to(to), nextt(nextt), c(c){}
}e[maxn*];
void init() {
num = ;
mem1(head);
}
void add(int u, int v, int c) {
e[num] = node(v, head[u], c); head[u] = num++;
e[num] = node(u, head[v], ); head[v] = num++;
}
int bfs() {
mem(dis);
dis[s] = ;
int st = , ed = ;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!dis[v]&&e[i].c) {
dis[v] = dis[u]+;
if(v == t)
return ;
q[ed++] = v;
}
}
}
return ;
}
int dfs(int u, int limit) {
if(u == t) {
return limit;
}
int cost = ;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[v] == dis[u]+) {
int tmp = dfs(v, min(limit-cost, e[i].c));
if(tmp>) {
e[i].c -= tmp;
e[i^].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -;
}
}
}
return cost;
} int dinic() {
int ans = ;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
} int judge(int x, int sum) {
init();
for(int i = ; i<=n; i++) {
for(int j = ; j<v[i].size(); j++) {
if(v[i][j].second<=x) {
add(i, v[i][j].first, inf);
add(v[i][j].first, i, inf);
}
}
add(s, i, a[i]);
add(i+n, t, b[i]);
add(i, i+n, inf);
}
if(dinic() == sum)
return ;
return ;
} int main()
{
while(cin>>n&&n) {
int sum = ;
for(int i = ; i<=n; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
for(int i = ; i<=n; i++) {
scanf("%d", &b[i]);
}
s = , t = *n+;
cin>>m;
int x, y, w;
for(int i = ; i<=n; i++)
v[i].clear();
for(int i = ; i<m; i++) {
scanf("%d%d%d", &x, &y, &w);
v[x].pb(mk(y, w));
}
int l = -, r = ;
while(r-l>) {
int m = l+r>>;
if(judge(m, sum))
r = m;
else
l = m;
}
if(l == ) {
puts("No Solution");
continue;
}
cout<<l+<<endl;
}
return ;
}
poj 3228 Gold Transportation 二分+网络流的更多相关文章
- POJ 3228 Gold Transportation
Gold Transportation Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Ori ...
- POJ 3228 Gold Transportation(带权并查集,好题)
参考链接:http://www.cnblogs.com/jiaohuang/archive/2010/11/13/1876418.html 题意:地图上某些点有金子,有些点有房子,还有一些带权路径,问 ...
- POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)
Secret Milking Machine Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9658 Accepted: ...
- hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)
#1389 : Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people coul ...
- poj 1797 Heavy Transportation(最大生成树)
poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...
- BZOJ_3993_[SDOI2015]星际战争_二分+网络流
BZOJ_3993_[SDOI2015]星际战争_二分+网络流 Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进 ...
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
- POJ 2516 Minimum Cost (网络流,最小费用流)
POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...
- POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)
POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...
随机推荐
- 前端制作中,IE6还有必要兼容吗?
国内市场对IE 6~7支持还有一定需求,但对于一个前端开发者,我们应该去推动这个行业向前发展,而不是一味迁就.妥协. 曾经,能够提供支持老版本 IE 是一个前端开发者的必备技能.随着移动互联网大潮来临 ...
- Python核心编程读笔 8: 文件和输入输出
第九章 文件和输入输出 一.文件内建函数.方法.属性 1 文件内建函数 file_object = open(file_name, access_mode='r', buffering=-1) 工厂函 ...
- C/C++输入输出
一. cin>>当碰到空格或换行符'\n'时,输入结束 该操作符是根据后面变量的类型读取数据. 输入结束条件 :遇到Enter.Space.Tab键. 对结束符的处理 :丢弃缓冲区中使得输 ...
- 如何使代码审查更高效【摘自InfoQ】
代码审查者在审查代码时有非常多的东西需要关注.一个团队需要明确对于自己的项目哪些点是重要的,并不断在审查中就这些点进行检查. 人工审查代码是十分昂贵的,因此尽可能地使用自动化方式进行审查,如:代码 ...
- 数字时钟DigClock
首先建立数字显示类: using System; using System.Drawing; namespace CsDev { class SevenSegmentDispay { Graphics ...
- hadoop笔记之Hive的数据存储(内部表)
Hive的数据存储(内部表) Hive的数据存储(内部表) 基于HDFS 可使用hadoop给我们提供的web管理工具查看数据.打开管理工具localhost:9000–>Utilities下的 ...
- javascript 动态创建表格
<html> <head> <script> function createTable(rows,lines){ this.rows=rows; this.line ...
- stackoverflow收藏
Make a video using several .png images http://stackoverflow.com/q/13590976/5624248 Specifying and sa ...
- 概率法求解三阶幻方[C语言]
#include <stdio.h> #include <string.h> ]={,,,,,,,,}; ]; ][]; int sum(int su[]) { ; ;su[i ...
- 剑指offer——已知二叉树的先序和中序排列,重构二叉树
这是剑指offer中关于二叉树重构的一道题.题目原型为: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2, ...