题目链接

给出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 二分+网络流的更多相关文章

  1. POJ 3228 Gold Transportation

    Gold Transportation Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Ori ...

  2. POJ 3228 Gold Transportation(带权并查集,好题)

    参考链接:http://www.cnblogs.com/jiaohuang/archive/2010/11/13/1876418.html 题意:地图上某些点有金子,有些点有房子,还有一些带权路径,问 ...

  3. POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

    Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: ...

  4. hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

    #1389 : Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people coul ...

  5. poj 1797 Heavy Transportation(最大生成树)

    poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...

  6. BZOJ_3993_[SDOI2015]星际战争_二分+网络流

    BZOJ_3993_[SDOI2015]星际战争_二分+网络流 Description 3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战.在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进 ...

  7. POJ 2135 Farm Tour (网络流,最小费用最大流)

    POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...

  8. POJ 2516 Minimum Cost (网络流,最小费用流)

    POJ 2516 Minimum Cost (网络流,最小费用流) Description Dearboy, a goods victualer, now comes to a big problem ...

  9. POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)

    POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...

随机推荐

  1. 关于VFP9.0备注字段(memo)插入编辑问题

    最近在做项目 用VFP9.0这个比较古老的数据库,有个问题一直纠结我很久.就是memo这个备注字段,你在insert 的时候只要插入的字符串数据超过64K的时候就会出错. 之后我一直在找原因原来是备注 ...

  2. 网上B2C书城,1.0javaWEB版!!好几天没更新了,都忙着做那个网站了~

    惯例帮师傅打个广告www.java1234.com,从基础学习java WEB! 从最初的构思,到一点点功能的实现,真是不容易啊,由于自己没有项目经验,完全依靠自己的感觉,以及自己琢磨出来的思路来写, ...

  3. TreeSet类的排序问题

     http://www.cnblogs.com/lixiaolun/archive/2012/12/25/2832775.html TreeSet支持两种排序方法:自然排序和定制排序.TreeSet默 ...

  4. cocos2dx3.3在Windows环境搭建以及新工程创建

    这个虽然比较简单,但是是学习cocos的第一步,不积跬步无以至千里,所以今天先分享Windows下环境搭建问题.关于mac搭建后续有机会在写(ps:因为我暂时没有mac),anyway,开始吧! 首先 ...

  5. Atom power-mode

    最近看到很多大牛都在用这个酷炫狂拽掉渣天的插件,于是就默默地git了一波.实际结果就是机械键盘加上砰砰砰砰,根本停不下来有没有. 建议(ˉ(∞)ˉ) :视力2.0的同学不建议使用,眼瞎的同学用就用吧, ...

  6. You have not agreed to the Xcode license.

    You have not agreed to the Xcode license. Before running the installer again please agree to the lic ...

  7. PHP fopen和fwrite函数实现创建html页面

    思路 用fopen函数和fread函数得到模板,然后用str_replace函数替换模板标签为变量,最后用fwrite函数输出新的HTML页面 index.html模板页面 <!DOCTYPE ...

  8. java通过JNI接口调用C语言-初级

    JNI(java native interface):即java本地调用C的接口. 先看整体运行: 下面是过程: #vim test.java public class test{ public na ...

  9. OSG显示文字——自定义显示文字函数

    #include <Windows.h> #include <osg/Geode> #include <osg/Geometry> #include <osg ...

  10. RedMine 版本管理工具

    一.介绍: Redmine 是一个开源的.基于Web的项目管理和缺陷跟踪工具.它用日历和甘特图辅助项目及进度可视化显示.同时它又支持多项目管理.Redmine是一个自由开放 源码软件解决方案,它提供集 ...