POJ 3469 最小割 Dual Core CPU
题意:
一个双核CPU上运行N个模块,每个模块在两个核上运行的费用分别为Ai和Bi。
同时,有M对模块需要进行数据交换,如果这两个模块不在同一个核上运行需要额外花费。
求运行N个模块的最小费用。
分析:
这是一个集合划分问题,将这两个模块划分成两个集合,一个集合中的模块在核A上运行,一个在核B上运行。
增加一个源点S和汇点T,每个模块分别和源点和汇点连一条边,容量为在该核上运行的花费。
然后在两个模块对之间连容量为额外花费的双向边。
图中的一个割就对应一个集合的划分,最小割就是最小的总费用。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std; const int maxn = + ;
const int INF = 0x3f3f3f3f; int n, s, t;
int N, M; struct Edge
{
int from, to, cap, flow;
Edge(int u, int v, int c, int f):from(u), to(v), cap(c), flow(f) {}
}; vector<Edge> edges;
vector<int> G[maxn]; void init()
{
edges.clear();
for(int i = ; i < n; i++) G[i].clear();
} void AddEdge(int u, int v, int c)
{
edges.push_back(Edge(u, v, c, ));
edges.push_back(Edge(v, u, , ));
int m = edges.size();
G[u].push_back(m - );
G[v].push_back(m - );
} bool vis[maxn];
int d[maxn], cur[maxn]; bool BFS()
{
memset(vis, false, sizeof(vis));
vis[s] = true;
queue<int> Q;
Q.push(s);
d[s] = ; while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = ; i < G[u].size(); i++)
{
Edge& e = edges[G[u][i]];
int v = e.to;
if(!vis[v] && e.cap > e.flow)
{
vis[v] = true;
d[v] = d[u] + ;
Q.push(v);
}
}
} return vis[t];
} int DFS(int u, int a)
{
if(u == t || a == ) return a;
int flow = , f;
for(int& i = cur[u]; i < G[u].size(); i++)
{
Edge& e = edges[G[u][i]];
int v = e.to;
if(d[v] == d[u] + && (f = DFS(v, min(a, e.cap - e.flow))) > )
{
flow += f;
e.flow += f;
a -= f;
edges[G[u][i]^].flow -= f;
if(a == ) break;
}
}
return flow;
} int Maxflow()
{
int flow = ;
while(BFS())
{
memset(cur, , sizeof(cur));
flow += DFS(s, INF);
}
return flow;
} int main()
{
while(scanf("%d%d", &N, &M) == )
{
n = N + ;
init();
s = , t = N + ;
for(int i = ; i <= N; i++)
{
int a, b; scanf("%d%d", &a, &b);
AddEdge(s, i, a);
AddEdge(i, t, b);
}
while(M--)
{
int a, b, w; scanf("%d%d%d", &a, &b, &w);
AddEdge(a, b, w);
AddEdge(b, a, w);
} printf("%d\n", Maxflow());
} return ;
}
代码君
POJ 3469 最小割 Dual Core CPU的更多相关文章
- poj 3469 最小割模板sap+gap+弧优化
/*以核心1为源点,以核心2为汇点建图,跑一遍最大流*/ #include<stdio.h> #include<string.h> #include<queue> ...
- poj 3469 Dual Core CPU【求最小割容量】
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 21453 Accepted: 9297 ...
- poj 3469 Dual Core CPU——最小割
题目:http://poj.org/problem?id=3469 最小割裸题. 那个限制就是在 i.j 之间连双向边. 根据本题能引出网络流中二元关系的种种. 别忘了写 if ( x==n+1 ) ...
- POJ 3469.Dual Core CPU 最大流dinic算法模板
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 24830 Accepted: 10756 ...
- POJ 3469 Dual Core CPU Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 23780 Accepted: 10338 Case Time Lim ...
- poj3469 Dual Core CPU——最小割
题目:http://poj.org/problem?id=3469 最小割水题(竟然没能1A): 代码如下: #include<iostream> #include<cstdio&g ...
- poj3469 Dual Core CPU
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 25576 Accepted: 11033 ...
- Dual Core CPU
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 20935 Accepted: 9054 Case ...
- POJ 3469 Dual Core CPU(最小割)
[题目链接] http://poj.org/problem?id=3469 [题目大意] 有N个模块要在A,B两台机器上执行,在不同机器上有不同的花费 另有M个模块组(a,b),如果a和b在同一台机子 ...
随机推荐
- Spring 顾问
1.名称匹配方法切入点顾问 接口:ISomeService public interface ISomeService { public void doSome(); public void doSe ...
- linux创建文件的四种方式(其实是两种,强行4种)
linux创建文件的四种方式: 1.vi newfilename->i->编辑文件->ESC->:wq! 2.touch newfilename 3.cp sourcePath ...
- 外观模式及php实现
外观模式: 外观模式(Facade Pattern):外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更 ...
- 洛谷 P2983 [USACO10FEB]购买巧克力Chocolate Buying
购买巧克力Chocolate Buying 乍一看以为是背包,然后交了一个感觉没错的背包上去. #include <iostream> #include <cstdio> #i ...
- ES6学习(1)
let 和 const 命令 ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效.for循环的计数器,就很合适使用let命令. 下面的代码 ...
- mysql操作封装
<?php//连接数据库function connect(){ $link = mysql_connect(DB_HOST,DB_USER,DB_PWD)or die("数据库连接失 ...
- i-nex安装教程
sudo add-apt-repository ppa:i-nex-development-team/stable sudo apt-get updatesudo apt-get i-nex
- Lucene-如何编写Lucene程序
Lucene版本:7.1 使用Lucene的关键点 创建文档(Document),添加文件(Field),保存了原始数据信息: 把文档加入IndexWriter: 使用QueryParser.pars ...
- python之map,zip,reduce,filter的用法
1.reduce(func,iterable,initial): 参数: - func 可执行函数 - iterable 可迭代对象 - initial 可选,初始参数 功能描述:调用func函数后, ...
- Android(java)学习笔记120:BroadcastReceiver之 应用程序安装和卸载 的广播接收者
国内的主流网络公司(比如网易.腾讯.百度等等),他们往往采用数据挖掘技术获取用户使用信息,从而采用靶向营销.比如电脑上,我们浏览网页的时候,往往会发现网页上会出现我们之前经常浏览内容的商业广告,这就是 ...