Dual Core CPU (poj 3469 最小割求解)
来源:互联网 发布:已上映的耽美网络剧 编辑:程序博客网 时间:2024/06/13 03:25
Description
As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Officer of TinySoft Corporation, decided to update their famous product - SWODNIW.
The routine consists of N modules, and each of them should run in a certain core. The costs for all the routines to execute on two cores has been estimated. Let's define them as Ai and Bi. Meanwhile, M pairs of modules need to do some data-exchange. If they are running on the same core, then the cost of this action can be ignored. Otherwise, some extra cost are needed. You should arrange wisely to minimize the total cost.
Input
There are two integers in the first line of input data, N and M (1 ≤ N ≤ 20000, 1 ≤ M ≤ 200000) .
The next N lines, each contains two integer, Ai and Bi.
In the following M lines, each contains three integers: a, b, w. The meaning is that if module a and module b don't execute on the same core, you should pay extra w dollars for the data-exchange between them.
Output
Output only one integer, the minimum total cost.
Sample Input
3 11 102 1010 32 3 1000
Sample Output
13
Source
题意:现在有n个模块,两个CPU A和B,每个模块要么在A上运行,要么在B上运行,给出每个模块在A和B机器上运行所需要的费用。接着m行,每行 a,b,w三个数字。表示如果a模块和b模块不在同一个机器上运行的话,需要额外花费w来共享数据。现在要求出运行所有任务最小的花费是多少。
思路:将两个CPU视为源点和汇点,对第i个模块在每个CPU中的耗费Ai和Bi,从源点向顶点i连接一条容量为Ai的弧,从顶点i向汇点连接一条容量为Bi的弧;对于a模块和b模块在不同CPU中运行造成的耗费w,从顶点a向b连容量为w的双向边。求最小割即求最大流。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 1005#define MAXN 20005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b) for(i = a; i <= b; i++)#define FRL(i,a,b) for(i = a; i < b; i++)#define mem(t, v) memset ((t) , v, sizeof(t))#define sf(n) scanf("%d", &n)#define sff(a,b) scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf printf#define DBG pf("Hi\n")const int MAXM = 480010;typedef long long ll;using namespace std;struct Edge{ int to,next,cap,flow;}edge[MAXM];int n,m,s,t;int tol;int head[MAXN];int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];void init(){ tol=0; memset(head,-1,sizeof(head));}//加边,单向图三个参数,双向图四个参数void addedge(int u,int v,int w,int rw=0){ edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u]; edge[tol].flow=0; head[u]=tol++; edge[tol].to=u; edge[tol].cap=rw; edge[tol].next=head[v]; edge[tol].flow=0; head[v]=tol++;}//输入参数:起点,终点,点的总数//点的编号没有影响,只要输入点的总数int sap(int start,int end,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==end) { 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 main(){ int i,j; while (~sff(n,m)) { int a,b,c; init(); s=0;t=n+1; FRE(i,1,n) { sff(a,b); addedge(s,i,a); addedge(i,t,b); } FRE(i,1,m) { sfff(a,b,c); addedge(a,b,c,c); } printf("%d\n",sap(s,t,t+1)); } return 0;}/*3 11 102 1010 32 3 1000*/
- Dual Core CPU (poj 3469 最小割求解)
- 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 最小割入门题
- UVA - 11384 Help is needed for Dexter 水题
- Reverse Words in a String II
- Bounce.js-可视化CSS3动画代码生成js插件
- 第一章
- 自适应高度
- Dual Core CPU (poj 3469 最小割求解)
- jQuery炫酷3d旋转木马特效插件
- PowerDesigner的安装及破解
- JavaScript中判断对象类型的种种方法
- 生产者和消费者 2例
- 【动态】CStatic显示图片
- 数组应用--常用排序(课时2)
- 数组应用--查找(课时3)
- 读写者问题