poj Dual Core CPU
来源:互联网 发布:网络直播收入排名 编辑:程序博客网 时间:2024/05/16 07:53
Dual Core CPU
题目:
给出由核A和核B组成的双核CPU上的运行N个模块。模块i在核A上执行的花费为Ai,在核B上执行的花费为Bi。有M个互相之间需要进行数据交换的模块组合(ai,bi),如果,这两块模块在同一个核上执行则没有额外的费用,否则会产生wi的花费。请计算执行所有模块所需的最小花费。
算法分析:
用最小的费用将对象划分成两个集合问题,常常可以转换最小割后顺利解决。
这道题目就是这类问题的一个经典例子。考虑把N个模块按照在哪个核上运行分成两个集合。
然后,就是建图问题了。因为,我们把两个核分成了两个集合,所以我们可以分别对在同一个核上运行的模块跟源或者汇连接。比如,Ai跟T连接,则Bi跟S连接,而转换核需要的花费就是对这两个模块分别连接一个花费为Wi的边就可以了。最后求解最小割就是答案了。
#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <cstdio>#include <cstring>using namespace std;const int MAXN = 20000 + 10;const int INF = 1 << 30;struct Edge{ int from,to,cap,flow; Edge(){}; Edge(int _from,int _to,int _cap,int _flow) :from(_from),to(_to),cap(_cap),flow(_flow){};};vector<Edge> edges;vector<int> G[MAXN];int cur[MAXN],d[MAXN];bool vst[MAXN];int N,M,S,T;void init(){ S = N + 1; T = N + 2; for(int i = 0;i <= T;++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)); int sz = edges.size(); G[from].push_back(sz - 2); G[to].push_back(sz - 1);}bool BFS(){ queue<int> Q; memset(vst,0,sizeof(vst)); Q.push(S); vst[S] = 1; d[S] = 0; while(!Q.empty()){ int u = Q.front(); Q.pop(); for(int i = 0;i < (int)G[u].size();++i){ Edge& e = edges[G[u][i]]; if(!vst[e.to] && e.cap > e.flow){ d[e.to] = d[u] + 1; vst[e.to] = 1; Q.push(e.to); } } } return vst[T];}int DFS(int x,int a){ if(x == T|| a == 0) return a; int flow = 0,f; for(int& i = cur[x];i < (int)G[x].size();++i){ Edge& e = edges[G[x][i]]; if(d[e.to] == d[x] + 1 && (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 flow = 0; while(BFS()){ memset(cur,0,sizeof(cur)); flow += DFS(S,INF); } return flow;}int main(){// freopen("Input.txt","r",stdin); while(~scanf("%d%d",&N,&M)){ init(); int a,b,w; for(int i = 1; i <= N;++i){ scanf("%d%d",&a,&b); addEdge(i,T,a); addEdge(S,i,b); } for(int i = 0;i < M;++i){ scanf("%d%d%d",&a,&b,&w); addEdge(a,b,w); //是双向的,注意!!!!!!!!!! addEdge(b,a,w); } printf("%d\n",maxFlow()); } return 0;}
3 0
- poj Dual Core CPU
- Dual Core CPU POJ
- POJ 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- POJ 3469 Dual Core CPU
- POJ-3469-Dual Core CPU
- POJ 3469 Dual Core CPU
- POJ 3469 Dual Core CPU
- 【Poj 3469】 Dual Core CPU
- POJ 3469 Dual Core CPU
- POJ 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- POJ 3469 Dual Core CPU
- poj 3469 Dual Core CPU
- Lua中的迭代器
- 怎样让玩家在应用内给自己的iOS app评分
- Apache Rewrite 功能
- 我要稳稳的幸福!
- 阿里云服务器 ECS Ubuntu系统安装配置
- poj Dual Core CPU
- (1)桌面客制化之单屏幕修改以及wight修改
- malloc的内存分配之 malloc(0)的内存分配情况
- 用两个for循环实现动态创建表格和实现乘法口诀表
- 3518流媒体服务器-搭建服务器
- 爱妃阿散井安科技快乐讲啊
- HDU 4565 So Easy!(矩阵快速幂)
- Replica Island 学习笔记 04 - AndouKun
- Android解耦库EventBus的使用和源码分析