POJ 3228 Gold Transportation
Gold Transportation
This problem will be judged on PKU. Original ID: 3228
64-bit integer IO format: %lld Java class name: Main
Input
Output
Sample Input
4
3 2 0 0
0 0 3 3
6
1 2 4
1 3 10
1 4 12
2 3 6
2 4 8
3 4 5
0
Sample Output
6
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc {
int to,flow,next;
arc(int x = ,int y = ,int z = -) {
to = x;
flow = y;
next = z;
}
};
arc e[maxn*maxn];
int head[maxn],d[maxn],gold[maxn],store[maxn];
int tot,n,m,S,T,cur[maxn],q[maxn],hd,tl;
int a[maxn*maxn],b[maxn*maxn],c[maxn*maxn];
void add(int u,int v,int w) {
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
void build(int mid) {
memset(head,-,sizeof(head));
tot = ;
for(int i = ; i < m; i++)
if(c[i] <= mid) {
add(a[i],b[i],INF);
add(b[i],a[i],INF);
}
for(int i = ; i <= n; i++)
add(S,i,gold[i]);
for(int i = ; i <= n; i++)
add(i,T,store[i]);
}
bool bfs() {
memset(d,-,sizeof(d));
hd = tl = ;
q[tl++] = S;
d[S] = ;
while(hd < tl) {
int u = q[hd++];
for(int i = head[u]; ~i; i = e[i].next) {
if(d[e[i].to] == - && e[i].flow > ) {
d[e[i].to] = d[u] + ;
q[tl++] = e[i].to;
}
}
}
return d[T] > -;
}
int dfs(int u,int low) {
if(u == T) return low;
int tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next) {
if(e[i].flow > && d[e[i].to] == d[u] + && (a = dfs(e[i].to,min(low,e[i].flow)))) {
tmp += a;
low -= a;
e[i].flow -= a;
e[i^].flow += a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic() {
int tmp = ;
while(bfs()) {
memcpy(cur,head,sizeof(head));
tmp += dfs(S,INF);
}
return tmp;
}
int main() {
int suma,sumb,high,low,ans;
while(scanf("%d",&n),n) {
suma = sumb = ;
for(int i = ; i <= n; i++) {
scanf("%d",gold+i);
suma += gold[i];
}
for(int i = ; i <= n; i++) {
scanf("%d",store+i);
sumb += store[i];
}
scanf("%d",&m);
low = INF;
high = -;
for(int i = ; i < m; i++) {
scanf("%d %d %d",a+i,b+i,c+i);
low = min(low,c[i]);
high = max(high,c[i]);
}
if(suma > sumb) {
puts("No Solution");
continue;
}
ans = -;
S = ;
T = n + ;
while(low <= high) {
int mid = (low + high)>>;
build(mid);
if(dinic() >= suma) {
ans = mid;
high = mid - ;
} else low = mid + ;
}
if(ans > ) printf("%d\n",ans);
else puts("No Solution");
}
return ;
}
POJ 3228 Gold Transportation的更多相关文章
- POJ 3228 Gold Transportation(带权并查集,好题)
参考链接:http://www.cnblogs.com/jiaohuang/archive/2010/11/13/1876418.html 题意:地图上某些点有金子,有些点有房子,还有一些带权路径,问 ...
- poj 3228 Gold Transportation 二分+网络流
题目链接 给出n个城市, 每个城市有一个仓库, 仓库有容量限制, 同时每个城市也有一些货物, 货物必须放到仓库中. 城市之间有路相连, 每条路有长度. 因为有些城市的货物量大于仓库的容量, 所以要运到 ...
- poj 1797 Heavy Transportation(最大生成树)
poj 1797 Heavy Transportation Description Background Hugo Heavy is happy. After the breakdown of the ...
- POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径)
POJ 1797 Heavy Transportation / SCU 1819 Heavy Transportation (图论,最短路径) Description Background Hugo ...
- POJ.1797 Heavy Transportation (Dijkstra变形)
POJ.1797 Heavy Transportation (Dijkstra变形) 题意分析 给出n个点,m条边的城市网络,其中 x y d 代表由x到y(或由y到x)的公路所能承受的最大重量为d, ...
- POJ:3228-Gold Transportation(要求最小生成树最大边最小)
Gold Transportation Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3079 Accepted: 1101 D ...
- poj 3228(二分+最大流)
题目链接:http://poj.org/problem?id=3228 思路:增设一个超级源点和一个超级汇点,源点与每一个gold相连,容量为gold数量,汇点与仓库相连,容量为仓库的容量,然后就是二 ...
- POJ 1797 Heavy Transportation (Dijkstra变形)
F - Heavy Transportation Time Limit:3000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- POJ 1797 Heavy Transportation
题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
随机推荐
- 9517 Link Link Look
9517 Link Link Look 该题有题解 时间限制:2000MS 内存限制:65535K提交次数:67 通过次数:18 题型: 编程题 语言: G++;GCC Description ...
- maven环境配置好,一直提示mvn不是内部命令
设置了环境变量 M2_HOME 跟path ,在cmd中输入mvn一直提示不是内部命令 解决办法:通过命令设置path 如下:set path=输入值
- 我的spark python 决策树实例
from numpy import array from pyspark.mllib.regression import LabeledPoint from pyspark.mllib.tree im ...
- CodeForces 660A
Description You are given an array of n elements, you must make it a co-prime array in as few moves ...
- hdoj--1151--Air Raid(最大独立集)
Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- C Tricks(十七)—— 对角线元素的屏蔽、二维数组(矩阵)的遍历
1. 对角线元素的屏蔽 使用 if + continue 实现对对角线元素的屏蔽 for u in range(n): for v in range(n): if u == v: continue . ...
- 【docker】python: can't open file 'helloworld.py': [Errno 13] Permission denied
运行容器提示权限问题 docker run -v $PWD/myapp:/usr/src/myapp -w /usr/src/myapp python:3.5 python helloworld. ...
- table合并单元格 colspan(跨列)和rowspan(跨行)
colspan和rowspan这两个属性用于创建特殊的表格. colspan是“column span(跨列)”的缩写.colspan属性用在td标签中,用来指定单元格横向跨越的列数: 在浏览器中将显 ...
- BPM使用ligerUI实现主从表显示
先看一下效果图: 界面有待美化,嘿嘿,下面说一下实现过程,当然,我的代码可能不对,就比如后台给前端返回JSON对象,应该包括状态和消息和数据,我这里直接给返回了JSON对象,所以,如果有大神,您知道怎 ...
- 在量化金融中15个最流行的Python数据分析库
Python是当今应用最广泛的编程语言之一,以其效率和代码可读性著称.作为一个科学数据的编程语言,Python介于R和java之间,前者主要集中在数据分析和可视化,而后者主要应用于大型应用.这种灵活性 ...