POJ 3469 Dual Core CPU(最小割)
来源:互联网 发布:java 跨域上传文件 编辑:程序博客网 时间:2024/06/08 00:39
题目链接:点击打开链接
题意:有n个模块要在A或B上运行, 费用分别是a[i]和b[i], 还有m个关系, 如果a[i]和b[i]不在同一个CPU上执行, 那么需要额外花费c[i]。 求最小花费。
思路:首先, 很显然的是, 要把模块分成两个集合, 有一些属于A, 有一些属于B,这种将对象划分成两个集合的问题, 我们常用最小割来解决, 那么对于每个模块, 如果它属于A, 为了割断 , 要将他与汇点连一条容量为a[i]的边, 反之, 与源点连。 对于额外费用, 只要模块a[i]向模块b[i]连一条容量为w[i]的边对应起来就行了。
为了便于理解, 我们可以把样例画出图来, 那么可以发现, 最小割取决于s到t的路径中最小的流量边。
细节参见代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;typedef long double ld;const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;// & 0x7FFFFFFFconst int seed = 131;const ll INF64 = ll(1e18);const int maxn = 20000 + 10;int T,n,m;struct Edge { int from, to, cap, flow;};bool operator < (const Edge& a, const Edge& b) { return a.from < b.from || (a.from == b.from && a.to < b.to);}struct Dinic { int n, m, s, t; // 结点数, 边数(包括反向弧), 源点编号, 汇点编号 vector<Edge> edges; // 边表, edges[e]和edges[e^1]互为反向弧 vector<int> G[maxn]; // 邻接表,G[i][j]表示结点i的第j条边在e数组中的序号 bool vis[maxn]; // BFS使用 int d[maxn]; // 从起点到i的距离 int cur[maxn]; // 当前弧指针void init(int 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); vis[s] = 1; d[s] = 0; 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; }}g;int u, v, a, b, c, kase = 0;int main() { while(~scanf("%d%d",&n,&m)) { g.init(n + 1); for(int i=1;i<=n;i++) { scanf("%d%d",&a,&b); g.AddEdge(0, i, a); g.AddEdge(i, n+1, b); } for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); g.AddEdge(a, b, c); g.AddEdge(b, a, c); } printf("%d\n",g.Maxflow(0, n+1)); } return 0;}
0 0
- 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(最小割)
- POJ 3469 Dual Core CPU(最小割)
- POJ 3469 Dual Core CPU (求最小割)
- java 静态代码块 代码块 构造函数执行顺序
- 热电效应
- Hadoop-写入数据的几种方式
- 一些简单常见的html标签
- 【matlab】:matlab的linspace函数解析
- POJ 3469 Dual Core CPU(最小割)
- 大苏打
- 理解Mysql语句:insert 表名 select null 及 select last_insert_id()
- GeoServer安装和部署shp文件
- 监听键盘是否在界面上
- Oracle附加数据库
- 安卓系统Launcher的开发(转)
- iOS学习笔记——iOS组件之UIScrollView详解
- 高通平台手机开发之LCD