poj 3469 Dual Core CPU 最大流-最小割
来源:互联网 发布:剑灵外形数据 编辑:程序博客网 时间:2024/06/06 11:38
题目:http://poj.org/problem?id=3469
题意:有双核处理器,有n个任务,给出每个任务在分别在两个处理核心上工作的花费,然后有m行,每行给出两个任务,如果两个任务不在同一个处理核心上工作,那么将有额外的花费。求最小花费
思路:碰到的第一题求最小割-最大流的题目,思路不是自己的。将两个CPU分别视为源点和汇点、模块视为顶点,则可以按照以下方式构图:对于第i个模块在每个CPU中的耗费Ai和Bi, 从源点向顶点i连接一条容量为Ai的弧、从顶点i向汇点连接一条容量为Bi的弧;对于a模块与b模块在不同CPU中运行造成的额外耗费w,顶点a与顶点b连接一条容量为w的弧。此时每个顶点(模块)都和源点及汇点(两个CPU)相连,即每个模块都可以在任意一个CPU中运行不难了解到,对于图中的任意一个割,源点与汇点必不连通。因此每个顶点(模块)都不可能同时和源点及汇点(两个CPU)相连,即每个模块只在同一个CPU中运行。此时耗费即为割的容量。很显然,当割的容量取得最小值时,总耗费最小。故题目转化为求最小割的容量。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int N = 20010;const int INF = 0x3f3f3f3f;struct edge{ int to, cap, next;} g[N*50];int head[N], iter[N], level[N];int cnt;void add_edge(int v, int u, int cap){ g[cnt].to = u, g[cnt].cap = cap, g[cnt].next = head[v], head[v] = cnt++; g[cnt].to = v, g[cnt].cap = 0, g[cnt].next = head[u], head[u] = cnt++;}bool bfs(int s, int t){ memset(level, -1, sizeof level); queue<int> que; level[s] = 0; que.push(s); while(! que.empty()) { int v = que.front(); que.pop(); for(int i = head[v]; i != -1; i = g[i].next) { int u = g[i].to; if(g[i].cap > 0 && level[u] < 0) { level[u] = level[v] + 1; que.push(u); } } } return level[t] == -1;}int dfs(int v, int t, int f){ if(v == t) return f; for(int &i = iter[v]; i != -1; i = g[i].next) { int u = g[i].to; if(g[i].cap > 0 && level[v] < level[u]) { int d = dfs(u, t, min(f, g[i].cap)); if(d > 0) { g[i].cap -= d, g[i^1].cap += d; return d; } } } return 0;}int dinic(int s, int t){ int flow = 0, f; while(true) { if(bfs(s, t)) return flow; memcpy(iter, head, sizeof head); while(f = dfs(s, t, INF), f > 0) flow += f; }}int main(){ int n, m, a, b, c; while(~ scanf("%d%d", &n, &m)) { cnt = 0; memset(head, -1, sizeof head); for(int i = 1; i <= n; i++) { scanf("%d%d", &a, &b); add_edge(0, i, a); add_edge(i, n + 1, b); } for(int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); add_edge(a, b, c); add_edge(b, a, c); } printf("%d\n", dinic(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 <Dinic + 最小割 + 最大流>
- Poj 3469 Dual Core CPU【最小割最大流-----Dinic】
- 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 最小割
- 年终总结
- 获取Android设备的唯一识别码
- 起始
- springMvc+myBatis上传图片到mySQL数据库
- Linux Ctrl+Z的使用方法
- poj 3469 Dual Core CPU 最大流-最小割
- ①可选参数②构造函数初始化器 (构造函数的重载)③const(常量)和readonly(只读)的异同
- 项目修正
- Python中的单步调试
- 『Android Studio』使用Gradle打包、签名
- 存储过程与视图
- Jquery与Dom对象互相转换
- 操作系统——线程与进程的区别与联系?什么是线程安全?
- rsync 客户端