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".
z
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 <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int MAX_D=250+5;
const int MAX_B=45000+5;
const int INF=0x3f3f3f3f;
int g[MAX_D];
int c[MAX_D];
int p[MAX_B][3];
int tot;
int head[MAX_D];
int iter[MAX_D];
int level[MAX_D];
struct node{
int to;
int cap;
int rev;
int next;
};
node edge[2*MAX_B];
void add_edge(int a,int b,int c){
edge[tot].to=b;
edge[tot].cap=c;
edge[tot].rev=tot+1;
edge[tot].next=head[a];
head[a]=tot++;
edge[tot].to=a;
edge[tot].cap=c;
edge[tot].rev=tot-1;
edge[tot].next=head[b];
head[b]=tot++;
}
void bfs(int s){
memset(level,-1,sizeof(level));
queue<int> que;
level[s]=0;
que.push(s);
while(!que.empty()){
int v=que.front();que.pop();
for(int i=head[v];i!=0;i=edge[i].next){
node &e=edge[i];
if(e.cap>0&&level[e.to]<0){
level[e.to]=level[v]+1;
que.push(e.to);
}
}
}
}
int dfs(int v,int t,int f){
if(v==t)return f;
for(int &i=iter[v];i!=0;i=edge[i].next){
node &e = edge[i];
if(e.cap>0&&level[v]<level[e.to]){
int d=dfs(e.to,t,min(f,e.cap));
if(d>0){
e.cap-=d;
edge[e.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t){
int flow=0;
while(1){
bfs(s);
if(level[t]<0)return flow;
for(int i=0;i<MAX_D;i++){
iter[i]=head[i];
}
int f;
while((f=dfs(s,t,INF))>0){
flow+=f;
}
}
}
void solve(){
int n;
while(~scanf("%d",&n)){
if(n==0)break;
int sum=0;
for(int i=1;i<=n;i++){
scanf("%d",&g[i]);
sum+=g[i];
}
for(int i=1;i<=n;i++){
scanf("%d",&c[i]);
}
int m;
scanf("%d",&m);
int left=0,right=INF,ans=-1;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&p[i][0],&p[i][1],&p[i][2]);
}
while(left<=right){
tot=1;
memset(head,0,sizeof(head));
for(int i=1;i<=n;i++){
add_edge(1,i+2,g[i]);
add_edge(i+2,2,c[i]);
}
int mid=(left+right)>>1;
for(int i=1;i<=m;i++){
if(p[i][2]<=mid){
add_edge(p[i][0]+2,p[i][1]+2,INF);
}
}
if(max_flow(1,2)==sum){
ans=mid;
right=mid-1;
}
else{
left=mid+1;
}
}
if(ans==-1){
printf("No Solution\n");
}
else{
printf("%d\n",ans);
}
}
}
int main()
{
solve();
return 0;
}