POJ 3469 Dual Core CPU 最大流

划分成两个集合使费用最小,可以转成最小割,既最大流。


//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c)
{
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c)
{
return max(max(a,b),max(a,c));
}
void debug()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in","r",stdin);
// freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch()
{
int ch;
while((ch=getchar())!=EOF)
{
if(ch!=‘ ‘&&ch!=‘\n‘)return ch;
}
return EOF;
}

struct Edge
{
int to,cap;
};
const int maxn = 20010;
vector<int> g[maxn];
vector<Edge> edge;
int n,m,s,t;
int d[maxn];
int vis[maxn];
int cur[maxn];

void add(int u,int v,int cap)
{
//printf("%d -> %d : %d\n", u, v, cap);
edge.push_back((Edge){v, cap});
g[u].push_back(edge.size() - 1);
edge.push_back((Edge){u, 0});
g[v].push_back(edge.size() - 1);
}
void build()
{
for(int i = 1; i <= n; i++)
g[i].clear();
edge.clear();
s = n + 1;
t = s + 1;
for(int i = 1; i <= n; i++)
{
int a,b;
scanf("%d%d", &a, &b);
add(s, i, a);
add(i, t, b);
}
for(int i = 1; i <= m; i++)
{
int a,b,w;
scanf("%d%d%d", &a, &b, &w);
add(a, b, w);
add(b, a, w);
}
}

bool bfs()
{
memset(vis, 0, sizeof(vis));
d[s] = 0;
queue<int> q;
q.push(s);
vis[s] = true;
while(!q.empty())
{
int x = q.front(); q.pop();
for (int i = 0; i < g[x].size(); i++)
{
Edge &e = edge[g[x][i]];
if(e.cap>0 && !vis[e.to])
{
vis[e.to] = true;
q.push(e.to);
d[e.to] = d[x] + 1;
}
}
}
return vis[t];
}

int dfs(int u,int f)
{
if(u==t || f==0) return f;
for(int &i = cur[u]; i < g[u].size(); i++)
{
Edge &e = edge[g[u][i]];
if(e.cap>0 && d[e.to] == d[u] + 1)
{
int d = dfs(e.to, min(f,e.cap));
if(d>0)
{
e.cap -= d;
edge[g[u][i]^1].cap += d;
return d;
}
}
}
return 0;
}

int max_flow()
{
int res = 0;
while(bfs())
{
memset(cur, 0, sizeof(cur));
int d;
while(d=dfs(s,INF))
{
res += d;
}
}
return res;
}
int main()
{
debug();
while(scanf("%d%d", &n, &m) != EOF)
{
build();
printf("%d\n", max_flow());
}
return 0;
}


POJ 3469 Dual Core CPU 最大流

时间: 2024-06-16 15:27:49

POJ 3469 Dual Core CPU 最大流的相关文章

POJ 3469.Dual Core CPU 最大流dinic算法模板

Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 24830   Accepted: 10756 Case Time Limit: 5000MS Description As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Officer of TinySoft C

POJ 3469 Dual Core CPU (求最小割)

POJ 3469 Dual Core CPU 链接:http://poj.org/problem?id=3469 题意:有两个cpu,n个模块.每个模块运行在连个cpu上运行时间不同.有m对模块之间要进行信息交互,如果模块在同一个cpu,那么进行信息交互时不需要额外时间,否则要花额外的时间.问怎么样分配模块,能够使得花费的时间最少. 思路:要将模块分给两个cpu,同时要使得时间最少,其实就是求最小割.那么题目可以转化为求最大流. 代码: /* ID: [email protected] PROG

POJ 3469 Dual Core CPU(最小割)

POJ 3469 Dual Core CPU 题目链接 题意:有a,b两台机器,有n个任务,在a机器和b机器需要不同时间,给定m个限制,如果u, v在不同机器需要额外开销,问最小开销 思路:最小割,源点为a机器,汇点为b机器,这样的话求出最小割,就是把点分成两个集合的最小代价,然后如果u, v在不同机器需要开销,则连u,v v,u两条边,容量为额外开销,这样如果这条边是割边,则a,b会在不同集合,并且代价就会被加上去 代码: #include <cstdio> #include <cst

POJ 3469 Dual Core CPU(网络流之最小割)

题目地址:POJ 3469 建图思路:建源点与汇点,源点与CPU1相连,汇点与CPU2相连,对共享数据的之间连无向边. 我的ISAP过这题还是毫无时间压力的嘛... 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctyp

POJ 3469 Dual Core CPU

一堆任务分配到2个不同的芯片上运行,一个任务在不同芯片上运行的时间不一样,有一些任务组如果分配到不同的芯片上运行会产生额外的时间.... 用最小的费用将不同对象划分到两个集合 , 最小割问题 . 建图: 用源点汇点代表两个芯片 对某个任务 , 在 A 芯片上运行时间 t1 . 则从源点连一条 到 这个任务容量为 t1 的边 . 对 B 芯片上运行时间同理 如果两个任务间有联系,着再俩个任务间连边. 原问题就转化成了, 将图分成两部分,所切割的容量最少. 最小割==最大流 isap 解决 Dual

POJ 3469 --Dual Core CPU【最小割】

Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 20852   Accepted: 9010 Case Time Limit: 5000MS Description As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Officer of TinySoft Co

poj 3469 Dual Core CPU 最小割

题意: 有n个模块在A和B核组成的双核计算机上运行,各个模块在A,B核上的运行时间已知,另外有m个三元组(a,b,w),表示a模块和b模块如果不在一个核上运行要产生w的额外花销,求总的最小花销. 分析: 即把n个模块划分为两个集合,可用求最小割的方法解决. 代码: //poj 3469 //sep9 #include <iostream> #include <queue> #include <algorithm> using namespace std; const i

poj 3469 Dual Core CPU——最小割

题目:http://poj.org/problem?id=3469 最小割裸题. 那个限制就是在 i.j 之间连双向边. 根据本题能引出网络流中二元关系的种种. 别忘了写 if ( x==n+1 ) return flow ; ! #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int N=2e4+5,M=2e5+

【网络流#8】POJ 3469 Dual Core CPU 最小割 - 《挑战程序设计竞赛》例题

[题意]有n个程序,分别在两个内核中运行,程序i在内核A上运行代价为ai,在内核B上运行的代价为bi,现在有程序间数据交换,如果两个程序在同一核上运行,则不产生额外代价,在不同核上运行则产生Cij的额外代价,问如何划分使得代价最小. 用最小的费用将对象划分为两个集合的问题,常常可以转换为最小割后顺利解决 建立源点与汇点,每个程序视为一个点,源点与在各个程序连一条边,最大流量为bi,汇点与各个程序连一条边,最大流量ai,对于有额外代价的程序,连一条双向边,流量为cij. 一开始用Dinic算法做,