Language:
Default
Dual Core CPU
Time Limit: 15000MS   Memory Limit: 131072K
Total Submissions: 19321   Accepted: 8372
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

最小割的模板,事实上就是最大流

注意:

Verson 2:

1.修复Bug。在本次模板中改动了q队列的长度。

题目。裸最小割,

设S为用模块A的集合,T为模块B

s->任务i  //模块B的cost

任务i->t  //模块A的cost

任务i->任务j //(i,j)不在一个集合的cost。注意最小割,要2个方向(最小割仅仅保证s->t没路径,t->s的路径不用割)


  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<algorithm>
  5. #include<functional>
  6. #include<iostream>
  7. #include<cmath>
  8. #include<cctype>
  9. #include<ctime>
  10. using namespace std;
  11. #define For(i,n) for(int i=1;i<=n;i++)
  12. #define Fork(i,k,n) for(int i=k;i<=n;i++)
  13. #define Rep(i,n) for(int i=0;i<n;i++)
  14. #define ForD(i,n) for(int i=n;i;i--)
  15. #define RepD(i,n) for(int i=n;i>=0;i--)
  16. #define Forp(x) for(int p=pre[x];p;p=next[p])
  17. #define Forpiter(x) for(int &p=iter[x];p;p=next[p])
  18. #define Lson (x<<1)
  19. #define Rson ((x<<1)+1)
  20. #define MEM(a) memset(a,0,sizeof(a));
  21. #define MEMI(a) memset(a,127,sizeof(a));
  22. #define MEMi(a) memset(a,128,sizeof(a));
  23. #define INF (2139062143)
  24. #define F (100000007)
  25. #define MAXn (20000+10)
  26. #define MAXm (200000+10)
  27. #define MAXN (MAXn+2)
  28. #define MAXM ((MAXn*2+MAXm*2)*2+100)
  29. long long mul(long long a,long long b){return (a*b)%F;}
  30. long long add(long long a,long long b){return (a+b)%F;}
  31. long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
  32. typedef long long ll;
  33. class Max_flow //dinic+当前弧优化
  34. {
  35. public:
  36. int n,s,t;
  37. int q[MAXN];
  38. int edge[MAXM],next[MAXM],pre[MAXN],weight[MAXM],size;
  39. void addedge(int u,int v,int w)
  40. {
  41. edge[++size]=v;
  42. weight[size]=w;
  43. next[size]=pre[u];
  44. pre[u]=size;
  45. }
  46. void addedge2(int u,int v,int w){addedge(u,v,w),addedge(v,u,0);}
  47. bool b[MAXN];
  48. int d[MAXN];
  49. bool SPFA(int s,int t)
  50. {
  51. For(i,n) d[i]=INF;
  52. MEM(b)
  53. d[q[1]=s]=0;b[s]=1;
  54. int head=1,tail=1;
  55. while (head<=tail)
  56. {
  57. int now=q[head++];
  58. Forp(now)
  59. {
  60. int &v=edge[p];
  61. if (weight[p]&&!b[v])
  62. {
  63. d[v]=d[now]+1;
  64. b[v]=1,q[++tail]=v;
  65. }
  66. }
  67. }
  68. return b[t];
  69. }
  70. int iter[MAXN];
  71. int dfs(int x,int f)
  72. {
  73. if (x==t) return f;
  74. Forpiter(x)
  75. {
  76. int v=edge[p];
  77. if (weight[p]&&d[x]<d[v])
  78. {
  79. int nowflow=dfs(v,min(weight[p],f));
  80. if (nowflow)
  81. {
  82. weight[p]-=nowflow;
  83. weight[p^1]+=nowflow;
  84. return nowflow;
  85. }
  86. }
  87. }
  88. return 0;
  89. }
  90. int max_flow(int s,int t)
  91. {
  92. int flow=0;
  93. while(SPFA(s,t))
  94. {
  95. For(i,n) iter[i]=pre[i];
  96. int f;
  97. while (f=dfs(s,INF))
  98. flow+=f;
  99. }
  100. return flow;
  101. }
  102. void mem(int n,int s,int t)
  103. {
  104. (*this).n=n;
  105. (*this).t=t;
  106. (*this).s=s;
  107.  
  108. size=1;
  109. MEM(pre)
  110. }
  111. }S;
  112.  
  113. int n,m;
  114.  
  115. int main()
  116. {
  117. // freopen("poj3469.in","r",stdin);
  118. // freopen(".out","w",stdout);
  119.  
  120. scanf("%d%d",&n,&m);
  121.  
  122. int s=1,t=n+2;
  123. S.mem(n+2,s,t);
  124. For(i,n)
  125. {
  126. int ai,bi;
  127. scanf("%d%d",&ai,&bi);
  128. S.addedge2(i+1,t,ai);
  129. S.addedge2(s,i+1,bi);
  130. }
  131.  
  132. For(i,m)
  133. {
  134. int a,b,w;
  135. scanf("%d%d%d",&a,&b,&w);
  136. S.addedge(a+1,b+1,w);
  137. S.addedge(b+1,a+1,w);
  138. }
  139.  
  140. cout<<S.max_flow(s,t)<<endl;
  141.  
  142. return 0;
  143. }

