题意:

一个双核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. Java基础学习_01 概述及环境配置

    一.概述 1.Java语言平台版本 1.1J2SE(Java 2 Platform Standard Edition)标准版 为开发普通桌面和商务应用程序提供的解决方案,该技术体系是其他两者的基础,可 ...

  2. java中接口(interface)和虚基类(abstract class)的区别

    在Java语言中,abstract class和interface是支持抽象类定义的两种机制.正是由于这两种机制的存在,才赋予了Java强大的面向对象能力.abstract class和interfa ...

  3. 介绍我最近做的网站 Asp.Net MVC4 + BootStrap

    一.前言 最近一直在做一个多站SEO数据分析的站点(www.easyyh.com),用了一些新技术,如Asp.Net MVC4,BootStrap,EasyUI,这些都是以前没有搞过的,最近搞得差不多 ...

  4. form-data、x-www-form-urlencoded、raw、binary的区别

    1.form-data: 就是http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开.既可以上传键值对,也可以上传文件.当上传的字段是文件时 ...

  5. C语言的time函数和localtime函数

    1.获取当前时间,并获取当前时间(即系统时间)距离1970年1月1日的时间间隔,以秒为单位. 2.获取指定时间距离1970年1月1日的时间间隔,以秒为单位.

  6. 数据库之游标过程-- 基于MySQL

    实例如下: DROP PROCEDURE IF EXISTS pr_change_station_user_acct_his; -- 如果存在存储过程,即删除存储过程 create procedure ...

  7. C#解析 json格式

    C# 解析 json JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集. JSON采用完全独立于语言的 ...

  8. fpga Verilog hdl 按键消抖 部分程序讲解

    module debounce(clk_in,rst_in,key_in,key_pulse,key_state); input clk_in;//system clock input rst_in; ...

  9. 爬取豆瓣电影top250并存储到mysql数据库

    import requests from lxml import etree import re import pymysql import time conn= pymysql.connect(ho ...

  10. cookie存验证码时间,时间没走完不能再次点击

    <script> var balanceSeconds=getcookie('Num'); console.log(balanceSeconds) var timer; var isCli ...