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 ...
随机推荐
- Spring——概览
Spring是什么? Spring是帮助开发者简化开发工作的工具. Spring的出现就是为了简化人们的复杂的开发.能够在不论什么Java应用中使用,使用了主要的JavaBean取代EJB. Spri ...
- DRP——重定向与转发
重定向 重定向就是又一次进行请求.第一次请求.容器推断请求的类型,是否须要重定向. 重定向的语句是"response.SendRedirect("index.jsp"); ...
- POJ2576 Tug of War 二维背包
题目大意 一群人拔河,给出每个人的重量,要求两队人数之差不超过1人,且每队总重量之差最小. 思路 选出严格总人数一半(或+1)的人为一队,在该队重量不超过所有人总重量一半的情况下,使其重量最大. 人数 ...
- spring注入对象类型的属性
一.1.创建service类和Dao类 (1)在service中得到dao对象 2.具体实现过程 (1)在service里边把dao作为类型属性 (2)生成dao类型属性的set方法 public c ...
- Camera-hal参数调整
路径: vendor/mediatek/proproetary/custom/mt6735/hal/D1/imgsensor/对应的SENSOR目录 .../D1/flashlight/flash_t ...
- 国内物联网平台初探(八):中移物联网开放平台OneNet
平台定位 OneNET是中移物联网有限公司搭建的开放.共赢设备云平台,为各种跨平台物联网应用.行业解决方案,提供简便的云端接入.存储.计算和展现,快速打造物联网产品应用,降低开发成本. IoT Paa ...
- 杂项-DB:时序数据库
ylbtech-杂项-DB:时序数据库 1.返回顶部 1. 时序数据库全称为时间序列数据库.时间序列数据库主要用于指处理带时间标签(按照时间的顺序变化,即时间序列化)的数据,带时间标签的数据也称为时间 ...
- java+appium+安卓模拟器实现app自动化Demo
网上有比较多相关教程,自己写一遍,加深下印象. 环境搭建 据说,很多人都被繁琐的环境搭建给吓到了. 是的,确实,繁琐. node.js 网址 cmd输入node -v,出现下图说明成功. JDK 网址 ...
- 从实现HTML页面局部刷新到JSONP
也可以用iframe方法发get请求,但是目前iframe基本已经被弃用,所以此处就不介绍这个方法. 方案一:用图片造 get 请求 button.addEventListener('click', ...
- jQuery中事件模块介绍
事件模块 1.提供其他DOM方法 包括:next 和 nextAll方法 1.1 next方法实现 目标:扩展框架方法,获取当前元素的下一个元素 问题:如何获取下一个元素? 1.1.1 提供 next ...