poj 3469 Dual Core CPU(最小割)
2016-05-20 16:14
295 查看
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
题目大意:
双核产品SWODNIW包含了N个模块,每个模块必须运行在某个CPU中,每个模块在每个CPU中运行的耗费已经被估算出来了,设为Ai和Bi。同时,M对模块之间需要共享数据,如果他们运行在同一个CPU中,共享数据可以忽略不计,否则,还需要额外的费用。你必须很好的安排这N个模块,使得总耗费最小。
建图:
我们可以把两个CPU分别视为源点和汇点,把模块视为顶点。从源点向顶点i连线容量为Ai,从顶点i向汇点连线容量为Bi。对于a模块与b模块在不同CPU中运行造成的额外耗费w,从顶点a向顶点b连线容量为w,从顶点b向顶点a连线容量为w,(有向图),此时每个模块都和源点及汇点相连,及每个模块都可以在任意CPU中运行。
不难了解到对于图中的任意一个割,源点和汇点必不连通。因此每个顶点都不可能同时和汇点及源点相连,及每个模块只在同一个CPU中运行,此时耗费就为割的容量。显然,当割的容量最小,总耗费最小。故题目转化为求最小割的容量,而对最小割的容量,根据最大流最小割定理,可以根据最大流流量来求解。
通过Dinic算法求解网络最大流。
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
题目大意:
双核产品SWODNIW包含了N个模块,每个模块必须运行在某个CPU中,每个模块在每个CPU中运行的耗费已经被估算出来了,设为Ai和Bi。同时,M对模块之间需要共享数据,如果他们运行在同一个CPU中,共享数据可以忽略不计,否则,还需要额外的费用。你必须很好的安排这N个模块,使得总耗费最小。
建图:
我们可以把两个CPU分别视为源点和汇点,把模块视为顶点。从源点向顶点i连线容量为Ai,从顶点i向汇点连线容量为Bi。对于a模块与b模块在不同CPU中运行造成的额外耗费w,从顶点a向顶点b连线容量为w,从顶点b向顶点a连线容量为w,(有向图),此时每个模块都和源点及汇点相连,及每个模块都可以在任意CPU中运行。
不难了解到对于图中的任意一个割,源点和汇点必不连通。因此每个顶点都不可能同时和汇点及源点相连,及每个模块只在同一个CPU中运行,此时耗费就为割的容量。显然,当割的容量最小,总耗费最小。故题目转化为求最小割的容量,而对最小割的容量,根据最大流最小割定理,可以根据最大流流量来求解。
通过Dinic算法求解网络最大流。
#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <vector> #include <queue> #define maxn 200000 #define INF 0x3f3f3f3f using namespace std; 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; vector<int>G[maxn]; bool vis[maxn]; int d[maxn]; int cur[maxn]; void Init(int n) { this->n = n; for(int i = 0; i < n; i ++) G[i].clear(); edges.clear(); } void Addedge(int from,int to,int cap) { edges.push_back(Edge(from,to,cap,0)); edges.push_back(Edge(to,from,0,0));///反向弧 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) { if(x == t || a == 0) return a; int flow = 0,f; for(int& i = cur[x]; i < G[x].size(); i ++) { Edge& e = edges[G[x][i]]; 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 == 0) break; } } return flow; } int Maxflow(int s, int t) { this->s = s; this->t = t; int flow = 0; while(BFS()) { memset(cur, 0, sizeof(cur)); flow += DFS(s, INF); } return flow; } }; Dinic g; int main() { int n,m,x,y,z; while(~scanf("%d%d",&n,&m)) { g.Init(n+1); int s=0,t=n+1; for(int i = 1; i <=n; i ++) { scanf("%d%d",&x,&y); g.Addedge(s,i,x); g.Addedge(i,t,y); } for(int i=0; i<m; i++) { scanf("%d%d%d",&x,&y,&z); g.Addedge(x,y,z); g.Addedge(y,x,z); } printf("%d\n",g.Maxflow(s,t)); } return 0; }
相关文章推荐
- EventBus使用详解(一)——初步使用EventBus
- 一种实现C++反射功能的想法(一)
- 160520、MyBatis的几种批量操作
- 算法练习:多米诺骨牌
- 数据库中经常分组查询的表如何做性能优化(group by)
- [PHP源码阅读]explode和implode函数
- Android 中获取Apk的包名和类名的实现方法
- VOOC还真算是OPPO的核心技术
- C# 控制连接超时
- JVM生产环境参数实例及分析
- 找水王
- 100SameTree
- linux下jdk,tomcat的安装和配置
- Openv Switch 完全使用手册
- YARN体系学习笔记
- 正则表达式入门
- Linux学习---静态库 动态库
- 我就不能去睡觉.......?
- Android上下文context
- Dubbo+Zookeeper入门实例