Time Limit: 15000MS   Memory Limit: 131072K
Total Submissions: 24830   Accepted: 10756
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: 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

  1. 3 1
  2. 1 10
  3. 2 10
  4. 10 3
  5. 2 3 1000

Sample Output

  1. 13

Source

 
题意:每个问题在a,b处理器上面处理有不同的代价,有一些问题在不同的处理器上面处理要增加额外的代价,求处理完所有问题需要的最小代价。
思路:将问题分别于a,b相连,边权为代价。将它们分成两个集合,与a在一个集合的表示在a上面处理,与b在一个集合的表示在b上面处理,如果要代价最小的话,那就是最小割。但是有一些物品在不同的处理器上面处理需要额外的代价,,那就在它们之间建立边权,这样的话将它们分开就会增加代价。最小割问题即求最大流。这题的边权值较大,注意如果用一般增广路算法(即在残留网络中,每次任意寻找一条增广路)的话时间复杂度为O(FVE),与最大流有关,最短增广路算法(在层次网络中,用dfs实现多次增广,即dinic算法)的时间复杂度为O(VVE)。
代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<algorithm>
  6. #include<set>
  7. #include<map>
  8. #include<queue>
  9. #include<stack>
  10. #include<vector>
  11. using namespace std;
  12. #define PI acos(-1.0)
  13. typedef long long ll;
  14. typedef pair<int,int> P;
  15. const int maxn=1e5+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
  16. const ll INF=1e18+;
  17. priority_queue<P,vector<P>,greater<P> >que;
  18. struct edge
  19. {
  20. int from,to;
  21. int cap;
  22. int rev;///方向边的编号
  23. };
  24. vector<edge>G[maxn];///邻接表存图
  25. int iter[maxn];///当前弧,在其之前的边已经没有了作用
  26. int level[maxn];///层次
  27. void addedge(int u,int v,int c)
  28. {
  29. edge e;
  30. e.from=u,e.to=v,e.cap=c,e.rev=G[v].size();
  31. G[u].push_back(e);
  32. e.from=v,e.to=u,e.cap=,e.rev=G[u].size()-;
  33. G[v].push_back(e);
  34. }
  35. int bfs(int s,int t)
  36. {
  37. memset(level,-,sizeof(level));
  38. queue<int>q;
  39. level[s]=;
  40. q.push(s);
  41. while(!q.empty())
  42. {
  43. int u=q.front();
  44. q.pop();
  45. for(int i=; i<G[u].size(); i++)
  46. {
  47. edge e=G[u][i];
  48. if(e.cap>&&level[e.to]<)
  49. {
  50. level[e.to]=level[u]+;
  51. q.push(e.to);
  52. }
  53. }
  54. }
  55. if(level[t]<) return ;
  56. else return ;
  57. }
  58. int dfs(int u,int t,int f)
  59. {
  60. if(u==t||f==) return f;
  61. int flow=;
  62. for(int &i=iter[u]; i<G[u].size(); i++)
  63. {
  64. edge e=G[u][i];
  65. if(e.cap>&&level[u]+==level[e.to])
  66. {
  67. int d=dfs(e.to,t,min(f,e.cap));
  68. if(d>)
  69. {
  70. G[u][i].cap-=d;
  71. G[e.to][e.rev].cap+=d;
  72. flow+=d;
  73. f-=d;
  74. if(f==) break;
  75. }
  76. }
  77. }
  78. return flow;
  79. }
  80. int max_flow(int s,int t)
  81. {
  82. int flow=;
  83. while(bfs(s,t))
  84. {
  85. memset(iter,,sizeof(iter));
  86. flow+=dfs(s,t,inf);
  87. }
  88. return flow;
  89. }
  90. int main()
  91. {
  92. int n,m;
  93. scanf("%d%d",&n,&m);
  94. int s=,t=n+;
  95. for(int i=; i<=n; i++)
  96. {
  97. int a,b;
  98. scanf("%d%d",&a,&b);
  99. addedge(s,i,a);
  100. addedge(i,t,b);
  101. }
  102. for(int i=; i<=m; i++)
  103. {
  104. int a,b,w;
  105. scanf("%d%d%d",&a,&b,&w);
  106. addedge(a,b,w);
  107. addedge(b,a,w);
  108. }
  109. cout<<max_flow(s,t)<<endl;
  110. return ;
  111. }

最大流模板

