解题报告 之 POJ3469 Dual Core CPU

解题报告 之 POJ3469 Dual Core CPU


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 asAi 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: abw. 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


题目大意:有A、B两个CPU,有n个任务,m种数据传输。第 i 个任务在A上运行的代价为ai,B上运行的代价为bi,但它只需要选一个CPU运行即可。第 j 种数据传输是如果 任务aj 和任务bj不在同一CPU执行,则需要花费代价cj。问执行完所有任务的最小代价。


分析:用最小花费将点集分为两个子集的问题,可以转化为最小割问题,再转化为最大流。具体如下,超级源点连接每个任务 i,负载为ai。超级汇点连接每个任务i,负载为bi。对于数据传输,从aj连一条边到bj,负载为cj(双向)。然后求出最小割,即将点分为两个子集SA,SB的最小代价,也就是求改图的最大流。


不知道什么MAXN开到25000,MAXM开到500000也RTE。所以是醉了,开太大又给我超时,后来改进了一下模板才成功过掉,真是醉了,不过还是有收获的,至少把Dinic模板又优化了。总之这题的数据感觉很奇葩。


上代码:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdio>
using namespace std;

const int MAXN = 201000;
const int MAXM = 2001000;
const int INF = 0x3f3f3f3f;

struct Edge
{
	int from, to, cap, next;
};

Edge edge[MAXM];
int level[MAXN];
int head[MAXN];
int src, des, cnt;

void addedge( int from, int to, int cap )
{
	edge[cnt].from = from;
	edge[cnt].to = to;
	edge[cnt].cap = cap;
	edge[cnt].next = head[from];
	head[from] = cnt++;

	swap( from, to );

	edge[cnt].from = from;
	edge[cnt].to = to;
	edge[cnt].cap = 0;
	edge[cnt].next = head[from];
	head[from] = cnt++;
}

int bfs( )
{
	memset( level, -1, sizeof level );
	queue<int> q;
	while(!q.empty( ))
		q.pop( );

	level[src] = 0;
	q.push( src );

	while(!q.empty( ))
	{
		int u = q.front( );
		q.pop( );

		for(int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;
			if(edge[i].cap&&level[v] == -1)
			{
				level[v] = level[u] + 1;
				q.push( v );
			}
		}
	}
	return level[des] != -1;
}

int dfs( int u, int f )
{
	if(u == des)
		return f;
	int tem = 0;
	int res = 0;
	for(int i = head[u]; i != -1&&res<f; i = edge[i].next)
	{
		int v = edge[i].to;
		if(edge[i].cap > 0 && level[v] == level[u] + 1)
		{
			tem = dfs( v, min( f - res, edge[i].cap ) );
			edge[i].cap -= tem;
			edge[i ^ 1].cap += tem;
			res += tem;
		}
	}

	if(!res)
		level[u] = -1;
	return res;
}

int Dinic( )
{
	int ans = 0;
	while(bfs( ))
	{
		ans += dfs(src,INF);
	}
	return ans;
}

int main( )
{

	int n, m;
	src = 0; 
	while(cin >> n >> m)
	{
		memset( head, -1, sizeof head );
		cnt = 0;
		des = n + 1;
		int a, b, c;

		for(int i = 1; i <= n; i++)
		{
			scanf( "%d%d", &a, &b );
			addedge( src, i, a );
			addedge( i, des, b );
		}

		for(int i = 1; i <= m; i++)
		{
			scanf( "%d%d%d", &a, &b, &c );
			addedge( a, b, c );
			addedge( b, a, c );
		}

		cout << Dinic( ) << endl;
	}


	return 0;
}

啦啦啦啦啦啦啦


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值