问题: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

回答:题目大意是第i个moduel在两个核的运行时间分别为ai、bi,两个modue之间的数据交换要有花费。可以将其建成一个网络流模型,源点和汇点分别是两个核,记为0,n+1,然后建图,求最小割即可。

#include<cstdio>  
    #include<cstring>  
    #include<iostream>  
    using namespace std;  
      
    const int N=20100;  
    const int M=200200;  
    const int inf=1<<29;  
    struct node  
    {  
     int v,f;  
     int next;  
    }edge[8*M];  
    int head[N],num;  
    int n,m;  
    int s,t,NN;  
      
    void init()  
    {  
     for(int i=0;i<=n+2;i++)  
      head[i]=-1;  
     num=0;  
    }  
      
    void addege(int u,int v,int f)  
    {  
     edge[num].v=v;  
     edge[num].f=f;  
     edge[num].next=head[u];  
     head[u]=num++;  
     edge[num].v=u;  
     edge[num].f=0;  
     edge[num].next=head[v];  
     head[v]=num++;  
    }  
      
    int sap()  
    {  
     int pre[N],cur[N],dis[N],gap[N];  
     int flow=0,aug=inf,u;  
     bool flag;  
     int i;  
     for(i=1;i<=NN;i++)  
     {  
      cur[i]=head[i];  
      gap[i]=dis[i]=0;  
     }  
     gap[s]=NN;  
     u=pre[s]=s;  
     while(dis[s]<NN)  
     {  
      flag=0;  
      for(int &j=cur[u];j!=-1;j=edge[j].next)  
      {  
       int v=edge[j].v;  
       if(edge[j].f>0 && dis[u]==dis[v]+1)  
       {  
        flag=1;  
        if(edge[j].f<aug) aug=edge[j].f;  
        pre[v]=u;  
        u=v;  
        if(u==t)  
        {  
         flow+=aug;  
         while(u!=s)  
         {  
          u=pre[u];  
          edge[cur[u]].f-=aug;  
          edge[cur[u]^1].f+=aug;  
         }  
         aug=inf;  
        }  
        break;  
       }  
      }  
      if(flag) continue;  
      int mindis=NN;  
      for(int j=head[u];j!=-1;j=edge[j].next)  
      {  
       int v=edge[j].v;  
       if(edge[j].f>0 && dis[v]<mindis)  
       {  
        mindis=dis[v];  
        cur[u]=j;  
       }  
      }  
      if((--gap[dis[u]])==0) break;  
      gap[dis[u]=mindis+1]++;  
      u=pre[u];  
     }  
     return flow;  
    }  
      
    int main()  
    {  
     scanf("%d%d",&n,&m);  
     int i,j;  
     int a,b,w;  
     s=0;t=n+1;NN=n+2;  
     init();  
     for(i=1;i<=n;i++)  
     {  
      scanf("%d%d",&a,&b);  
      addege(s,i,a);  
      addege(i,t,b);  
     }  
     for(i=1;i<=m;i++)  
     {  
      scanf("%d%d%d",&a,&b,&w);  
      addege(a,b,w);  
      addege(b,a,w);  
     }  
     printf("%d/n",sap());  
     return 0;  
    }

