poj 3469 Dual Core CPU 最小割+邻接表

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 asAi 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 extraw dollars for the data-exchange between them.

Output

Output only one integer, the minimum total cost.

Sample Input

3 1
1 10
2 10
10 3
2 3 1000

Sample Output

13

 

//

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
const int inf=(1<<28);
using namespace std;
#define maxn 500000
struct edge
{
    int u,v,next,f,pre;
}e[2*maxn];
int num,rnum;
int head[maxn],rhead[maxn];
int d[maxn];
int numb[maxn];
int start[maxn];
int n,m;//见图后的点数(1->n)和原图边数
int p[maxn];
int source,sink;
//要初始化source 和 sink,重定义n
void Init()
{
    memset(head,-1,sizeof(head));
    memset(rhead,-1,sizeof(rhead));
    memset(p,-1,sizeof(p));
    num=0;
    return ;
}
void BFS()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        d[i]=n;
        numb[i]=0;
    }
    int Q[maxn],head(0),tail(0);
    d[sink]=0;
    numb[0]=1;
    Q[++tail]=sink;
    while(head<tail)
    {
        i=Q[++head];
        for(j=rhead[i];j!=-1;j=e[j].pre)
        {
            if(e[j].f==0||d[e[j].u]<n)
                continue;
            d[e[j].u]=d[i]+1;
            numb[d[e[j].u]]++;
            Q[++tail]=e[j].u;
        }
    }
    return ;
}
int Augment()
{
    int i;
    int tmp=inf;
    for(i=p[sink];i!=-1;i=p[e[i].u])
    {
        if(tmp>e[i].f)
            tmp=e[i].f;
    }
    for(i=p[sink];i!=-1;i=p[e[i].u])
    {
        e[i].f-=tmp;
        e[i^1].f+=tmp;
    }
    return tmp;
}
int Retreat(int &i)
{
    int tmp,j,mind(n-1);
    for(j=head[i];j!=-1;j=e[j].next)
    {
        if(e[j].f>0&&d[e[j].v]<mind)
            mind=d[e[j].v];
    }
    tmp=d[i];
    d[i]=mind+1;
    numb[tmp]--;
    numb[d[i]]++;
    if(i!=source)
        i=e[p[i]].u;
    return numb[tmp];
}
int maxflow()
{
    int flow(0),i,j;
    BFS();
    for(i=1;i<=n;i++)
        start[i]=head[i];
    i=source;
    while(d[source]<n)
    {
        for(j=start[i];j!=-1;j=e[j].next)
            if(e[j].f>0&&d[i]==d[e[j].v]+1)
                break;
        if(j!=-1)
        {
            start[i]=j;
            p[e[j].v]=j;
            i=e[j].v;
            if(i==sink)
            {
                flow+=Augment();
                i=source;
            }
        }
        else
        {
            start[i]=head[i];
            if(Retreat(i)==0)
                break;
        }
    }
    return flow;
}
//a->b=c;
void addedge(int a,int b,int c)
{
    e[num].next=head[a];
    head[a]=num;
    e[num].pre=rhead[b];
    rhead[b]=num;
    e[num].f=c;
    e[num].u=a;
    e[num++].v=b;
    e[num].next=head[b];
    head[b]=num;
    e[num].pre=rhead[a];
    rhead[a]=num;
    e[num].u=b;
    e[num].v=a;
    e[num++].f=0;
    return ;
}
int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        n+=2;
        source=1,sink=n;
        Init();
        for(int i=2;i<n;i++)
        {
            int a,b;scanf("%d%d",&a,&b);
            addedge(source,i,a);
            addedge(i,sink,b);
        }
        for(int i=1;i<=m;i++)
        {
            int u,v,w;scanf("%d%d%d",&u,&v,&w);u++,v++;
            addedge(u,v,w);//此题中要加双向边
            addedge(v,u,w);
        }
        int cnt=maxflow();
        printf("%d\n",cnt);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值