After days of affliction, XiaoA decides to assemble a computer himself. He has to buy n components. Two companies are finally chosen after he searches the web. They both offer all the n components but the prices may vary. There are some pairs of components such that if XiaoA buys them from different companies he has to spend an additional fee to buy adapters to avoid the collision problem.
XiaoA wants to spend as little as possible. Please tell him the minimum dollars he has to pay.
For each test case, the first line contains two integers n and m(1<=n<=500, 0<=m<=n*(n-1)/2), indicating the number of components and the number of pairs. The components are conveniently numbered from 1 to n and all pairs are distinct. The next two lines both have n integers(in the range [1, 200]), describing the price in dollar for each component they offer in order. The next m lines contain three integers i, j and c(1<=i, j<=n, i!=j, 1<=c<=200) each, indicating that if he buys component i and j from different companies, he has to spend another c dollars for an adapter.
Proceed to the end of file.
5 3 2 3 4 5 2 1 5 3 6 4 1 2 3 2 3 5 3 4 2
16
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2010;
const int M=550000;//500000 不够
const int inf=(1<<28);
int head[N];
struct Edge
{
int v,next,w;
} edge[M];
int cnt,n,s,t;//n从0开始 0->n-1
void addedge(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].v=u;
edge[cnt].w=0;
edge[cnt].next=head[v];
head[v]=cnt++;
}
int sap()
{
int pre[N],cur[N],dis[N],gap[N];
int flow=0,aug=inf,u;
bool flag;
for(int i=0; i<n; i++)
{
cur[i]=head[i];
gap[i]=dis[i]=0;
}
gap[s]=n;
u=pre[s]=s;
while(dis[s]<n)
{
flag=0;
for(int &j=cur[u]; j!=-1; j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w>0&&dis[u]==dis[v]+1)
{
flag=1;
if(edge[j].w<aug) aug=edge[j].w;
pre[v]=u;
u=v;
if(u==t)
{
flow+=aug;
while(u!=s)
{
u=pre[u];
edge[cur[u]].w-=aug;
edge[cur[u]^1].w+=aug;
}
aug=inf;
}
break;
}
}
if(flag) continue;
int mindis=n;
for(int j=head[u]; j!=-1; j=edge[j].next)
{
int v=edge[j].v;
if(edge[j].w>0&&dis[v]<mindis)
{
mindis=dis[v];
cur[u]=j;
}
}
if((--gap[dis[u]])==0)
break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return flow;
}
//初始化 cnt=0;memset(head,-1,sizeof(head));
int main()
{
int m,p;
while(scanf("%d%d",&m,&p)==2)
{
cnt=0;
memset(head,-1,sizeof(head));
n=m+2;
s=0,t=n-1;
for(int i=1;i<=m;i++)
{
int x;scanf("%d",&x);
addedge(s,i,x);
}
for(int i=1;i<=m;i++)
{
int x;scanf("%d",&x);
addedge(i,t,x);
}
while(p--)
{
int u,v,w;scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
int ans=sap();
printf("%d\n",ans);
}
return 0;
}