poj3469 Dual Core CPU [最小割]

in 题目 with 0 comment

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.


Sample Input

3 1
1 10
2 10
10 3
2 3 1000


Sample Output

13


Source

POJ Monthly--2007.11.25, Zhou Dong


Solution

最小割问题,可转化为最大流用dinic求解,建图方法:

  1. 指定超级源S=0,超级汇T=n+1
  2. 对于每一个节点,$ S\rightarrow i$连边,权值为ai
  3. 对于每一个节点,$ i\rightarrow T$连边,权值为bi
  4. 对于节点间有相互关系的节点 i , j ,$ i\leftrightarrow j$连边,权值为w

Code

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio> 
#define maxn 22000
#define maxe 1000010
#define INF 0x7f7f7f7f
#define DEBUG
using namespace std;
int n,m,cnt=1,T;
int head[maxn],dis[maxn];

struct edge
{
    int to,next,val;
}e[maxe];

inline int read()
{
    char ch;
    int read=0,sign=1;
    do
        ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-');
    if(ch=='-') sign=-1,ch=getchar();
    while(ch>='0'&&ch<='9')
    {
        read=read*10+ch-'0';
        ch=getchar();
    }
    return sign*read;
}

inline void ins(int u,int v,int w)
{
    cnt++;
    e[cnt].to=v;
    e[cnt].val=w;
    e[cnt].next=head[u];
    head[u]=cnt;
}

inline void insert(int u,int v,int w)
{ ins(u,v,w),ins(v,u,0); }

bool bfs()
{
    memset(dis,-1,sizeof(dis));
    queue<int> q;
    q.push(0);
    dis[0]=0;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        int i=head[u];
        while(i)
        {
            if(e[i].val && dis[e[i].to]==-1)
                q.push(e[i].to),dis[e[i].to]=dis[u]+1;
            i=e[i].next;
        }
    }
    if(dis[T]==-1) return 0;
    return 1;
}

int dfs(int x,int flow)
{
    if(x==T) return flow;
    int i=head[x],used=0,w;
    while(i)
    {
        if(e[i].val && dis[e[i].to]==dis[x]+1)
        {
            w=flow-used;
            w=dfs(e[i].to,min(w,e[i].val));
            e[i].val-=w;
            e[i^1].val+=w;
            used+=w;
            if(used==flow) return flow;
        }
        i=e[i].next;
    }
    if(!used) dis[x]=-1;
    return used;
}

int dinic()
{
    int ans=0;
    while(bfs()) ans+=dfs(0,INF);
    return ans;
}

int main()
{
    n=read(),m=read(),T=n+1;
    for(int i=1;i<=n;++i)
    {
        int a=read(),b=read();
        insert(0,i,a);
        insert(i,T,b);
    }
    for(int i=1;i<=m;++i)
    {
        int u,v,w;
        u=read(),v=read(),w=read();
        insert(u,v,w);
        insert(v,u,w);
    }
    printf("%d\n",dinic());
    return 0;
} 


扫描二维码,在手机上阅读!
Responses