这道题与poj2391方法完全一样
如果不会 可以先看我写的poj2391的题解:http://blog.csdn.net/frozensmile/article/details/77045408 然后再用这道题练手
79ms过
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<stack>
#include<queue>
#include<cmath>
#include<stack>
#include<list>
#include<map>
#include<set>
typedef long long ll;
using namespace std;
const int MAXN=405;//jiedian de zui da zhi
const int MAXM=45000;//bian de zui da zhi
const int INF=0x3f3f3f3f;
struct Node
{
int from,to,next;
int cap;
}edge[MAXM];
int tol;
int head[MAXN];
int dep[MAXN];
int gap[MAXN];
int sto[205];
int stoh[205];
int bian[20005][3];
void init() //remember write it in main function
{
tol=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
edge[tol].from=u;
edge[tol].to=v;
edge[tol].cap=w;
edge[tol].next=head[u];
head[u]=tol++;
edge[tol].from=v;
edge[tol].to=u;
edge[tol].cap=w;//wuxiangtu this place change to w;
edge[tol].next=head[v];
head[v]=tol++;
}
void BFS(int start,int end)
{
memset(dep,-1,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0]=1;
int que[MAXN];
int front,rear;
front=rear=0;
dep[end]=0;
que[rear++]=end;
while(front!=rear)
{
int u=que[front++];
if(front==MAXN)front=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(dep[v]!=-1)continue;
que[rear++]=v;
if(rear==MAXN)rear=0;
dep[v]=dep[u]+1;
++gap[dep[v]];
}
}
}
int SAP(int start,int end,int n) //n shi jiedian de zui da ge shu ,including source and sink
{
int res=0;
BFS(start,end);
int cur[MAXN];
int S[MAXN];
int top=0;
memcpy(cur,head,sizeof(head));
int u=start;
int i;
while(dep[start]<n)
{
if(u==end)
{
int temp=INF;
int inser;
for(i=0;i<top;i++)
if(temp>edge[S[i]].cap)
{
temp=edge[S[i]].cap;
inser=i;
}
for(i=0;i<top;i++)
{
edge[S[i]].cap-=temp;
edge[S[i]^1].cap+=temp;
}
res+=temp;
top=inser;
u=edge[S[top]].from;
}
if(u!=end&&gap[dep[u]-1]==0)
break;
for(i=cur[u];i!=-1;i=edge[i].next)
if(edge[i].cap!=0&&dep[u]==dep[edge[i].to]+1)
break;
if(i!=-1)
{
cur[u]=i;
S[top++]=i;
u=edge[i].to;
}
else
{
int min=n;
for(i=head[u];i!=-1;i=edge[i].next)
{
if(edge[i].cap==0)continue;
if(min>dep[edge[i].to])
{
min=dep[edge[i].to];
cur[u]=i;
}
}
--gap[dep[u]];
dep[u]=min+1;
++gap[dep[u]];
if(u!=start)u=edge[S[--top]].from;
}
}
return res;
}
void getmap(int mid,int m,int n)
{
init();
int i;
for(i=1;i<=n;i++)
{
addedge(0, i, sto[i]);
addedge(i+n, 2*n+1, stoh[i]);
addedge(i, i+n, INF);
}
for(i=1;i<=m;i++)
{
if(bian[i][2]<=mid)
{
addedge(bian[i][0], bian[i][1]+n, INF);
}
}
}
int main()
{
int i,j,k,n,m;
while(scanf("%d",&n)==1)
{
if(n==0)break;
int sum=0;
for(i=1;i<=n;i++)
{
scanf("%d",&sto[i]);//i点现在有多少矿物
sum+=sto[i];//二分时求得的最大流必须大于等于sum才符合题意
}
for(i=1;i<=n;i++)
{
scanf("%d",&stoh[i]);//i点的最大存储量
}
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&bian[i][0],&bian[i][1],&bian[i][2]);//记录边
}
int l=0,r=0x3f3f3f3f,mid,ans=-1;
while(l<=r)
{
mid=(l+r)/2;
getmap(mid,m,n);
if(SAP(0,2*n+1,2*n+2)>=sum)
{
ans=mid;
r=mid-1;
}
else
{
l=mid+1;
}
}
if(ans==-1)printf("No Solution\n");
else printf("%d\n",ans);
}
return 0;
}