poj 3469 Dual Core CPU(最小割,好题)

来源:互联网 发布:python 教程 hetland 编辑:程序博客网 时间:2024/05/22 18:55

                                                                                                              题目链接

Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072KTotal Submissions: 23140 Accepted: 10072Case Time Limit: 5000MS

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: abw. 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 11 102 1010 32 3 1000

Sample Output

13

Source

POJ Monthly--2007.11.25, Zhou Dong

[Submit]   [Go Back]   [Status]   [Discuss]




题解:

这是道挑战程序设计上面的例题。p238

用最小的费用将对象划分成两个集合的问题,可以转换为最小割解决,这一题就是一个经典的例子。


#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int INF=0x3f3f3f3f;const int MAXN=20000+1000;//点数的最大值const int MAXM=200000*2+2*MAXN+1000000;//边数的最大值struct Node{    int from,to,next;    int cap;}edge[MAXM];int tol;int dep[MAXN];//dep为点的层次int head[MAXN];void init(){    tol=0;    memset(head,-1,sizeof(head));}void addedge(int u,int v,int w)//第一条边下标必须为偶数{    edge[tol].from=u;    edge[tol].to=v;    edge[tol].cap=w;    edge[tol].next=head[u];    head[u]=tol++;    edge[tol].from=v;    edge[tol].to=u;    edge[tol].cap=0;    edge[tol].next=head[v];    head[v]=tol++;}int BFS(int start,int end){    int que[MAXN];    int front,rear;    front=rear=0;    memset(dep,-1,sizeof(dep));    que[rear++]=start;    dep[start]=0;    while(front!=rear)    {        int u=que[front++];        if(front==MAXN)front=0;        for(int i=head[u];i!=-1;i=edge[i].next)        {            int v=edge[i].to;            if(edge[i].cap>0&&dep[v]==-1)            {                dep[v]=dep[u]+1;                que[rear++]=v;                if(rear>=MAXN)rear=0;                if(v==end)return 1;            }        }    }    return 0;}int dinic(int start,int end){    int res=0;    int top;    int stack[MAXN];//stack为栈,存储当前增广路    int cur[MAXN];//存储当前点的后继    while(BFS(start,end))    {        memcpy(cur,head,sizeof(head));        int u=start;        top=0;        while(1)        {            if(u==end)            {                int min=INF;                int loc;                for(int i=0;i<top;i++)                  if(min>edge[stack[i]].cap)                  {                      min=edge[stack[i]].cap;                      loc=i;                  }                for(int i=0;i<top;i++)                {                    edge[stack[i]].cap-=min;                    edge[stack[i]^1].cap+=min;                }                res+=min;                top=loc;                u=edge[stack[top]].from;            }            for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)              if(edge[i].cap!=0&&dep[u]+1==dep[edge[i].to])                 break;            if(cur[u]!=-1)            {                stack[top++]=cur[u];                u=edge[cur[u]].to;            }            else            {                if(top==0)break;                dep[u]=-1;                u=edge[stack[--top]].from;            }        }    }    return res;}int main(){int n,m;init();scanf("%d%d",&n,&m);int start=0,end=n+1;for(int i=1;i<=n;i++){int p,q;scanf("%d%d",&p,&q);addedge(start,i,p);addedge(i,end,q);}while(m--){int u,v,w;scanf("%d%d%d",&u,&v,&w);addedge(u,v,w);addedge(v,u,w);}printf("%d\n",dinic(start,end));}






















0 0
原创粉丝点击