(POJ3469)Dual Core CPU 网络流最小割,Dinic模板应用

                  Dual Core CPU

Time Limit: 15000MS Memory Limit: 131072K
Total Submissions: 22723 Accepted: 9893
Case 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: 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

分析:
题意:是有n个模块,每个模块必须运行在某个CPU中,一直每个模块在A,B CPU中运行的耗费。同时m对模块之间需要信息共享,如果某对模块在同一个CPU中运行则共享耗费不记,否则要额外的费用。如何安排这n个模块,使总耗费最小。


我们要解决的问题就是,将所有模块分为两堆, 堆内有一定的消耗,并且堆之间有一定的连接消耗,求如何安排使消耗最小。就是将n个节点分为两部分,求最小割问题。
我们可以按以下方式建图:将两个CPU视为源点和汇点,模块视为中间的顶点。对于第i个模块在CPU中的耗费为Ai,Bi,则从源点到i建一条容量为Ai的弧,从顶点i到汇点建一条容量为Bi的弧。a模块与b模块在不同的CPU信息共享要耗费c,则在a到b,和b到c之间建一条容量为c的弧。
不难了解到,对于图中任意一个割,源点和汇点比不联通,因此每个顶点只能和其中一个相连,即只在一个CPU中运行。此时最小的耗费就是最小割的容量。
由最大流最小割定理(最小割容量只求正向弧,所以在信息共享时建双向边),我们直接求所建的容量网络的最大流即可,使用Dinic模板。

AC代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

#define N 20010
#define INF 100000000
struct Edge
{
    int from,to,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic
{
    int n,m,s,t;//结点数,边数(包括反向弧),源点编号,汇点编号
    vector<Edge>edges;//边表,dges[e]和dges[e^1]互为反向弧
    vector<int>G[N];//邻接表,G[i][j]表示结点i的第j条边在e数组中的编号
    bool vis[N]; //BFS的使用
    int d[N]; //从起点到i的距离
    int cur[N]; //当前弧下标

    void addedge(int from,int to,int cap)
    {
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));
        int  m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool bfs()
    {
        memset(vis,0,sizeof(vis));
        queue<int>Q;
        Q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!Q.empty())
        {
            int x=Q.front();Q.pop();
            for(int i=0;i<G[x].size();i++)
            {
                Edge&e=edges[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow)//只考虑残量网络中的弧
                {
                    vis[e.to]=1;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }

        }
        return vis[t];
    }

    int dfs(int x,int a)//x表示当前结点,a表示目前为止的最小残量
    {
        if(x==t||a==0)return a;//a等于0时及时退出,此时相当于断路了
        int flow=0,f;
        for(int&i=cur[x];i<G[x].size();i++)//从上次考虑的弧开始,注意要使用引用,同时修改cur[x]
        {
            Edge&e=edges[G[x][i]];//e是一条边
            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[x][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(!a)break;//a等于0及时退出,当a!=0,说明当前节点还存在另一个曾广路分支。

            }
        }
        return flow;
    }

    int Maxflow(int s,int t)//主过程
    {
        this->s=s,this->t=t;
        int flow=0;
        while(bfs())//不停地用bfs构造分层网络,然后用dfs沿着阻塞流增广
        {
            memset(cur,0,sizeof(cur));
            flow+=dfs(s,INF);
        }
        return flow;
    }
  };

  int main()
  {
      //freopen("in.txt","r",stdin);
      int n,m;
      while(scanf("%d%d",&n,&m)!=EOF)
      {
          int a,b,c;
          Dinic dinic;
          //dinic.n=n+1;dinic.m=m;
          for(int i=1;i<=n;i++)
          {
              scanf("%d%d",&a,&b);
              dinic.addedge(0,i,a);
              dinic.addedge(i,n+1,b);
          }
          for(int i=0;i<m;i++)
          {
              scanf("%d%d%d",&a,&b,&c);
              dinic.addedge(a,b,c);
              dinic.addedge(b,a,c);
          }
          printf("%d\n",dinic.Maxflow(0,n+1));
      }
  }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值