(点击此处查看原题)

题意介绍

在一个由核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 (最小割)的更多相关文章

  1. poj 3469 Dual Core CPU——最小割

    题目:http://poj.org/problem?id=3469 最小割裸题. 那个限制就是在 i.j 之间连双向边. 根据本题能引出网络流中二元关系的种种. 别忘了写 if ( x==n+1 ) ...

  2. POJ 3469 Dual Core CPU (最小割建模)

    题意 现在有n个任务,两个机器A和B,每个任务要么在A上完成,要么在B上完成,而且知道每个任务在A和B机器上完成所需要的费用.然后再给m行,每行 a,b,w三个数字.表示如果a任务和b任务不在同一个机 ...

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

    [题意]有n个程序,分别在两个内核中运行,程序i在内核A上运行代价为ai,在内核B上运行的代价为bi,现在有程序间数据交换,如果两个程序在同一核上运行,则不产生额外代价,在不同核上运行则产生Cij的额 ...

  4. poj 3469 Dual Core CPU 最小割

    题目链接 好裸的题....... 两个cpu分别作为源点和汇点, 每个cpu向元件连边, 权值为题目所给的两个值, 如果两个元件之间有关系, 就在这两个元件之间连边, 权值为消耗,这里的边应该是双向边 ...

  5. poj 3469 Dual Core CPU【求最小割容量】

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 21453   Accepted: 9297 ...

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

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 24830   Accepted: 10756 ...

  7. POJ 3469 Dual Core CPU Dual Core CPU

    Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 23780   Accepted: 10338 Case Time Lim ...

  8. POJ 3469(Dual Core CPU-最小割)[Template:网络流dinic V2]

    Language: Default Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 19321 ...

  9. POJ 3469 Dual Core CPU(最小割)

    [题目链接] http://poj.org/problem?id=3469 [题目大意] 有N个模块要在A,B两台机器上执行,在不同机器上有不同的花费 另有M个模块组(a,b),如果a和b在同一台机子 ...

随机推荐

  1. Lock和synchronized的区别

    总结来说,Lock和synchronized有以下几点不同: 1)Lock是一个接口,而synchronized是Java中的关键字,synchronized是内置的语言实现: 2)synchroni ...

  2. hbase数据导入

    hbase数据导入: 参考http://blog.csdn.net/hua840812/article/details/7414875,在把代码copy下来后,发现运行总是报错: java.io.IO ...

  3. Codeforces Round #567 (Div. 2) E2 A Story of One Country (Hard)

    https://codeforces.com/contest/1181/problem/E2 想到了划分的方法跟题解一样,但是没理清楚复杂度,很难受. 看了题解觉得很有道理,还是自己太菜了. 然后直接 ...

  4. java web过滤器防止未登录进入界面

    import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import ja ...

  5. Docker部署测试

    安装虚拟机 准备一台Centos7的VM,名为Centos7-1 具体过程可以参考: KVM安装 KVM——以桥接的方式搭建虚拟机网络配置 安装Docker 下载rpm包:https://downlo ...

  6. DP----鬼畜的数字三角形

    数字三角形 1   洛谷   P1216  数字金字塔 我们可以用 f [ i ] [ j ] 表示从(1,1)出发,到达(i,j)的最大权值和. (i , j)可以由 正上(i - 1 , j)或者 ...

  7. [Ajax三级联动 无刷新]

    三级联动 的效果图 html页面: <body> <label class="fl">区域:</label> <select class= ...

  8. 查询出与jack互为好友的人名字

    建表 /* Navicat MySQL Data Transfer Source Server : connect1 Source Server Version : 50611 Source Host ...

  9. LC 963. Minimum Area Rectangle II

    Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these ...

  10. Zabbix - 配置服务器对第三方服务的监控

    需求: 需要配置zabbix监控,使得zabbix服务器可以监控到另一台服务器上运行的第三方服务的状态,当状态异常时发送告警邮件. 限制:被监控的服务器不允许安装任意客户端,且该台服务器不能联通外网 ...