Dinic问题的更多相关文章

  1. ACM/ICPC 之 有流量上下界的网络流-Dinic(可做模板)(POJ2396)

    //有流量上下界的网络流 //Time:47Ms Memory:1788K #include<iostream> #include<cstring> #include<c ...

  2. ACM/ICPC 之 Dinic+枚举最小割点集(可做模板)(POJ1815)

    最小割的好题,可用作模板. //Dinic+枚举字典序最小的最小割点集 //Time:1032Ms Memory:1492K #include<iostream> #include< ...

  3. 网络流dinic实现总结

    太羞耻了,搞了半天居然没发现自己写的不是dinic,直到被一道时限紧的题目卡掉才发现 int dfs(int now,int flow,int sum) { if(now==n) return flo ...

  4. ACM/ICPC 之 Dinic算法(POJ2112)

    Optimal Milking //二分枚举最大距离的最小值+Floyd找到最短路+Dinic算法 //参考图论算法书,并对BFS构建层次网络算法进行改进 //Time:157Ms Memory:65 ...

  5. hihoCoder 1393 网络流三·二分图多重匹配(Dinic求二分图最大多重匹配)

    #1393 : 网络流三·二分图多重匹配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 学校的秋季运动会即将开始,为了决定参赛人员,各个班又开始忙碌起来. 小Hi和小H ...

  6. HDU 3572 Task Schedule(拆点+最大流dinic)

    Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  7. ISAP算法对 Dinic算法的改进

    ISAP算法对 Dinic算法的改进: 在刘汝佳图论的开头引言里面,就指出了,算法的本身细节优化,是比较复杂的,这些高质量的图论算法是无数优秀算法设计师的智慧结晶. 如果一时半会理解不清楚,也是正常的 ...

  8. hdu4292Food(最大流Dinic算法)

    /* 题意:每一个人都有喜欢的吃的和喝的,每一个人只选择一个数量的吃的和一个数量的喝的,问能满足最多的人数!? 思路:建图很是重要!f-food, p-people, d-drink 建图: 0(源点 ...

  9. DINIC网络流+当前弧优化

    DINIC网络流+当前弧优化 const inf=; type rec=record s,e,w,next:longint; end; var b,bb,d,q,tb:..] of longint; ...

  10. dinic模板

    procedure addedge(u,v,cap:longint); begin sid[tot].u:=u; sid[tot].v:=v; sid[tot].cap:=cap; sid[tot]. ...

随机推荐

  1. FZU 2150 Fire Game --两点同步搜索

    枚举两点,然后同步BFS,看代码吧,很容易懂的. 代码: #include <iostream> #include <cstdio> #include <cstring& ...

  2. n个整数中,找出尽可能多的数使他们组成一个等差数列,求最长等差数列的长度

    例子:  3,8,4,5,6,2          返回值应该为 :5 这是昨天做的一道优酷土豆的编程题,和leetcode中的128/ Longest Consecutive Sequence 有点 ...

  3. Win8/8.1 .NET3.5安装失败

    .NetFramework 作为开发人员,很多情况都需要.NetFramework,在Win7及之前的系统上直接双击 .NetFramework的安装包就可安装了. Win8/8.1无法安装.Net3 ...

  4. java 14-11 对象数组

    有5个学生,请把这个5个学生的信息存储到数组中,并遍历数组,获取得到每一个学生信息. 创建学生类: 学生:Student 成员变量:name,age 构造方法:无参,带参 成员方法:getXxx()/ ...

  5. java9-2形式参数

    1.形式参数: 基本类型(太简单) 引用类型 类名:(匿名对象的时候其实我们已经讲过了)需要的是该类的对象 抽象类:需要的是该抽象的类子类对象 接口:需要的是该接口的实现类对象 A. 类名:(匿名对象 ...

  6. 在windows中,如何使用cmd命令行窗口正确显示编码为utf-8格式的文字

    在windows中,如何使用cmd命令行窗口正确显示编码为utf-8格式的文字呢? 正确的步骤如下: 1, 打开cmd命令行窗口 2, 输入命令 >chcp 65001 数字65001代表的是c ...

  7. f2fs解析(八)node 管理器中的node_info

    free_info 功成身退,node_info顺利接班. // 这里还是蛮复杂的一件事,如果不搞清除的话,这个历史性的接班工作我们就接不上 上面说到 alloc_nid 和 alloc_nid_do ...

  8. 设置Eclipse中的tab键为4个空格的完整方法

    1.点击 window->preference-,依次选择 General->Editors->Text Editors,选中右侧的 insert space for tabs;如下 ...

  9. Jquery easyui tree的使用

    这个ui用的一切都是json数据.树也是如此! 后台需要返回与格式匹配的json数据才能正确加载树. 页面定义一个ui: <ul id="messageInfoAddTree" ...

  10. 从客户端中检测到有潜在危险的request.form值

    今天被这个问题卡住了,在用到CKEDITOR的时候,老是报错显示输入字符存在潜在危险,之后百度了一下,试了这两种方法: 解决方案一:    在.aspx文件头中加入这句:    <%@ Page ...