【HDU】4848 Wow! Such Conquering! dfs爆搜+强力剪枝

Wow! Such Conquering!

Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 131    Accepted Submission(s): 42


Problem Description
There are n Doge Planets in the Doge Space. The conqueror of Doge Space is Super Doge, who is going to inspect his Doge Army on all Doge Planets. The inspection starts from Doge Planet 1 where DOS (Doge Olympic Statue) was built. It takes Super Doge exactly Txy time to travel from Doge Planet x to Doge Planet y.
With the ambition of conquering other spaces, he would like to visit all Doge Planets as soon as possible. More specifically, he would like to visit the Doge Planet x at the time no later than Deadlinex. He also wants the sum of all arrival time of each Doge Planet to be as small as possible. You can assume it takes so little time to inspect his Doge Army that we can ignore it.
 

Input
There are multiple test cases. Please process till EOF.
Each test case contains several lines. The first line of each test case contains one integer: n, as mentioned above, the number of Doge Planets. Then follow n lines, each contains n integers, where the y-th integer in the x-th line is Txy . Then follows a single line containing n - 1 integers: Deadline2 to Deadlinen.
All numbers are guaranteed to be non-negative integers smaller than or equal to one million. n is guaranteed to be no less than 3 and no more than 30.
 

Output
If some Deadlines can not be fulfilled, please output “-1” (which means the Super Doge will say “WOW! So Slow! Such delay! Much Anger! . . . ” , but you do not need to output it), else output the minimum sum of all arrival time to each Doge Planet.
 

Sample Input
  
  
4 0 3 8 6 4 0 7 4 7 5 0 2 6 9 3 0 30 8 30 4 0 2 3 3 2 0 3 3 2 3 0 3 2 3 3 0 2 3 3
 

Sample Output
  
  
36 -1
 

Source
2014西安全国邀请赛

传送门:【HDU】4848 Wow! Such Conquering!

题目大意。有n个星球,编号从1~n,现在你从1开始需要访问其他所有的星球。给你Txy表示从x到y的时间,再给n-1个Deadline表示第一次到星球2~n的最迟时间不能超过所给的Deadline。现在问你是否能在要求内访问所有的星球,如果可以输出最小的时间,否则输出-1。(3<= n <= 30 )
输入数据多组。第一行n表示有n个星球,接下来n行每行n列,第i行第j列的Tij表示从i到j的耗时。
接下来一行n-1个数,表示Deadline[ 2 ]~Deadline[ n ]。

题目分析:既然星球数不超过30,就意味着本题通过搜索加一些剪枝便可以过。
首先floyd预处理出每两个点间的耗时(注意x->y及y->x的耗时可能不同),这样接下来我们进行DFS的时候每次只用选取没走过的点增广即可。然后答案就是所有星球走遍后不同可行方案的最小值。
那么不加剪枝是一定会超时的,30^30怎么看都不太可能。
怎么剪枝?我们很容易可以想到,如果当前应该选取的点 j 和之前方案选取的最后一个点 i 之间的耗时Tij乘以剩下要去的星球数(因为现在选了的边在之后都是起作用的)+方案之前所用的时间之和tottime大于已经得到的结果ans,那么直接跳过,因为接下来已经不可能得到最优解了,继续枚举下一个点。
但是光这个剪枝实在太弱,提交依旧会超时。
怎么办?
其实还有一个超级强力的剪枝:如果方案选取最后一个点是 i 走到未走过的当前点 j 的耗时time + Tij > Deadline[ j ],接下来不管怎么走都是不可能得到解的(因为接下来继续DFS下去时间是递增的,但是已经存在有一个星球当前无法走到了,那么之后一定也不会走的到),直接返回上一层枚举 i 。
加了这个以后,基本是OK了的,如果再加入其他什么的优化估计效率会高很多。
继续研究~

代码如下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std ;

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

const int MAXN = 30 ;
const int INF = 3000000 ;
const int MAXH = 1000000 ;

int n ;
int T[MAXN][MAXN] ;
int Deadline[MAXN] ;
bool vis[MAXN] ;
int ans ;

void dfs ( int u , int cnt , int time , int tottime ) {
	if ( !cnt ) {
		if ( ans > tottime )
			ans = tottime ;
		return ;
	}
	REP ( i , n )
		if ( !vis[i] && time + T[u][i] > Deadline[i] )
			return ;
	REP ( i , n ) {
		if ( vis[i] || tottime + cnt * T[u][i] >= ans )
			continue ;
		vis[i] = 1 ;
		dfs ( i , cnt - 1 , time + T[u][i] , tottime + cnt * T[u][i] ) ;
		vis[i] = 0 ;
	}
}

void work () {
	while ( ~scanf ( "%d" , &n ) ) {
		clear ( vis , 0 ) ;
		ans = INF ;
		REP ( i , n )
			REP ( j , n )
				scanf ( "%d" , &T[i][j] ) ;
		REPF ( i , 1 , n - 1 )
			scanf ( "%d" , &Deadline[i] ) ;
		REP ( k , n )
			REP ( i , n )
				REP ( j , n )
					T[i][j] = min ( T[i][j] , T[i][k] + T[k][j] ) ;
		vis[0] = 1 ;
		dfs ( 0 , n - 1 , 0 , 0 ) ;
		if ( ans == INF )
			ans = -1 ;
		printf ( "%d\n" , ans ) ;
	}
}

int main () {
	work () ;
	return 0 ;
}

最后给个我自己YY的双向链表版的,也是看到大神用了才想到去用用:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std ;

#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define clear( a , x ) memset ( a , x , sizeof a )
#define GO( i ) for ( int i = R[0] ; i ; i = R[i] )

const int MAXN = 30 ;
const int INF = 3000000 ;
const int MAXH = 1000000 ;

int n ;
int T[MAXN][MAXN] ;
int Deadline[MAXN] ;
int ans ;
int R[MAXN + 1] , L[MAXN + 1] ;

void dfs ( int u , int cnt , int time , int tottime ) {
	if ( !cnt ) {
		if ( ans > tottime )
			ans = tottime ;
		return ;
	}
	GO ( i )
		if ( time + T[u][i] > Deadline[i] )
			return ;
	GO ( i ) {
		int tmp = tottime + cnt * T[u][i] ;
		if ( tmp >= ans )
			continue ;
		L[R[i]] = L[i] , R[L[i]] = R[i] ;//删除i
		dfs ( i , cnt - 1 , time + T[u][i] , tmp ) ;
		L[R[i]] = R[L[i]] = i ;//恢复i
	}
}

void work () {
	while ( ~scanf ( "%d" , &n ) ) {
		ans = INF ;
		REP ( i , n )
			REP ( j , n )
				scanf ( "%d" , &T[i][j] ) ;
		REP ( i , n - 1 )
			scanf ( "%d" , &Deadline[i + 1] ) ;
		REP ( i , n )
			R[i] = ( i + 1 ) % n , L[( i + 1 ) % n] = i ;
		REP ( k , n )
			REP ( i , n )
				REP ( j , n )
					T[i][j] = min ( T[i][j] , T[i][k] + T[k][j] ) ;
		//L[R[1]] = n - 1 , R[L[1]] = 1 ;题目从1~n,我的实现是从0~n-1,所以不用删除就默认1(0)已经被删除了。
		dfs ( 0 , n - 1 , 0 , 0 ) ;
		if ( ans == INF )
			ans = -1 ;
		printf ( "%d\n" , ans ) ;
	}
}

int main () {
	work () ;
	return 0 ;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值