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 ...
随机推荐
- 查看EBS R12应用中使用CONTEXT_FILE的版本及路径
SELECT * FROM APPLSYS.FND_OAM_CONTEXT_FILES;
- MySQL管理一些基础SQL语句
.进入information_schema 数据库(存放了其他的数据库的信息) use information_schema; .查询所有数据的大小: ),),'MB') as data from i ...
- Android GreenDao with Android Studio IDE
转:http://blog.surecase.eu/using-greendao-with-android-studio-ide/ In this tutorial we will show you ...
- ORACLE同义词总结
ORACLE同义词总结 同义词概念 Oracle的同义词(synonyms) 从字面上理解就是别名的意思,和视图的功能类似,就是一种映射关系.它可以节省大量的数据库空间,对不同用户的操作同一张表没有多 ...
- J2SE知识点摘记(二十三)
我们简单介绍一下这个接口: 1.4.3 Comparable 接口 在 java.lang 包中,Comparable 接口适用于一个类有自然顺序的时候.假定对象集合是同一类型,该接口允 ...
- 快速美眉(FastMM)使用手记
今天在SourceForge下到了FastMM (Fast Memory Manager),听说比官方的内存管理快多了,试了一下,果然不错.目前最新的是4.27. 就我的使用范围来说,我就是想看看我的 ...
- HDU 5738 Eureka(极角排序)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5738 [题目大意] 给出平面中一些点,在同一直线的点可以划分为一个集合,问可以组成多少包含元素不少 ...
- tomcat oracle 连接池配置
<?xml version='1.0' encoding='utf-8'?> <Context displayName="zcgl" docBase=" ...
- OA项目之打印
打印 若此页有一个打印按钮: <input type="button" id="btnPrint" class="button_sm7" ...
- [poj 2978]Colored Stones[状态压缩DP]
题意: 给出n个石子,一共m种颜色.问最少去掉几个石子使得同种颜色全连续. 思路见注释. #include <algorithm> #include <cstdio> #inc ...