【HDU】3663 Power Stations 精确覆盖

传送门:【HDU】3663 Power Stations


题目分析:可以将每个镇子按照天数拆成N*D列,然后对于每个镇子的可行区间,按照区间的可行方案依次拆成行,这样本题就转化成了所有列的1的覆盖的问题,但是我们要保证每个镇子只能选一次,那么我们再增加N列,所有属于一个镇子x的区间,在N*D+x的位置设为1。那么就是Dancing Links的活了~


PS:找了一晚上的RE,真心纠结啊TUT,回到寝室用手机看代码,一直没发现哪里错。。。。然后最后一不小心瞟到输入,发现没加scanf != EOF 。。。。然后手机上加上个'~'交一下就AC了TUT。。。。


代码如下:


#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 REC( i , A , o ) for ( int i = A[o] ; i != o ; i = A[i] )
#define CLR( a , x ) memset ( a , x , sizeof a )

const int MAXN = 65 ;
const int MAXM = 2000 ;
const int MAXNODE = 500000 ;

struct Node {
	int l , r , idx ;
	Node () {}
	Node ( int l , int r , int idx ) : l ( l ) , r ( r ) , idx ( idx ) {}
} ;

struct DLX {
	int U[MAXNODE] , D[MAXNODE] , L[MAXNODE] , R[MAXNODE] ;
	int row[MAXNODE] , col[MAXNODE] ;
	int S[MAXM] , H[MAXM] ;
	int deep , ans[MAXN] ;
	int n , m ;
	int size ;
	
	
	int N , M , DD ;
	int X[MAXN] , Y[MAXN] ;
	Node node[MAXM] ;
	int G[MAXN][MAXN] ;
	
	void remove ( int c ) {
		L[R[c]] = L[c] ;
		R[L[c]] = R[c] ;
		REC ( i , D , c )
			REC ( j , R , i ) {
				D[U[j]] = D[j] ;
				U[D[j]] = U[j] ;
				-- S[col[j]] ;
			}
	}
	
	void resume ( int c ) {
		REC ( i , U , c )
			REC ( j , L , i ) {
				++ S[col[j]] ;
				U[D[j]] = j ;
				D[U[j]] = j ;
			}
		R[L[c]] = c ;
		L[R[c]] = c ;
	}
	
	int dance ( int d ) {
		if ( R[0] == 0 ) {
			deep = d ;
			return 1 ;
		}
		int c = R[0] ;
		REC ( i , R , 0 )
			if ( S[c] > S[i] )
				c = i ;
		//printf ( "ok\n" ) ;
		remove ( c ) ;
		REC ( i , D , c ) {
			ans[d] = row[i] ;
			REC ( j , R , i )
				remove ( col[j] ) ;
			if ( dance ( d + 1 ) )
				return 1 ;
			REC ( j , L , i )
				resume ( col[j] ) ;
		}
		resume ( c ) ;
		return 0 ;
	}
	
	void link ( int r , int c ) {
		++ size ;
		++ S[c] ;
		row[size] = r ;
		col[size] = c ;
		U[size] = U[c] ;
		D[size] = c ;
		D[U[c]] = size ;
		U[c] = size ;
		if ( ~H[r] ) {
			R[size] = H[r] ;
			L[size] = L[H[r]] ;
			R[L[size]] = size ;
			L[R[size]] = size ;
		}
		else
			H[r] = L[size] = R[size] = size ;
	}
	
	void init () {
		CLR ( H , -1 ) ;
		FOR ( i , 0 , n ) {
			S[i] = 0 ;
			L[i] = i - 1 ;
			R[i] = i + 1 ;
			U[i] = i ;
			D[i] = i ;
		}
		L[0] = n ;
		R[n] = 0 ;
		size = n ;
	}
	
	void solve () {
		int x , y ;
		n = N * DD + N ;
		m = 0 ;
		init () ;
		CLR ( G , 0 ) ;
		CLR ( node , 0 ) ;
		REP ( i , 0 , M ) {
			scanf ( "%d%d" , &x , &y ) ;
			G[x][y] = G[y][x] = 1 ;
		}
		FOR ( i , 1 , N )
			G[i][i] = 1 ;
		FOR ( idx , 1 , N ) {
			scanf ( "%d%d" , &x , &y ) ;
			int tmp = N * DD + idx ;
			++ m ;
			link ( m , tmp ) ;//none select
			node[m] = Node ( 0 , 0 , idx ) ;
			FOR ( i , x , y )
				FOR ( j , i , y ) {
					++ m ;
					link ( m ,  tmp ) ;
					node[m] = Node ( i , j , idx ) ;
					FOR ( a , 1 , N )
						if ( G[idx][a] )
							FOR ( b , i , j )
								link ( m , ( a - 1 ) * DD + b ) ;
				}
		}
		if ( !dance ( 0 ) )
			printf ( "No solution\n" ) ;
		else {
			CLR ( X , 0 ) ;
			CLR ( Y , 0 ) ;
			REP ( i , 0 , deep ) {
				X[node[ans[i]].idx] = node[ans[i]].l ;
				Y[node[ans[i]].idx] = node[ans[i]].r ;
			}
			FOR ( i , 1 , N )
				printf ( "%d %d\n" , X[i] , Y[i] ) ;
		}
		printf ( "\n" ) ;
	}
						
} dlx ;

int main () {
	while ( ~scanf ( "%d%d%d" , &dlx.N , &dlx.M , &dlx.DD  ) )
		dlx.solve () ;
	return 0 ;
}


你可能感兴趣的:(HDU,Links,Dancing)