Dual Core CPU

Time Limit: 15000MS Memory Limit: 131072K

Total Submissions: 26136 Accepted: 11270

Case Time Limit: 5000MS

Description

As more and more computers are equipped with dual core CPU, SetagLilb, the Chief Technology Officer of TinySoft Corporation, decided to update their famous product - SWODNIW.

The routine consists of N modules, and each of them should run in a certain core. The costs for all the routines to execute on two cores has been estimated. Let’s define them as Ai and Bi. Meanwhile, M pairs of modules need to do some data-exchange. If they are running on the same core, then the cost of this action can be ignored. Otherwise, some extra cost are needed. You should arrange wisely to minimize the total cost.

Input

There are two integers in the first line of input data, N and M (1 ≤ N ≤ 20000, 1 ≤ M ≤ 200000) .

The next N lines, each contains two integer, Ai and Bi.

In the following M lines, each contains three integers: a, b, w. The meaning is that if module a and module b don’t execute on the same core, you should pay extra w dollars for the data-exchange between them.

Output

Output only one integer, the minimum total cost.

Sample Input

3 1

1 10

2 10

10 3

2 3 1000

Sample Output

13

Source

POJ Monthly–2007.11.25, Zhou Dong

由于要将nnn个点分成两组,那么这显然是“割”的意思,再仔细想想,如果我们建立源点sss和汇点ttt,从sss向每个点连容量为AiA_iAi​的边,从每个点向ttt连容量为BiB_iBi​的边,然后两个点i,ji,ji,j连容量为www的无向边,建好图后我们只用求出最小割即可。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#define N 250000
#define M 3000000
using namespace std;
inline long long read(){
	long long ans=0;
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
	return ans;
}
int n,m,s,t,ans=0,d[N],first[N],cnt=-1;
struct Node{int v,next,c;}e[M<<1];
inline void add(int u,int v,int c){
	e[++cnt].v=v;
	e[cnt].next=first[u];
	e[cnt].c=c;
	first[u]=cnt;
	e[++cnt].v=u;
	e[cnt].next=first[v];
	e[cnt].c=0;
	first[v]=cnt;
}
inline bool bfs(){
	queue<int>q;
	q.push(s);
	memset(d,-1,sizeof(d));
	d[s]=0;
	while(!q.empty()){
		int x=q.front();
		q.pop();
		for(int i=first[x];i!=-1;i=e[i].next){
			int v=e[i].v;
			if(d[v]!=-1||e[i].c<=0)continue;
			d[v]=d[x]+1;
			if(v==t)return true;
			q.push(v);
		}
	}
	return false;
}
inline int dfs(int x,int f){
	if(x==t||!f)return f;
	int flow=f;
	for(int i=first[x];i!=-1;i=e[i].next){
		int v=e[i].v;
		if(flow&&e[i].c>0&&d[v]==d[x]+1){
			int tmp=dfs(v,min(flow,e[i].c));
			if(!tmp)d[v]=-1;
			e[i].c-=tmp;
			e[i^1].c+=tmp;
			flow-=tmp;
		}
	}
	return f-flow;
}
int main(){
	memset(first,-1,sizeof(first));
	n=read(),m=read(),s=0,t=n+1;
	for(int i=1;i<=n;++i){
		int a=read(),b=read();
		add(s,i,a),add(i,t,b);
	}
	for(int i=1;i<=m;++i){
		int a=read(),b=read(),w=read();
		add(a,b,w),add(b,a,w);
	}
	while(bfs())ans+=dfs(s,0x3f3f3f3f);
	printf("%d",ans);
	return 0;
}

2018.06.27Dual Core CPU(最小割)的更多相关文章

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

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

  2. POJ3469 Dual Core CPU(最小割)

    题意:给你n个模块,每个模块在A核花费为ai,在B核跑花费为bi,然后由m个任务(ai,bi,wi),表示如果ai,bi不在同一个核上跑,额外的花费为wi,求最小的花费. 一开始想的时候以为是费用流, ...

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

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

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

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

  5. poj3469 Dual Core CPU——最小割

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

  6. poj 3469 Dual Core CPU 最小割

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

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

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

  8. POJ 3469 最小割 Dual Core CPU

    题意: 一个双核CPU上运行N个模块,每个模块在两个核上运行的费用分别为Ai和Bi. 同时,有M对模块需要进行数据交换,如果这两个模块不在同一个核上运行需要额外花费. 求运行N个模块的最小费用. 分析 ...

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

    (点击此处查看原题) 题意介绍 在一个由核A和核B组成的双核CPU上执行N个任务,任务i在核A上执行,花费Ai,在核B上执行,花费为Bi,而某两个任务之间可能需要进数据交互,如果两个任务在同一个核上执 ...

随机推荐

  1. CodeForces - 920C Swap Adjacent Elements

    传送门:点我 You have an array a consisting of n integers. Each integer from 1 to n appears exactly once i ...

  2. Python+Selenium学习--alert/confirm/prompt 处理

    场景 webdriver 中处理JavaScript 所生成的alert.confirm 以及prompt 是很简单的.具体思路是使用switch_to.alert()方法定位到alert/confi ...

  3. SpringBoot08 请求方式、参数获取注解、参数验证、前后台属性名不一致问题、自定义参数验证注解、BeanUtils的使用

    1 请求方式 在定义一个Rest接口时通常会利用GET.POST.PUT.DELETE来实现数据的增删改查:这几种方式有的需要传递参数,后台开发人员必须对接收到的参数进行参数验证来确保程序的健壮性 1 ...

  4. 【Linux 线程】线程同步《二》

    1.读写锁 与互斥量类似,但读写锁允许更高的并行性.其特性为:写独占,读共享. 读写锁状态: 一把读写锁具备三种状态: (1)读模式下加锁状态 (读锁) (2)写模式下加锁状态 (写锁) (3)不加锁 ...

  5. Java06-java基础语法(五)数组

    Java06-java基础语法(五)数组 一.循环的嵌套 在一个循环体内部再含有一个或多个循环 强调:内循环全部做完以后再去执行下一次的外循环 int k = 0; for(int i = 0; i& ...

  6. AngularJS——第4章 数据绑定

    第4章 数据绑定 AngularJS是以数据做为驱动的MVC框架,所有模型(Model)里的数据经由控制器(Controller)展示到视图(View)中. 所谓数据绑定指的就是将模型(Model)中 ...

  7. SVO+PL-SVO+PL-StVO

    PL-SVO是基于点.线特征的半直接法单目视觉里程计,我们先来介绍一下基于点特征的SVO,因为是在这个基础上提出的. [1]References:      SVO: Fast Semi-Direct ...

  8. go语言使用go-sciter创建桌面应用(五) 加载元素资源

    有些时候我们需要动态的给某个UI元素加载内容或数据. demo6.go代码如下: package main; import ( "github.com/sciter-sdk/go-scite ...

  9. mysql修改表引擎Engine

    修改my.ini,在[mysqld]下加上default-storage-engine=INNODB 其中红色字体部分是要指定的引擎名称.用sql语句修改已经建成表的引擎:alter table ta ...

  10. Excel上传找到错误数据类型

    一:查询数据库表中字段的类型语句 SELECT CASE WHEN col.colorder = 1 THEN obj.name ELSE '' END AS 表名, col.colorder AS ...