POJ - 3469 Dual Core CPU (最小割)
题意介绍
在一个由核A和核B组成的双核CPU上执行N个任务,任务i在核A上执行,花费Ai,在核B上执行,花费为Bi,而某两个任务之间可能需要进数据交互,如果两个任务在同一个核上执行,那么数据交互将没有花费,如果在不同核上执行,将产生wi的花费,问将n个任务全部执行产生的最小花费 。
解题思路
题目要求将n个任务分配为两个不同的集合,使得执行n个任务总花费最少,这类的题目我们一般将其转化为最小割问题,即花费最小的代价将n个点分为两部分。建图如下:
1)由源点向每个任务建一条容量为Ai的边
2)由每个任务向汇点建一条容量为Bi的边
3)对于不在同一核上运行的两个任务a,b,将产生额外的w花费,我们在a,b之间建两条容量为w的边,分为是a->b,b->a
最后我们在构建的图中跑最小割并输出最小割即可。
代码区
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<string>
#include<fstream>
#include<vector>
#include<stack>
#include <map>
#include <iomanip> #define bug cout << "**********" << endl
#define show(x, y) cout<<"["<<x<<","<<y<<"] "
#define LOCAL = 1;
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll mod = 1e9 + ;
const int Max = 4e6 + ; struct Edge
{
int to, flow, next;
} edge[Max << ]; int n, m, s, t;
int head[Max], tot;
int dis[Max], cur[Max]; void init()
{
memset(head, -, sizeof(head));
tot = ;
s = ;
t = n + ;
} void add(int u, int v, int flow)
{
edge[tot].to = v;
edge[tot].flow = flow;
edge[tot].next = head[u];
head[u] = tot++;
} bool bfs()
{
memset(dis, -, sizeof(dis));
queue<int> q;
dis[s] = ;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].flow > && dis[v] == -)
{
dis[v] = dis[u] + ;
if (v == t)
return true;
q.push(v);
}
}
}
return false;
} int dfs(int u, int flow_in)
{
if (u == t)
return flow_in;
int flow_out = ;
for (int i = cur[u]; i != -; i = edge[i].next)
{
cur[u] = i;
int v = edge[i].to;
if (dis[v] == dis[u] + && edge[i].flow > )
{
int flow = dfs(v, min(flow_in, edge[i].flow));
if (flow == )
continue;
flow_in -= flow;
flow_out += flow;
edge[i].flow -= flow;
edge[i ^ ].flow += flow;
if (flow_in == )
break;
}
}
return flow_out;
} int Dinic(int ans)
{
int sum = ;
while (bfs())
{
for (int i = ; i <= ans; i++)
cur[i] = head[i];
sum += dfs(s, inf);
}
return sum;
} int main()
{
#ifdef LOCAL
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
while (scanf("%d%d", &n, &m) != EOF)
{
init();
for (int i = , a, b; i <= n; i++)
{
scanf("%d%d", &a, &b);
add(s, i, a);
add(i, s, );
add(i, t, b);
add(t, i, );
}
for(int i =, u, v, flow; i <= m ;i ++)
{
scanf("%d%d%d",&u,&v,&flow);
add(u,v,flow);
add(v,u,flow);
}
printf("%d\n",Dinic(n+));
}
return ;
}
POJ - 3469 Dual Core CPU (最小割)的更多相关文章
- poj 3469 Dual Core CPU——最小割
题目:http://poj.org/problem?id=3469 最小割裸题. 那个限制就是在 i.j 之间连双向边. 根据本题能引出网络流中二元关系的种种. 别忘了写 if ( x==n+1 ) ...
- POJ 3469 Dual Core CPU (最小割建模)
题意 现在有n个任务,两个机器A和B,每个任务要么在A上完成,要么在B上完成,而且知道每个任务在A和B机器上完成所需要的费用.然后再给m行,每行 a,b,w三个数字.表示如果a任务和b任务不在同一个机 ...
- 【网络流#8】POJ 3469 Dual Core CPU 最小割【ISAP模板】 - 《挑战程序设计竞赛》例题
[题意]有n个程序,分别在两个内核中运行,程序i在内核A上运行代价为ai,在内核B上运行的代价为bi,现在有程序间数据交换,如果两个程序在同一核上运行,则不产生额外代价,在不同核上运行则产生Cij的额 ...
- poj 3469 Dual Core CPU 最小割
题目链接 好裸的题....... 两个cpu分别作为源点和汇点, 每个cpu向元件连边, 权值为题目所给的两个值, 如果两个元件之间有关系, 就在这两个元件之间连边, 权值为消耗,这里的边应该是双向边 ...
- poj 3469 Dual Core CPU【求最小割容量】
Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 21453 Accepted: 9297 ...
- 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-最小割)[Template:网络流dinic V2]
Language: Default Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 19321 ...
- POJ 3469 Dual Core CPU(最小割)
[题目链接] http://poj.org/problem?id=3469 [题目大意] 有N个模块要在A,B两台机器上执行,在不同机器上有不同的花费 另有M个模块组(a,b),如果a和b在同一台机子 ...
随机推荐
- AcWing:173. 矩阵距离(bfs)
给定一个N行M列的01矩阵A,A[i][j] 与 A[k][l] 之间的曼哈顿距离定义为: dist(A[i][j],A[k][l])=|i−k|+|j−l|dist(A[i][j],A[k][l]) ...
- Jmeter -- 循环控制器和线程并发(关注执行顺序)
测试计划中包含两个线程组,分别设置如下: 线程组1:线程数为2,循环次数为4 线程组2:线程数为4 执行顺序 监听器View Results in Table(用表格查看结果)中,ThreadName ...
- Pycharm添加python2解释器
Anaconda3+python3环境下如何创建python2环境(win+Linux下适用,同一个anaconda下py2/3共存) conda info -e 查看python2的路径 Pycha ...
- zookeeper系列(七)zookeeper的序列化及通讯协议
作者:leesf 掌控之中,才会成功:掌控之外,注定失败.原创地址http://www.cnblogs.com/leesf456/p/6091208.html尊重作者原创,奇文共欣赏,大家共同学 ...
- mybatis oracle mysql 批量插入时的坑爹问题--需谨记
mybatis oracle mysql 批量插入一.oracle的批量插入方式insert into db(id, zgbh, shbzh) select '1', '2', '3' from du ...
- mysql 查看库结构---查看表结构
查看库结构:SHOW DATABASES; 查看表结构: show tables即为显示当前数据库中所有的表.又如: mysql> use dbname //进入dbname库Database ...
- pandas之to_datetime时区转换
from datetime import date, datetime, timedelta import time import pandas as pd from pand ...
- Centos 安装freesurfer fsl matlab 等软件---转自网络
freesurfer: freesurfer好安装,只需要下载官网源码包,如freesurfer5.3.tar.gz 下载至本地,解压: tar zxvf freesurfer5.3.tar.gz - ...
- [go]go并发
同步协程 通过睡眠方法 // 通过睡眠方式等待 time.Sleep(time.Second) <-time.NewTimer(time.Second).C <-time.After(ti ...
- LC 535. Encode and Decode TinyURL
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...