POJ 3469(Dual Core CPU-最小割)[Template:网络流dinic V2]的更多相关文章

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

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

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

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

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

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

  4. poj 3469 Dual Core CPU 最小割

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

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

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

  6. POJ 3469.Dual Core CPU 最大流dinic算法模板

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 24830   Accepted: 10756 ...

  7. POJ 3469 Dual Core CPU Dual Core CPU

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

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

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

  9. POJ - 3469 Dual Core CPU (最小割)

    (点击此处查看原题) 题意介绍 在一个由核A和核B组成的双核CPU上执行N个任务,任务i在核A上执行,花费Ai,在核B上执行,花费为Bi,而某两个任务之间可能需要进数据交互,如果两个任务在同一个核上执 ...

随机推荐

  1. 用js判断浏览器类型及设备

    <!DOCTYPE html> <html> <head> <title>JS判断是什么设备是什么浏览器</title> <meta ...

  2. window maven安装(六)

    Maven 实战系列之在Windows上安装Maven Maven是一个优秀的构建工具(类似于 Ant, 但比 Ant 更加方便使用),能帮助我们自动化构建过程,从清理.编译.测试到生成报告,再到打包 ...

  3. scale out instead of scale up

    Scale Out(也就是Scale horizontally)横向扩展,向外扩展Scale Up(也就是Scale vertically)纵向扩展,向上扩展无论是Scale Out,Scale Up ...

  4. 关于 ajax 动态返回数据 css 以及 js 失效问题(动态引入JS)

    ajax 毕竟是异步的 所以动态加载出来的数据 难免遇到 css 或者 js 失效的问题,所以要动态加载 css ji等文件了 1.公共方法 load //动态加载 js /css function ...

  5. 三读bootmem【转】

    转自:http://blog.csdn.net/lights_joy/article/details/2704788 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 11  ...

  6. 标准C程序设计七---32

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  7. 在 Ubuntu 下使用 com port, serial port

    1. Install putty 2. Insert serial-to-usb converter cable converter to NB or PC 3. check converter un ...

  8. 四、 java循环结构

    for循环结构: 格式:①初始化条件;②循环条件;③迭代条件;④循环体 for(①;②;③){ //④ } 执行过程:①-②-④-③-②-④-③-...-④-③-②,直至循环条件不满足,退出当前的循环 ...

  9. checkbox 复选框只能选中一次,之后不能用

    <td> <input type="checkbox" name="is_check" id="is_check" val ...

  10. Netty中NioEventLoopGroup的创建源码分析

    NioEventLoopGroup的无参构造: public NioEventLoopGroup() { this(0); } 调用了单参的构造: public NioEventLoopGroup(i ...