POJ 3228-Gold Transportation(网络流_最大流+二分查找)

Gold Transportation
 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2995 Accepted: 1065

Description

Recently, a number of gold mines have been discovered in Zorroming State. To protect this treasure, we must transport this gold to the storehouses as quickly as possible. Suppose that the Zorroming State consists of N towns and there are M bidirectional roads among these towns. The gold mines are only discovered in parts of the towns, while the storehouses are also owned by parts of the towns. The storage of the gold mine and storehouse for each town is finite. The truck drivers in the Zorroming State are famous for their bad temper that they would not like to drive all the time and they need a bar and an inn available in the trip for a good rest. Therefore, your task is to minimize the maximum adjacent distance among all the possible transport routes on the condition that all the gold is safely transported to the storehouses.

Input

The input contains several test cases. For each case, the first line is integer N(1<=N<=200). The second line is N integers associated with the storage of the gold mine in every towns .The third line is also N integers associated with the storage of the storehouses in every towns .Next is integer M(0<=M<=(n-1)*n/2).Then M lines follow. Each line is three integers x y and d(1<=x,y<=N,0<d<=10000), means that there is a road between x and y for distance of d. N=0 means end of the input.

Output

For each case, output the minimum of the maximum adjacent distance on the condition that all the gold has been transported to the storehouses or "No Solution".

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int inf=0x3f3f3f3f;
int n,cnt,s,t,nv,sum;
struct node {
int u,v,cap;
int next;
} edge[1000010];

void add(int u, int v, int cap)
{
edge[cnt].v=v;
edge[cnt].cap=cap;

edge[cnt].v=u;
edge[cnt].cap=cap;
}

void bfs()
{
memset(num,0,sizeof(num));
memset(d,-1,sizeof(d));
int f1=0, f2=0, i;
q[f1++]=t;
num[0]=1;
d[t]=0;
while(f1>=f2) {
int u=q[f2++];
int v=edge[i].v;
if(d[v]!=-1) continue;
d[v]=d[u]+1;
num[d[v]]++;
q[f1++]=v;
}
}
}

int isap()
{
int flow=0, u=pre[s]=s, i;
bfs();
while(d[s]<nv) {
if(u==t) {
int f=inf, pos;
for(i=s; i!=t; i=edge[cur[i]].v) {
if(f>edge[cur[i]].cap) {
f=edge[cur[i]].cap;
pos=i;
}
}
for(i=s; i!=t; i=edge[cur[i]].v) {
edge[cur[i]].cap-=f;
edge[cur[i]^1].cap+=f;
}
flow+=f;
if(flow>=sum)
return flow;
u=pos;
}
for(i=cur[u]; i!=-1; i=edge[i].next) {
if(d[edge[i].v]+1==d[u]&&edge[i].cap)
break;
}
if(i!=-1) {
cur[u]=i;
pre[edge[i].v]=u;
u=edge[i].v;
} else {
if(--num[d[u]]==0) break;
int mind=nv;
if(mind>d[edge[i].v]&&edge[i].cap) {
mind=d[edge[i].v];
cur[u]=i;
}
}
d[u]=mind+1;
num[d[u]]++;
u=pre[u];
}
}
return flow;
}

int main()
{
int m,i,j;
int sum1;
int u,v,w;
int g[510],s1[510],mp[510][510];
while(~scanf("%d",&n)){
if(n==0) break;
sum=sum1=0;
for(i=1;i<=n;i++){
scanf("%d",&g[i]);
sum+=g[i];
}
for(i=1;i<=n;i++){
scanf("%d",&s1[i]);
sum1+=s1[i];
}
scanf("%d",&m);
memset(mp,inf,sizeof(mp));
while(m--){
scanf("%d %d %d",&u,&v,&w);
if(mp[u][v]>w)
mp[u][v]=mp[v][u]=w;
}
if(sum1<sum){
printf("No Solution\n");
continue ;
}
int low=1,high=10010,mid;
int ans=-1,x;
while(low<=high){
mid=(low+high)/2;
s=0;
t=2*n+1;
nv=t+1;
cnt=0;
for(i=1;i<=n;i++){
for(j=1;j<i;j++){
if(mp[i][j]<=mid)
}
}
x=isap();
if(x>=sum){
ans=mid;
high=mid-1;
}
else
low=mid+1;
}
if(ans!=-1)
printf("%d\n",ans);
else
printf("No Solution\n");;
}
return 0;
}


©️2019 CSDN 皮肤主题: 酷酷鲨 设计师: CSDN官方博客