您的位置:首页 > 其它

【POJ】3469 Dual Core CPU 最小割——项目分配问题

2014-08-04 20:17 429 查看
传送门:【POJ】3469 Dual Core CPU

题目分析:刚才HDU4307不是白过的T U T。。这题一看就知道是项目分配问题了,太让人感动了。。。

还是这个函数:



现在就是赤裸裸的用了~

假设每个任务i在核心A上执行Xi取1,在核心B上执行Xi取0。

同时假设与源点相连的是Xi取1的,与汇点相连的是Xi取0的。

那么,现在我们建立源点S,汇点T,对所有的Xi建边S->Xi,Xi->T,表示Xi还不清楚分配给哪个核心执行。

由公式可知Xi选择1会产生ai的花费,Xi选择0会产生bi的花费,Xi和Xj的选择不同会产生cij的花费。

如果Xi取0,说明任务i被分配给了B,那么就不能再将任务分配给A了,这时我们需要割掉边Xi->T,代价为Bi,所以Xi->T的容量为Bi。

如果Xi取1,说明任务i被分配给了A,那么就不能再将任务分配给B了,这时我们需要割掉S->Xi,代价为Ai,所以S->Xi的容量为Ai。

如果存在边i->j,且Xi取1,Xj取0,那么说明Xi和Xj不属于一个集合,那么需要割掉i->j的边,因为割掉边的代价为W,所以边的容量为W。对于本题,一条无向边(i,j)需要拆成两条有向边< i , j >,< j , i >看待。

最后我们跑一遍最小割,最小割的容量就是本题的答案。

PS:我说前排怎么这么多的G++。。原来G++对输入优化优化了蛮多的啊。。。不加输入优化2300ms左右,加了就只有1200ms多了。

代码如下:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;

#define REP( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define REV( i , a , b ) for ( int i = a - 1 ; i >= b ; -- i )
#define FOR( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define FOV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define CLR( a , x ) memset ( a , x , sizeof a )
#define CPY( a , x ) memcpy ( a , x , sizeof a )

typedef long long LL ;
typedef int type_c ;
typedef int type_f ;

const int MAXN = 20005 ;
const int MAXQ = 20005 ;
const int MAXE = 500000 ;
const int INF = 0x3f3f3f3f ;

struct Edge {
	int v , n ;
	type_c c , rc ;
	Edge () {}
	Edge ( int v , type_c c , int n ) : v ( v ) , c ( c ) , n ( n ) {}
} ;

struct Net {
	Edge E[MAXE] ;
	int H[MAXN] , cntE ;
	int d[MAXN] , cur[MAXN] , pre[MAXN] , num[MAXN] ;
	int Q[MAXQ] , head , tail ;
	int s , t , nv ;
	type_f flow ;
	
	int n , m ;
	
	void init () {
		cntE = 0 ;
		CLR ( H , -1 ) ;
	}
	
	void addedge ( int u , int v , type_c c , type_c rc = 0 ) {
		E[cntE] = Edge ( v ,  c , H[u] ) ;
		H[u] = cntE ++ ;
		E[cntE] = Edge ( u , rc , H[v] ) ;
		H[v] = cntE ++ ;
	}
	
	void rev_bfs () {
		CLR ( d , -1 ) ;
		CLR ( num , 0 ) ;
		head = tail = 0 ;
		Q[tail ++] = t ;
		d[t] = 0 ;
		num[d[t]] = 1 ;
		while ( head != tail ) {
			int u = Q[head ++] ;
			for ( int i = H[u] ; ~i ; i = E[i].n ) {
				int v = E[i].v ;
				if ( d[v] == -1 ) {
					Q[tail ++] = v ;
					d[v] = d[u] + 1 ;
					num[d[v]] ++ ;
				}
			}
		}
	}
	
	type_f ISAP () {
		CPY ( cur , H ) ;
		rev_bfs () ;
		flow = 0 ;
		int u = pre[s] = s , i , pos , mmin ;
		while ( d[s] < nv ) {
			if ( u == t ) {
				type_f f = INF ;
				for ( i = s ; i != t ; i = E[cur[i]].v )
					if ( f > E[cur[i]].c ) {
						f = E[cur[i]].c ;
						pos = i ;
					}
				for ( i = s ; i != t ; i = E[cur[i]].v ) {
					E[cur[i]].c -= f ;
					E[cur[i] ^ 1].c += f ;
				}
				u = pos ;
				flow += f ;
			}
			for ( i = cur[u] ; ~i ; i = E[i].n )
				if ( E[i].c && d[u] == d[E[i].v] + 1 )
					break ;
			if ( ~i ) {
				cur[u] = i ;
				pre[E[i].v] = u ;
				u = E[i].v ;
			}
			else {
				if ( 0 == -- num[d[u]] )
					break ;
				for ( mmin = nv , i = H[u] ; ~i ; i = E[i].n )
					if ( E[i].c && mmin > d[E[i].v] ) {
						mmin = d[E[i].v] ;
						cur[u] = i ;
					}
				d[u] = mmin + 1 ;
				num[d[u]] ++ ;
				u = pre[u] ;
			}
		}
		return flow ;
	}
	
	void read ( int &x , char c = 0 ) {
		while ( ( c = getchar () ) < '0' || c > '9' ) ;
		x = c - '0' ;
		while ( ( c = getchar () ) >= '0' && c <= '9' )
			x = x * 10 + c - '0' ;
	}
	
	void solve () {
		int u , v , c , x , y ;
		init () ;
		s = 0 , t = n + 1 , nv = t + 1 ;
		FOR ( i , 1 , n ) {
			read  ( x ) , read ( y ) ;
			addedge ( s , i , x ) ;
			addedge ( i , t , y ) ;
		}
		while ( m -- ) {
			read ( u ) , read ( v ) , read ( c ) ;
			addedge ( u , v , c , c ) ;
		}
		printf ( "%d\n" , ISAP () ) ;
	}
} e ;

int main () {
	while ( ~scanf ( "%d%d" , &e.n , &e.m ) )
		e.solve () ;
	return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: