【COGS】1325 [ZJOI2010] 网络扩容 最大流+费用流

传送门:【COGS】1325 [ZJOI2010] 网络扩容


题目分析:首先按要求添加边,但是费用为0(这是原图就存在的),求一遍最大流,然后用再添加同样的边一遍但是容量为无穷大,费用为w(这是添加边的费用),求一遍费用流即可。


代码如下:


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

typedef long long LL ;

#define REP( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define REV( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define FOR( 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 )

const int MAXN = 1005 ;
const int MAXM = 5005 ;
const int MAXE = 100005 ;
const int INF = 0x3f3f3f3f ;

struct Edge {
	int v , c , w , n ;
	Edge () {}
	Edge ( int v , int c , int w , int n ) : v ( v ) , c ( c ) , w ( w ) , n ( n ) {}
} E[MAXE] ;

int H[MAXN] , cntE ;
int d[MAXN] , cur[MAXN] , cap[MAXN] , cnt[MAXN] , pre[MAXN] ;
int Q[MAXN] , head , tail ;
bool vis[MAXN] ;
int s , t , nv ;
int flow ;
int cost ;
int n , m , k ;
int uu[MAXM] , vv[MAXM] , cc[MAXM] , ww[MAXM] ;

void clear () {
	cntE = 0 ;
	CLR ( H , -1 ) ;
}

void addedge ( int u , int v , int c , int w ) {
	E[cntE] = Edge ( v , c ,  w , H[u] ) ;
	H[u] = cntE ++ ;
	E[cntE] = Edge ( u , 0 , -w , H[v] ) ;
	H[v] = cntE ++ ;
}

void rev_bfs () {
	CLR ( d , -1 ) ;
	CLR ( cnt , 0 ) ;
	head = tail = 0 ;
	d[t] = 0 ;
	Q[tail ++] = t ;
	cnt[0] = 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] ) continue ;
			d[v] = d[u] + 1 ;
			cnt[d[v]] ++ ;
			Q[tail ++] = v ;
		}
	}
}

int ISAP () {
	CPY ( cur , H ) ;
	rev_bfs () ;
	flow = 0 ;
	int u = pre[s] = s , i , f , pos , minv ;
	while ( d[s] < nv ) {
		if ( u == t ) {
			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 == cnt[d[u]] ) break ;
			for ( minv = nv , i = H[u] ; ~i ; i = E[i].n ) {
				int v = E[i].v ;
				if ( E[i].c && minv > d[v] ) {
					minv = d[v] ;
					cur[u] = i ;
				}
			}
			d[u] = minv + 1 ;
			cnt[d[u]] ++ ;
			u = pre[u] ;
		}
	}
	return flow ;
}

int spfa () {
	CLR ( d , INF ) ;
	CLR ( vis , 0 ) ;
	head = tail = 0 ;
	Q[tail ++] = s ;
	cap[s] = INF ;
	cur[s] = -1 ;
	d[s] = 0 ;
	while ( head != tail ) {
		int u = Q[head ++] ;
		if ( head == MAXN ) head = 0 ;
		vis[u] = 0 ;
		for ( int i = H[u] ; ~i ; i = E[i].n ) {
			int v = E[i].v , c = E[i].c , w = E[i].w ;
			if ( c && d[v] > d[u] + w ) {
				d[v] = d[u] + w ;
				cap[v] = min ( cap[u] , c ) ;
				cur[v] = i ;
				if ( !vis[v] ) {
					vis[v] = 1 ;
					Q[tail ++] = v ;
					if ( tail == MAXN ) tail = 0 ;
				}
			}
		}
	}
	if ( d[t] == INF ) return 0 ;
	cost += d[t] * cap[t] ;
	flow += cap[t] ;
	for ( int i = cur[t] ; ~i ; i = cur[E[i ^ 1].v] ) {
		E[i].c -= cap[t] ;
		E[i ^ 1].c += cap[t] ;
	}
	return 1 ;
}

int mcmf () {
	flow = cost = 0 ;
	while ( spfa () ) ;
	return cost ;
}

void solve () {
	int u , v , c , w ;
	clear () ;
	scanf ( "%d%d%d" , &n , &m , &k ) ;
	s = 1 ;
	t = n ;
	nv = t + 1 ;
	REP ( i , 0 , m ) {
		scanf ( "%d%d%d%d" , &uu[i] , &vv[i] , &cc[i] , &ww[i] ) ;
		addedge ( uu[i] , vv[i] , cc[i] , 0 ) ;
	}
	ISAP () ;
	printf ( "%d " , flow ) ;
	s = 0 ;
	addedge ( s , 1 , k , 0 ) ;
	REP ( i , 0 , m ) addedge ( uu[i] , vv[i] , INF , ww[i] ) ;
	mcmf () ;
	printf ( "%d\n" , cost ) ;
}

int main () {
	freopen ( "networkzj2010.in" , "r" , stdin ) ;
	freopen ( "networkzj2010.out" , "w" , stdout ) ;
	solve () ;
	return 0 ;
}


你可能感兴趣的:(cogs)