Dual Core CPU

Time Limit: 15000MS Memory Limit: 131072K

Total Submissions: 26136 Accepted: 11270

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

由于要将nnn个点分成两组,那么这显然是“割”的意思,再仔细想想,如果我们建立源点sss和汇点ttt,从sss向每个点连容量为AiA_iAi​的边,从每个点向ttt连容量为BiB_iBi​的边,然后两个点i,ji,ji,j连容量为www的无向边,建好图后我们只用求出最小割即可。

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<string>
  5. #include<algorithm>
  6. #include<queue>
  7. #define N 250000
  8. #define M 3000000
  9. using namespace std;
  10. inline long long read(){
  11. long long ans=0;
  12. char ch=getchar();
  13. while(!isdigit(ch))ch=getchar();
  14. while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
  15. return ans;
  16. }
  17. int n,m,s,t,ans=0,d[N],first[N],cnt=-1;
  18. struct Node{int v,next,c;}e[M<<1];
  19. inline void add(int u,int v,int c){
  20. e[++cnt].v=v;
  21. e[cnt].next=first[u];
  22. e[cnt].c=c;
  23. first[u]=cnt;
  24. e[++cnt].v=u;
  25. e[cnt].next=first[v];
  26. e[cnt].c=0;
  27. first[v]=cnt;
  28. }
  29. inline bool bfs(){
  30. queue<int>q;
  31. q.push(s);
  32. memset(d,-1,sizeof(d));
  33. d[s]=0;
  34. while(!q.empty()){
  35. int x=q.front();
  36. q.pop();
  37. for(int i=first[x];i!=-1;i=e[i].next){
  38. int v=e[i].v;
  39. if(d[v]!=-1||e[i].c<=0)continue;
  40. d[v]=d[x]+1;
  41. if(v==t)return true;
  42. q.push(v);
  43. }
  44. }
  45. return false;
  46. }
  47. inline int dfs(int x,int f){
  48. if(x==t||!f)return f;
  49. int flow=f;
  50. for(int i=first[x];i!=-1;i=e[i].next){
  51. int v=e[i].v;
  52. if(flow&&e[i].c>0&&d[v]==d[x]+1){
  53. int tmp=dfs(v,min(flow,e[i].c));
  54. if(!tmp)d[v]=-1;
  55. e[i].c-=tmp;
  56. e[i^1].c+=tmp;
  57. flow-=tmp;
  58. }
  59. }
  60. return f-flow;
  61. }
  62. int main(){
  63. memset(first,-1,sizeof(first));
  64. n=read(),m=read(),s=0,t=n+1;
  65. for(int i=1;i<=n;++i){
  66. int a=read(),b=read();
  67. add(s,i,a),add(i,t,b);
  68. }
  69. for(int i=1;i<=m;++i){
  70. int a=read(),b=read(),w=read();
  71. add(a,b,w),add(b,a,w);
  72. }
  73. while(bfs())ans+=dfs(s,0x3f3f3f3f);
  74. printf("%d",ans);
  75. return 0;
  76. }

2018.06.27Dual Core CPU(最小割)的更多相关文章

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

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

  2. POJ3469 Dual Core CPU(最小割)

    题意:给你n个模块,每个模块在A核花费为ai,在B核跑花费为bi,然后由m个任务(ai,bi,wi),表示如果ai,bi不在同一个核上跑,额外的花费为wi,求最小的花费. 一开始想的时候以为是费用流, ...

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

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

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

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

  5. poj3469 Dual Core CPU——最小割

    题目:http://poj.org/problem?id=3469 最小割水题(竟然没能1A): 代码如下: #include<iostream> #include<cstdio&g ...

  6. poj 3469 Dual Core CPU 最小割

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

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

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

  8. POJ 3469 最小割 Dual Core CPU

    题意: 一个双核CPU上运行N个模块,每个模块在两个核上运行的费用分别为Ai和Bi. 同时,有M对模块需要进行数据交换,如果这两个模块不在同一个核上运行需要额外花费. 求运行N个模块的最小费用. 分析 ...

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

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

随机推荐

  1. jackson java对象和json对象的互相转换

    概述 Jackson框架是基于Java平台的一套数据处理工具,被称为“最好的Java Json解析器”. Jackson框架包含了3个核心库:streaming,databind,annotation ...

  2. SQLite4Unity3d

    What's this? When I started with Unity3d development I needed to use SQLite in my project and it was ...

  3. Codeforces Beta Round #57 (Div. 2)

    Codeforces Beta Round #57 (Div. 2) http://codeforces.com/contest/61 A #include<bits/stdc++.h> ...

  4. 24 【python入门指南】class

    一.类 1.1,构造函数,析构函数 #!/bin/python class dog(): def __init__(self, age, name): self.age = age self.name ...

  5. centos实现两种秒级任务的简单方法

    1.通过写shell脚本,死循环,守护进程运行 > vi /data/sec.sh #!/bin/bash while true do #写上自已的命令 echo "hello wor ...

  6. Hibernate: save, persist, update, merge, saveOrUpdate[z]

    [z]https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate 1. Introduction In this ...

  7. c# mac地址 和http://xx.xx.xx/ 正则表达式匹配

    Mac  :^([0-9a-fA-F]{2})(([/\s:][0-9a-fA-F]{2}){5})$ C# 书写方式 一下是允许mac中间间隔符是“:”或者“-”两种输入方式 并且我把上边的正则表达 ...

  8. dbus 消息和消息总线实例讲解-二

    转自:http://www.fmddlmyy.cn/text53.html 2.3.2.ListActivatableNames和服务器的自动启动 运行: $ dbus-send --system - ...

  9. A面&B面

    难难难.道是玄,不遇知音不可谈.遇了知音聊两句,免教那枉费舌尖.难得今天心情不错,反思毕业这五年的种种,有浑噩.迷茫.彷徨.莽撞.执着.困顿.不惧,走到今天迈过了几道坎早已忘却,同时也还在询问自己值不 ...

  10. robot framework测试驱动无法定位页面元素

    robot framework错误提示: [ WARN ] Keyword 'Capture Page Screenshot' could not be run on failure: NoSuchW ...