Description
As more and more computers are equipped with dual core CPU, SetagLilb,
the Chief Technology Officer of TinySoft Corporation, decided to
update their famous product - SWODNIW.The routine consists of N modules, and each of them should run in a
certain core. The costs for all the routines to execute on two cores
has been estimated. Let’s define them as Ai and Bi. Meanwhile, M pairs
of modules need to do some data-exchange. If they are running on the
same core, then the cost of this action can be ignored. Otherwise,
some extra cost are needed. You should arrange wisely to minimize the
total cost.Input
There are two integers in the first line of input data, N and M (1 ≤ N
≤ 20000, 1 ≤ M ≤ 200000) . The next N lines, each contains two
integer, Ai and Bi. In the following M lines, each contains three
integers: a, b, w. The meaning is that if module a and module b don’t
execute on the same core, you should pay extra w dollars for the
data-exchange between them.Output
Output only one integer, the minimum total cost.
每个点分别向源汇连
A
和
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int s=40005,t=40006,oo=0x3f3f3f3f;
int fir[40010],ne[2000010],to[2000010],w[2000010],f[40010],que[40010],n,m,tot;
void add(int u,int v,int x)
{
tot++;
ne[tot*2]=fir[u];
fir[u]=tot*2;
to[tot*2]=v;
w[tot*2]=x;
ne[tot*2+1]=fir[v];
fir[v]=tot*2+1;
to[tot*2+1]=u;
w[tot*2+1]=0;
}
bool bfs()
{
int i,u,v,hd,tl;
for (i=1;i<=n;i++) f[i]=0;
f[s]=1;
f[t]=0;
que[hd=tl=1]=s;
while (hd<=tl)
{
u=que[hd++];
for (i=fir[u];i;i=ne[i])
if (w[i]&&!f[v=to[i]])
{
f[v]=f[u]+1;
que[++tl]=v;
}
}
return f[t];
}
int dfs(int u,int lim)
{
int i,v,ret=0,x;
if (u==t) return lim;
for (i=fir[u];i&&ret<lim;i=ne[i])
if (w[i]&&f[v=to[i]]==f[u]+1)
{
x=dfs(v,min(lim-ret,w[i]));
w[i]-=x;
w[i^1]+=x;
ret+=x;
}
if (!ret) f[u]=0;
return ret;
}
int main()
{
int i,u,v,x,y,ans=0;
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
add(s,i,x);
add(i,t,y);
}
while (m--)
{
scanf("%d%d%d",&u,&v,&x);
add(u,v,x);
add(v,u,x);
}
while (bfs())
while (x=dfs(s,oo))
ans+=x;
printf("%d\n",ans);
}