POJ 3469.Dual Core CPU 最大流dinic算法模板的更多相关文章

  1. 【网络流#8】POJ 3469 Dual Core CPU 最小割【ISAP模板】 - 《挑战程序设计竞赛》例题

    [题意]有n个程序,分别在两个内核中运行,程序i在内核A上运行代价为ai,在内核B上运行的代价为bi,现在有程序间数据交换,如果两个程序在同一核上运行,则不产生额外代价,在不同核上运行则产生Cij的额 ...

  2. POJ 3469 Dual Core CPU 最大流

    划分成两个集合使费用最小,可以转成最小割,既最大流. //#pragma comment(linker, "/STACK:1024000000,1024000000") #incl ...

  3. poj 3469 Dual Core CPU【求最小割容量】

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 21453   Accepted: 9297 ...

  4. POJ 3469 Dual Core CPU Dual Core CPU

    Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 23780   Accepted: 10338 Case Time Lim ...

  5. POJ 3469 Dual Core CPU (最小割建模)

    题意 现在有n个任务,两个机器A和B,每个任务要么在A上完成,要么在B上完成,而且知道每个任务在A和B机器上完成所需要的费用.然后再给m行,每行 a,b,w三个数字.表示如果a任务和b任务不在同一个机 ...

  6. POJ 3469 Dual Core CPU(最小割)

    [题目链接] http://poj.org/problem?id=3469 [题目大意] 有N个模块要在A,B两台机器上执行,在不同机器上有不同的花费 另有M个模块组(a,b),如果a和b在同一台机子 ...

  7. poj 3469 Dual Core CPU——最小割

    题目:http://poj.org/problem?id=3469 最小割裸题. 那个限制就是在 i.j 之间连双向边. 根据本题能引出网络流中二元关系的种种. 别忘了写 if ( x==n+1 ) ...

  8. poj 3469 Dual Core CPU

    题目描述:由于越来越多的计算机配置了双核CPU,TinySoft公司的首席技术官员,SetagLilb,决定升级他们的产品-SWODNIW.SWODNIW包含了N个模块,每个模块必须运行在某个CPU中 ...

  9. poj 3469 Dual Core CPU 最小割

    题目链接 好裸的题....... 两个cpu分别作为源点和汇点, 每个cpu向元件连边, 权值为题目所给的两个值, 如果两个元件之间有关系, 就在这两个元件之间连边, 权值为消耗,这里的边应该是双向边 ...

随机推荐

  1. LinQ to sql简介及增删改查

    Linq to sql 类 LinQ它就是一个集成化的数据库访问类,它会自动生成许多原本需要我们自己创建的东西: 它和ADO.NET是一样的东西,都是为了访问数据库而出现的,EF框架 一.创建LinQ ...

  2. HTML表格与表单复习

    1.表格 <table></table> 表格 width:宽度.可以用像素或百分比表示.常用960像素. border:边框.常用值0. cellpadding:内容跟单元格 ...

  3. Winform 学生管理系统增删改查

    数据库: create database adonet go use adonet go create table xue ( code ), name ), sex bit, birth datet ...

  4. ARM9/ARM11/Cortex A8处理器(转载) .

    //toppic:推荐几个值得中小企业使用的ARM9/ARM11/Cortex A8处理器 // 作者:gooogleman //原文地址:http://blog.csdn.net/goooglema ...

  5. js中基本事件的总结,onclick、onblur、onchange等

    js中的基本事件总结: 特定的场景下发生的一个动作:事件:事件=函数(),事件发生会触发函数执行. 属性 当以下情况发生时,出现此事件 FF N IE onabort 图像加载被中断 1 3 4 on ...

  6. 径向模糊(Radial Blur)

    [径向模糊(Radial Blur)] 径向模糊,是一种从中心向外呈幅射状的逐渐模糊的效果,在图形处理软件photoshop里面也有这个模糊滤镜.而在游戏中常常用来模拟一些动感的效果,如鬼泣4中的场景 ...

  7. STO单没有取进FP,IN_SAELS_ORDER表无,但IN_PO_STO有

    描述 :业务反馈STO单没有取进FP,经检查IN_SALES_ORDER表没有此单数据,但在IN_PO_STO表却有 跟进如下: 1.检查IN_PO_STO表是否有数据 '; 2.检查SAP_SALE ...

  8. 二、Adapter 适配器

    适配器:继承适配与委托适配 需求:Banner 可以输出强电流380v.弱电流12v,但是不能被直接使用.通过别的方式,介间的使用banner? 委托类图: 代码清单: 需要隐藏的功能类: publi ...

  9. numpy--深度学习中的线代基础

    参考自 微信公众号--深度学习世界(http://mp.weixin.qq.com/s?__biz=MzI4MDMwMDM3NA==&mid=2247484616&idx=1& ...

  10. 【C++】STL算法之remove_if

    之前写过这样一段代码: auto iter=remove_if(AllEdges.begin(),AllEdges.end(),[&](Edge* edge){return _isEedge( ...