poj 3469 Dual Core CPU ( 最小割)
来源:互联网 发布:手机淘宝我的尺码在哪 编辑:程序博客网 时间:2024/04/30 00:17
题意:
双核CPU,n个模块,每个模块必须运行在某个CPU核心上,每个模块在cpu单核的消耗A和B,M对模块要共享数据,如果在同一个核心上不用消耗,否则需要耗费。安排N个模块,使得总耗费最小
思路:
将两个cpu核心看成源点和汇点,其他模块分别与源点汇点连线(表示每个模块可以在任意cpu上运行),m对模块分别连双向边,要使得模块只能在一个cpu上运行,就是找到一个割,源点和汇点必不联通,耗费最少就是最小割,最小割最大流原理转换成求最大流。
代码;#include <set>#include <map>#include <stack>#include <queue>#include <deque>#include <cmath>#include <vector>#include <string>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define L(i) i<<1#define R(i) i<<1|1#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-9#define maxm 500010#define maxn 701000#define MOD 1000000007#define LL long longstruct Edge{ int to,next,cap,flow;} edge[maxm];int tot=0;int head[maxn];int gap[maxn],dep[maxn],pre[maxn],cur[maxn];void init(){ tot =0; memset(head,-1,sizeof(head));}void addedge(int u,int v,int w,int rw=0){ edge[tot].to=v; edge[tot].cap = w; edge[tot].next = head[u]; edge[tot].flow = 0; head[u] = tot++; edge[tot].to = u; edge[tot].cap = rw; edge[tot].next = head[v]; edge[tot].flow = 0; head[v] = tot++;}int sap(int start,int ed,int N){ memset(gap,0,sizeof(gap)); memset(dep,0,sizeof(dep)); memcpy(cur,head,sizeof(head)); int u =start; pre[u] = -1; gap[0]=N; int ans=0; while(dep[start]< N) { if(u==ed) { int Min = INF; for(int i=pre[u]; i!=-1; i=pre[edge[i^1].to]) { if(Min > edge[i].cap - edge[i].flow) Min = edge[i].cap - edge[i].flow; } for(int i=pre[u]; i!=-1; i = pre[edge[i^1].to]) { edge[i].flow +=Min; edge[i^1].flow -=Min; } u = start; ans +=Min; continue; } bool flag = false; int v; for(int i=cur[u]; i!=-1; i = edge[i].next) { v = edge[i].to; if(edge[i].cap - edge[i].flow && dep[v] + 1==dep[u]) { flag = true; cur[u] =pre[v] =i; break; } } if(flag) { u=v; continue; } int Min =N; for(int i = head[u]; i!=-1; i=edge[i].next) if(edge[i].cap -edge[i].flow && dep[edge[i].to] <Min) { Min = dep[edge[i].to]; cur[u]= i; } gap[dep[u]]--; if(!gap[dep[u]]) return ans; dep[u] =Min +1; gap[dep[u]] ++; if(u!=start) u = edge[pre[u]^1].to; } return ans;}int T,n,m,k;int a,b,c;int main(){ int cas=1; while(scanf("%d%d",&n,&m)!=EOF) { init(); for(int i=1; i<=n; i++) { scanf("%d%d",&a,&b); addedge(0,i,a); addedge(i,n+1,b); } for(int i=0; i<m; i++) { scanf("%d%d%d",&a,&b,&c); addedge(a,b,c,c); } int ans = sap(0,n+1,n+2); printf("%d\n",ans); } 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 (求最小割)
- php数组函数
- Oracle解锁表被锁
- mybatis 一对多,多对一配置
- 数据结构实验之排序二:交换排序
- 706C. Hard problem
- poj 3469 Dual Core CPU ( 最小割)
- POJ-3026 Borg Maze (BFS+最小生成树)
- Android入门学习--之HTML
- 基于quartz的动态任务
- Spring安全权限管理(Spring Security的配置使用)
- HDU-3874 Necklace(线段树)
- JDBC预编译语句
- 牛客上的面经
- 设置MyEclipse的背景色