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 ...
随机推荐
- css让文字旋转270度
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...
- Linux命令(二)——目录和文件管理命令
一.Linux系统的目录结构 1.根目录(/):顶层目录,某些系统中的唯一分区. 2./bin命令文件目录:包含Linux命令的二进制可执行文件. 3./boot目录:存放系统的内核文件和引导装载程序 ...
- hdu4849 Wow! Such City!(最短路dijkstra)
转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:pid=4849">http://acm.hdu.edu ...
- C 中 main 函数的參数
看到不同的人写出的 C 或者 C++ 程序时,可能会出现不一样的 main 函数的定义,以下的几种定义方式都是对的: int main(void) int main(int argc) i ...
- Maven—Windows操作系统中安装配置Maven环境
今天难得的周末,借此难的机会总结一下关于maven的一些操作: 1.在安装maven之前要确认计算机已经安装并配置了JDK. 2.下载maven: maven-3.0.3:http://downloa ...
- ffmpeg实现
最近做一个小项目,要在线播放录制的 MP4 视频,想开源的 flash player 或 html 5 可以播放.可,虽然 MP4 是 H.264 编码,但就是播放不了.可能是封装方式(PS 方式)不 ...
- js设计模式-桥接模式
桥接模式定义:桥梁模式的用意是"将抽象化(Abstraction)与实现化(Implementation)脱耦,使得二者可以独立地变化".这句话有三个关键词,也就是抽象化.实现化和 ...
- numpy快速指南
Quickstart tutorial 引用https://docs.scipy.org/doc/numpy-dev/user/quickstart.html Prerequisites Before ...
- POJ 3122 二分
大致题意: 就是公平地分披萨pie 我生日,买了n个pie,找来f个朋友,那么总人数共f+1人 每个pie都是高为1的圆柱体,输入这n个pie的每一个尺寸(半径),如果要公平地把pie分给每一个人(就 ...
- B - Mike and Cellphone(map)
Problem description While swimming at the beach, Mike has accidentally dropped his cellphone into th ...