poj3469 Dual Core CPU

125 篇文章 0 订阅

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 B的费用,相互之间连不在同一机器上的费用,求最小割。如果两个点在不同集合就必须还割掉相互之间的边。

#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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值