POJ 3469_Dual Core CPU
题意:
N个模块可以在A,B两个核上运行,分别需要A[i]和B[i],模块之间需要传递数据,若两个模块在同一核上,则不需要花费,否则需要花费w[i]。问最少需要花费多少?
分析:
用最小的费用将两个对象分成两个集合的问题,常常可以转换为最小割问题。
按照N个模块在哪个核上运行分成两个集合。并建边使最小费用等于最小割的容量,即转化为求图中最大流。
代码:
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
struct edge{int to, cap, rev;};
const int maxm = 20010, INF = 0x3fffffff;
int d[maxm], iter[maxm];
int s, t;
vector<edge>G[maxm];
void add_edge(int from, int to, int cap)
{
G[from].push_back((edge){to, cap, G[to].size()});
G[to].push_back((edge){from, 0, G[from].size()-1});
}
void bfs()
{
memset(d, -1, sizeof(d));
queue<int>q;
d[s] = 0;
q.push(s);
while(!q.empty()){
int v = q.front();q.pop();
for(int i = 0; i <G[v].size(); i++){
edge &e = G[v][i];
if(e.cap>0&&d[e.to]<0){
d[e.to] = d[v] + 1;
q.push(e.to);
}
}
}
}
int dfs(int v, int f)
{
if(v==t) return f;
for(int &i = iter[v]; i < G[v].size(); i++){
edge &e = G[v][i];
if(e.cap > 0 && d[v] < d[e.to]){
int tf = dfs(e.to, min(f, e.cap));
if(tf > 0){
e.cap -= tf;
G[e.to][e.rev].cap +=tf;
return tf;
}
}
}
return 0;
}
int max_flow()
{
int flow = 0;
for(;;){
bfs();
if(d[t]<0) return flow;
memset(iter, 0, sizeof(iter));
int f;
while((f = dfs(s, INF))>0){
flow += f;
}
}
}
int main (void)
{
int N, M;scanf("%d%d",&N,&M);
s = N + 1, t = s +1;
int A, B;
for(int i = 1; i <= N;i++){
scanf("%d%d",&A,&B);
add_edge(s, i, B);
add_edge(i, t, A);
}
int a, b, w;
for(int i = 0; i < M; i++){
scanf("%d%d%d",&a, &b, &w);
add_edge(a, b, w);
add_edge(b, a, w);
}
printf("%d\n",max_flow());
}
输入时少读个数都能过样例。也是奇葩【手动再见】。还是细心细心。
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 ...
- POJ 3469 Dual Core CPU Dual Core CPU
Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 23780 Accepted: 10338 Case Time Lim ...
- poj 3469 Dual Core CPU【求最小割容量】
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 21453 Accepted: 9297 ...
- Dual Core CPU
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 20935 Accepted: 9054 Case ...
- 2018.06.27Dual Core CPU(最小割)
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 26136 Accepted: 11270 Cas ...
- poj3469 Dual Core CPU
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 25576 Accepted: 11033 ...
- Inter Core CPU 型号的尾字母含义
Inter Core CPU 型号的尾字母含义: M:表示移动处理器(Mobile Processor):QM:四核移动处理器(Quad Mobile Processor):U:超低电压处理器(Ult ...
- POJ 3469 Dual Core CPU (最小割建模)
题意 现在有n个任务,两个机器A和B,每个任务要么在A上完成,要么在B上完成,而且知道每个任务在A和B机器上完成所需要的费用.然后再给m行,每行 a,b,w三个数字.表示如果a任务和b任务不在同一个机 ...
- poj 3469 Dual Core CPU
题目描述:由于越来越多的计算机配置了双核CPU,TinySoft公司的首席技术官员,SetagLilb,决定升级他们的产品-SWODNIW.SWODNIW包含了N个模块,每个模块必须运行在某个CPU中 ...
随机推荐
- Neither BindingResult nor plain target object for bean name 'user' available as request attribute
这个异常是因为jsp页面写错了. 把<form:form></form:form>标签改成普通的标签即可. 应该是第一次访问的时候,user是空的.但springmvc不能是空 ...
- 移动web开发基础(一)——像素
这篇文章要弄清楚2个问题:一.什么是逻辑像素和物理像素:二.这两者有什么关系. 对于问题一,先抛出两个概念.我们经常使用的px就是逻辑像素,是浏览器使用的抽象单位:物理像素又和dp/pt(设备无关像素 ...
- sublime text3安装Package Control
转自:https://www.cnblogs.com/lq147760524/p/8202521.html 一.下载Sublime3 https://www.sublimetext.com/3 二.安 ...
- vscode显示php函数列表
1.安装插件支持 https://marketplace.visualstudio.com/items?itemName=linyang95.php-symbols 2.ctrt+shift+o 即可 ...
- Day3 CSS 引入及基本选择器
一 .CSS 层叠样式表,为了使网页元素的样式更加丰富,内容与样式拆分开来.HTML负责结构与内容,表现形式交给CSS. CSS注释/**/ 来注释 二.CSS基本语法与引用 CSS的语法结构 选择器 ...
- JAVA——不简单的fianl关键字
protected用来修饰 域,代表域的访问权限是:包权限 或者 不同包,但是是子类 : final 修饰常量只要是该常量代入的计算式,在编译时期,就会被执行计算,以减轻运行时的负担.(只对基本数据类 ...
- Flutter走过的坑(持续更新)
1 Target of URI doesn't exist 'package:flutter/material.dart' 官方下载的flutter中有一个example文件夹,里面有很多flutte ...
- TCP/IP 协议分层
协议分层 可能大家对OSI七层模型并不陌生,它将网络协议很细致地从逻辑上分为了7层.但是实际运用中并不是按七层模型,一般大家都只使用5层模型.如下: 物理层:一般包括物理媒介,电信号,光信号等,主要对 ...
- SQL Server 兼容级别
ALTER DATABASE (Transact-SQL) 兼容级别 适用对象:SQL Server(从 2008 版开始)Azure SQL 数据库Azure SQL 数据仓库并行数据仓库 将某些数 ...
- python compare with other language
java http://dirtsimple.org/2004/12/python-is-not-java.htmlhttp://twistedmatrix.com/users/glyph/rant/ ...