HDU 4848 Wow! Such Conquering! 深搜+强剪枝

Wow! Such Conquering!

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


 

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

Hint

Explanation: In case #1: The Super Doge travels to Doge Planet 2 at the time of 8 and to Doge Planet 3 at the time of 12, then to Doge Planet 4 at the time of 16. The minimum sum of all arrival time is 36.

 

 

Source

2014西安全国邀请赛

题意:n个点,告诉两两点之间的距离,然后求从1开始到其它所有的点的距离的和。

每一个点有一个时间底线,只有不超过时间底线才有可能到达此点否则无法到达。

输出距离和的最小值。

 

想法:因为有两两点之间的距离,所以可以枚举这个人到达所有点的顺序。有两个剪枝:1.如果当前时刻要到达k点,那么k点的时间底线一定要大于等于当前时刻+map[now][k]。

     2.让总时间先走一步。因为时间是累加的,类似于:

        1

        1+2

        1+2+3

        1+2+3+4

        ............

        1+2+3+4+5+...+n

        显然可以发现1被加了n次,2被加了n-1次,以此类推k被加了n-(k-1)次,所以在计算总时间的时候可以采用这种方法,让总时间先行一步,如果此时的总时间大于已有的总时间答案的话,那么写下来的数都不用再去枚举了。

 

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int INF = 1e9+7;
const int MAXN = 50;
int n,ans;
int dis[MAXN][MAXN],vis[MAXN],dead[MAXN];
void dfs(int u, int t, int cnt,int tmp) {
	if(dead[u]<t || tmp>ans)return;
	if(cnt == n) {
		ans=min(ans,tmp);
		return ;
	}
	for(int i=2; i<=n; i++) {
		int w=dis[u][i],x=dead[i];
		if(!vis[i] && t+w > x)
			return;
	}
	for(int i=2; i<=n; i++)
		if(!vis[i] && dead[i]>=t+dis[u][i]) {
			vis[i]=1;
			dfs(i,t+dis[u][i],cnt+1,tmp+dis[u][i]*(n-cnt));
			vis[i]=0;
		}
}
int main() {
	while(~scanf("%d",&n)) {
		for(int i=1; i<=n; i++)
			for(int j=1; j<=n; j++)
				scanf("%d",&dis[i][j]);
		for(int k=1; k<=n; k++)
			for(int i=1; i<=n; i++)
				for(int j=1; j<=n; j++)
					dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
		dead[1]=INF;
		for(int i=2; i<=n; i++)
			scanf("%d",&dead[i]);
		ans=INF;
		memset(vis,0,sizeof(vis));
		vis[1]=1;
		dfs(1,0,1,0);
		if(ans == INF)printf("-1\n");
		else printf("%d\n",ans);
		/*for(int i=1; i<=n; i++) {
			for(int j=1; j<=n; j++) {
				cout<<dis[i][j]<<" ";
			}
			cout<<endl;
		}*/

	}
	return 0;
}
/*

*/

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值