题意:

一个双核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的更多相关文章

  1. poj 3469 最小割模板sap+gap+弧优化

    /*以核心1为源点,以核心2为汇点建图,跑一遍最大流*/ #include<stdio.h> #include<string.h> #include<queue> ...

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

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

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

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

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

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

  5. POJ 3469 Dual Core CPU Dual Core CPU

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

  6. poj3469 Dual Core CPU——最小割

    题目:http://poj.org/problem?id=3469 最小割水题(竟然没能1A): 代码如下: #include<iostream> #include<cstdio&g ...

  7. poj3469 Dual Core CPU

    Dual Core CPU Time Limit: 15000MS   Memory Limit: 131072K Total Submissions: 25576   Accepted: 11033 ...

  8. Dual Core CPU

    Dual Core CPU Time Limit: 15000MS Memory Limit: 131072K Total Submissions: 20935 Accepted: 9054 Case ...

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

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

随机推荐

  1. CSS3学习-用CSS制作立体导航栏

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Git中文件属性的变化,被认为是文件有改动

    问题描述: 1.  从公司的git服务器上, 下载最新的代码(zip格式), 解压缩出来, 2.  过一段时间, 去执行git pull代码, 出现如下情况: $ git pull Updating ...

  3. Python+selenium之下载文件

    一.Firefox文件下载 Web容许我们设置默认的文件下载路劲,文件会自动下载并且存放在指定的目录下. from selenium import webdriver import os fp = w ...

  4. C#语言基础 Main 函数中的输出输入

    C# 是一门面向对象的编程语言,保留了C  C++等等强大功能,但是它与 Java 非常相似,有许多强大的编程功能,它是微软(Microsoft)专门为.NET应用而开发的一门语言. 也就是人与计算机 ...

  5. APP自动化测试

    CTS工具,主要是基于Androidinstrumentation和JUnit测试原理推单元测试用例: Monkey用来对UI进行压力测试,伪随机的模拟用户的按键输入,触摸屏输入,手势输入等: ASE ...

  6. External Pricing in C4C and ERP

    从下图可以看出,C4C的Opportunity,Sales Quote和Sales Order这些business transaction没有自己的pricing engine,使用的是在ERP Pr ...

  7. 卓越管理的实践技巧(1)如何进行有效的指导 Guidelines for Effective Coaching

    Guidelines for Effective Coaching 前文卓越管理的秘密(Behind Closed Doors)最后一部分提到了总结的13条卓越管理的实践技巧并列出了所有实践技巧名称的 ...

  8. Oracle RAC/Clusterware 多种心跳heartbeat机制介绍 RAC超时机制分析

    ORACLE RAC中最主要存在2种clusterware集群件心跳 &  RAC超时机制分析: 1.Network Heartbeat 网络心跳 每秒发生一次: 10.2.0.4以后网络心跳 ...

  9. Codeforces Round #321 (Div. 2) D Kefa and Dishes(dp)

    用spfa,和dp是一样的.转移只和最后一个吃的dish和吃了哪些有关. 把松弛改成变长.因为是DAG,所以一定没环.操作最多有84934656,514ms跑过,实际远远没这么多. 脑补过一下费用流, ...

  10. springmvc 的原理分析

    1. 用户发送请求至前端控制器(DispatcherServlet) 2.DispatcherServlet 将受到的请求调用HandlerMapping 处理映射器 3.处理器映射器根据配置注解找到 ...