POJ3469 Dual Core CPU 最小割求解最小费用划分集合
2016-04-11 23:24
375 查看
题意
两个核执行n个模块,每个模块在两个核上运行花费不同另外,m对模块如果在不同核上运行,会有额外的花费
求解最小花费的划分方式
思路
最小费用划分成两个集合,是经典的题目类型,经常会用最小割求解可以想象,我们加入一个源点s,一个汇点t,那么求完最小割,从s出可以连到的点是在一个集合,不能连到的在另一个集合,每个边的容量对应费用,那么最小割就是一种最小花费的划分方式了
对于这个题,网络中的点是模块,我们认为从s出可以连到的点所在的集合是在A上运行,其它点在B上运行。
然后我们加边,先不考虑m对的影响,由于最后s连到的点是A上运行,也就是说要去掉这些点连向t的边,因此根据最小割的定义,我们就把i -> t边的边权设为a[i];类似的,s -> i的边权设为b[i]
考虑m对的影响,由于不分在一组,才会产生花费,所以我们容易想到用去掉i , j之间的边,反映它们不在一个核上运行,然后把它们之间的边权设为w(i , j)即可。要注意,因为最后是s->i和j->t的边保留或者s->j和i->t的边保留,都会产生这个花费,所以要在i,j间加入双方向的边。
实现
#include <vector> #include <queue> #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <map> using namespace std; const int maxn = 2e4+6; const int INF = 0x3f3f3f3f; struct MaxFlow{ const static int MAX_V = maxn; int V; //终点、容量、反向边 struct edge{ int to, cap, rev; }; vector<edge> G[MAX_V]; int level[MAX_V];//顶点到源点的距离标号 int iter[MAX_V];// 当前弧,在其之前的边已经没有用了 void add_edge(int from, int to, int cap){ G[from].push_back((edge){to, cap, (int)G[to].size()}); G[to].push_back((edge){from, 0, (int)G[from].size()-1}); } // 通过BFS计算从源点出发的距离标号 void bfs(int s){ fill(level, level + V, -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()){ int v = que.front(); que.pop(); for (int i=0; i< G[v].size(); i++){ edge& e = G[v][i]; if (e.cap > 0 && level[e.to] < 0){ level[e.to] = level[v] + 1; que.push(e.to); } } } } //通过DFS寻找增广路 int dfs(int v, int t, int f){ if (v == t) return f; for (int &i = iter[v]; i < G[v].size(); i++){ edge& e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]){ int d = dfs(e.to, t, min(f, e.cap)); if (d > 0){ e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } //求解从s到t的最大流 int max_flow(int s, int t){ int flow = 0; for (;;){ bfs(s); if(level[t] < 0) return flow; fill(iter, iter + V, 0); int f; while ((f = dfs(s, t, INF)) > 0){ flow += f; } } } void init(int n = 0){ for (int i = 0; i < V; i++){ G[i].clear(); } V = n; } }mf; int n,m; int main(){ ios::sync_with_stdio(false); cin>>n>>m; mf.init(n+2); for (int i=1;i<=n;i++){ int a,b; cin>>a>>b; mf.add_edge(0,i,b); mf.add_edge(i,n+1,a); } for (int i=0;i<m;i++){ int a,b,w; cin>>a>>b>>w; mf.add_edge(a, b, w); mf.add_edge(b, a, w); } cout << mf.max_flow(0,n+1) << endl; return 0; }
相关文章推荐
- CodeForces 589D Boulevard (一维区间判断相交 非线段交做法)
- mysql事务隔离级别REPEATABLE-READ幻读
- Xarmarin.forms for vs2015 搞了一整天没能配置成功,生成老是出错,暂决定不用建Xarmarin.forms项目
- 论信息系统项目风险管理
- CSDN-markdown编辑器使用进阶篇
- poj1661 经典dp
- setAdapter和requestAsyncTask组合使用揭秘
- 你的知识资产
- RtlInitUnicodeString使用注意事项
- wordpress----如何设置段落首行自动空两格
- 用队列模拟服务台前的排队现象问题_c/c++
- mysql二次安装&中文乱码(各种编码问题)
- Nginx服务器
- 为什么你应该(从现在开始就)写博客
- vs2012调试mysql源码
- 大型网站架构不得不考虑的10个问题
- 一个模 仿探探 相册 的控件(升级了一下)
- Statistical Analysis of Large Datasets An Exploration of R - MySQL Interface
- 宽带连接错误代码651网友推荐解决方法
- 程序员必须知道的几个Git代码托管平台