Dual Core CPU (poj 3469 最小割求解)
2015-02-24 13:00
369 查看
Language: Default Dual Core CPU
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 1 1 10 2 10 10 3 2 3 1000 Sample Output 13 Source POJ Monthly--2007.11.25, Zhou Dong |
思路:将两个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 1 1 10 2 10 10 3 2 3 1000*/
相关文章推荐
- POJ3469 Dual Core CPU 最小割求解最小费用划分集合
- 【网络流#8】POJ 3469 Dual Core CPU 最小割【ISAP模板】 - 《挑战程序设计竞赛》例题
- 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(最小割)
- POJ 3469 Dual Core CPU (最小割建模)
- POJ3469_Dual Core CPU_最小割::分两部分
- POJ 3469 Dual Core CPU 最小割
- POJ 3469-Dual Core CPU(Dinic 最大流/最小割算法)
- POJ 3469 Dual Core CPU 最小割入门题
- POJ 3469 Dual Core CPU (最大流最小割经典题)
- poj3469 Dual Core CPU --- 最小割
- Dual Core CPU POJ - 3469 最小割(经典网络流)
- POJ-3469 Dual Core CPU 最小割最大流
- POJ 3469 --Dual Core CPU【最小割】