【HDU】3987 Harry Potter and the Forbidden Forest 最小割

Harry Potter and the Forbidden Forest

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1335    Accepted Submission(s): 455


Problem Description
Harry Potter notices some Death Eaters try to slip into Castle. The Death Eaters hide in the most depths of Forbidden Forest. Harry need stop them as soon as.

The Forbidden Forest is mysterious. It consists of N nodes numbered from 0 to N-1. All of Death Eaters stay in the node numbered 0. The position of Castle is node n-1. The nodes connected by some roads. Harry need block some roads by magic and he want to minimize the cost. But it’s not enough, Harry want to know how many roads are blocked at least.
 

Input
Input consists of several test cases.

The first line is number of test case.

Each test case, the first line contains two integers n, m, which means the number of nodes and edges of the graph. Each node is numbered 0 to n-1.

Following m lines contains information about edges. Each line has four integers u, v, c, d. The first two integers mean two endpoints of the edges. The third one is cost of block the edge. The fourth one means directed (d = 0) or undirected (d = 1).

Technical Specification

1. 2 <= n <= 1000
2. 0 <= m <= 100000
3. 0 <= u, v <= n-1
4. 0 < c <= 1000000
5. 0 <= d <= 1
 

Output
For each test case:
Output the case number and the answer of how many roads are blocked at least.
 

Sample Input
  
  
3 4 5 0 1 3 0 0 2 1 0 1 2 1 1 1 3 1 1 2 3 3 1 6 7 0 1 1 0 0 2 1 0 0 3 1 0 1 4 1 0 2 4 1 0 3 5 1 0 4 5 2 0 3 6 0 1 1 0 0 1 2 0 1 1 1 1 1 2 1 0 1 2 1 0 2 1 1 1
 

Sample Output
  
  
Case 1: 3 Case 2: 2 Case 3: 2
 

Author
aMR @ WHU
 

Source
2011 Multi-University Training Contest 15 - Host by WHU

传送门:【HDU】3987 Harry Potter and the Forbidden Forest

题目大意:求割边最少的最小割。

题目分析:将容量乘上一个比较大的常数并加一来代替原来的容量,这样跑出来的最大流除以常数就是最小割容量,对常数取模就是最少割边数。

代码如下:

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

#define RPEF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define clear( a , x ) memset ( a , x , sizeof a )
#define copy( a , x ) memcpy ( a , x , sizeof a )

typedef long long LL ;

const int MAXN = 1005 ;
const int MAXE = 1000000 ;
const int MAXQ = 1000000 ;
const LL INF = 1e15 ;

struct Edge {
	int v , n ;
	LL c ;
	Edge ( int var = 0 , LL cap = 0 , int next = 0 ) :
		v ( var ) , c ( cap ) , n ( next ) {}
} ;

struct netWork {
	Edge edge[MAXE] ;
	int adj[MAXN] , cntE ;
	int cur[MAXN] , d[MAXN] , num[MAXN] , pre[MAXN] ;
	bool vis[MAXN] ;
	int Q[MAXQ] , head , tail ;
	int s , t , nv ;
	LL flow ;
	
	void init () {
		cntE = 0 ;
		clear ( adj , -1 ) ;
	}
	
	void addedge ( int u , int v , LL c , LL rc = 0 ) {
		edge[cntE] = Edge ( v ,  c , adj[u] ) ;
		adj[u] = cntE ++ ;
		edge[cntE] = Edge ( u , rc , adj[v] ) ;
		adj[v] = cntE ++ ;
	}
	
	void rev_Bfs () {
		clear ( vis , 0 ) ;
		clear ( num , 0 ) ;
		d[t] = 0 ;
		vis[t] = 1 ;
		head = tail = 0 ;
		Q[tail ++] = t ;
		num[0] = 1 ;
		while ( head != tail ) {
			int u = Q[head ++] ;
			for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
				int v = edge[i].v ;
				if ( vis[v] )
					continue ;
				vis[v] = 1 ;
				d[v] = d[u] + 1 ;
				++ num[d[v]] ;
				Q[tail ++] = v ;
			}
		}
	}
	
	LL ISAP () {
		copy ( cur , adj ) ;
		rev_Bfs () ;
		flow = 0 ;
		int i , u = pre[s] = s ;
		while ( d[s] < nv ) {
			if ( u == t ) {
				LL f = INF ;
				int pos ;
				for ( i = s ; i != t ; i = edge[cur[i]].v )
					if ( f > edge[cur[i]].c )
						f = edge[cur[i]].c , pos = i ;
				for ( i = s ; i != t ; i = edge[cur[i]].v )
					edge[cur[i]].c -= f , edge[cur[i] ^ 1].c += f ;
				u = pos ;
				flow += f ;
			}
			for ( i = cur[u] ; ~i ; i = edge[i].n )
				if ( edge[i].c && d[u] == d[edge[i].v] + 1 )
					break ;
			if ( ~i ) {
				cur[u] = i ;
				pre[edge[i].v] = u ;
				u = edge[i].v ;
			}
			else {
				if ( 0 == ( -- num[d[u]] ) )
					break ;
				int mmin = nv ;
				for ( i = adj[u] ; ~i ; i = edge[i].n )
					if ( edge[i].c && mmin > d[edge[i].v] )
						cur[u] = i , mmin = d[edge[i].v] ;
				d[u] = mmin + 1 ;
				++ num[d[u]] ;
				u = pre[u] ;
			}
		}
		return flow ;
	}
} ;

netWork net ;

int read () {
	char c = ' ' ;
	int x = 0 ;
	while ( c < '0' || c > '9' )
		c = getchar () ;
	while ( c >= '0' && c <= '9' ) {
		x = x * 10 + c - '0' ;
		c = getchar () ;
	}
	return x ;
}

void work () {
	int n , m ;
	int d , u , v , c ;
	net.init () ;
	n = read () , m = read () ;
	net.s = 0 , net.t = n - 1 , net.nv = net.t + 1 ;
	while ( m -- ) {
		u = read () , v = read () , c = read () , d = read () ;
		LL cc = c * 1000000LL + 1 ;
		if ( d )
			net.addedge ( u , v , cc , cc ) ;
		else
			net.addedge ( u , v , cc ) ;
	}
	LL flow = net.ISAP () ;
	printf ( "%I64d\n" ,  flow % 1000000 ) ;
}

int main () {
	int T , cas = 0 ;
	scanf ( "%d" , &T ) ;
	while ( T -- ) {
		printf ( "Case %d: " , ++ cas ) ;
		work () ;
	}
	return 0 ;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值