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): 846    Accepted Submission(s): 255

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

题意:n个点,要求从1点开始跑,遍历所有点,要求到达每个点的时间<=time[i],求到达每个点时间的总和最小。

爆搜+最优性剪枝+可行性剪枝(参考别人的)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define L long long
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=32;
int dis[maxn][maxn];
int n,tim[maxn];
void Floyd()
{
	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]);
}
int vis[maxn],ans;
void dfs(int src,int cur_time,int sum_time,int num)
{
	if(num==n)
	{
		ans=min(ans,sum_time);
		return ;
	}
	//最优性剪枝:当前总时间+当前时间*剩余点数(因为往后到达的点的时间总是小于等于当前时间的)>=当前最优解 剪掉
	if(sum_time+cur_time*(n-num)>=ans)return ;
	//可行性剪枝:如果当前时间有超Deadtime的,剪掉
	for(int i=2;i<=n;i++)
		if(!vis[i]&&cur_time>tim[i])
		return ;
	for(int i=2;i<=n;i++)
	{
		if(src!=i&&!vis[i]&&cur_time+dis[src][i]<=tim[i])
		{
			vis[i]=1;
			dfs(i,cur_time+dis[src][i],sum_time+cur_time+dis[src][i],num+1);
			vis[i]=0;
		}
	}

}
int main()
{
	while(~scanf("%d",&n))
	{
		memset(vis,0,sizeof(vis));
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
			scanf("%d",&dis[i][j]);
		tim[1]=INF;
		for(int i=2;i<=n;i++)
			scanf("%d",&tim[i]);
		Floyd();
		vis[1]=1;
		ans=INF;
		dfs(1,0,0,1);
		if(ans==INF)ans=-1;
		printf("%d\n",ans);
	}
	return 0;
}
时间: 2024-09-30 16:39:11

HDU 4848-Wow! Such Conquering!(DFS+最优性剪枝)的相关文章

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): 0    Accepted Submission(s): 0 Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space

hdu 4848 Wow! Such Conquering!

Wow! Such Conquering! 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 Olymp

HDU 1010 Tempter of Bone DFS + 奇偶剪枝

奇偶剪枝: 对于从起始点 s 到达 终点 e,走且只走 t 步的可达性问题的一种剪枝策略. 如下列矩阵 : 从任意 0 到达 任意 0,所需步数均为偶数,到达任意 1 ,均为奇数.反之亦然 所以有,若s走且只走 t 步可到达e,则必有t >= abs(s.x - e.x) + abs(s.y - e.y),且 (t&1) == ((abs(s.x - e.x) + abs(s.y - e.y))&1). 否则必不可达. #include <iostream> #inclu

HDOJ 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): 651    Accepted Submission(s): 195 Problem Description There are n Doge Planets in the Doge Space. The conquero

HDU-4848 Wow! Such Conquering! (回溯+剪枝)

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.

hdu 5024 Wang Xifeng&#39;s Little Plot【暴力dfs,剪枝】

2014年广州网络赛的C题,也是水题.要你在一个地图中找出一条最长的路,这条路要保证最多只能有一个拐角,且只能为90度 我们直接用深搜,枚举每个起点,每个方向进行dfs,再加上剪枝. 但是如果直接写的话,那一定会特别麻烦,再加上方向这一属性也是我们需要考虑的方面,我们将从别的地方到当前点的方向编一个号:往右为0,如下图顺时针顺序编号 (往右下方向为1,往下为2......以此类推) 我们知道了当前点的坐标,来到当前点的方向,以及到目前有没有拐弯,这几个属性之后,就可以记忆化搜索了,用一个四维数组

HDU 1010-Tempter of the Bone(DFS+奇偶剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 71121    Accepted Submission(s): 19592 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

HDU 1010 Tempter of the Bone(DFS+奇偶性剪枝)

Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 70665    Accepted Submission(s): 19487 Problem Description The doggie found a bone in an ancient maze, which fascinated him a

hdoj--1010&lt;dfs+奇偶剪枝&gt;

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目描述:在n*m的矩阵中,有一起点和终点,中间有墙,给出起点终点和墙,并给出步数,在该步数情况下走到终点,走过的点不能再走: 题目要点:dfs+奇偶剪枝:输入: 本题用dfs可以做出结果,但是会超时,需要用到就剪枝,来减去大部分的可能: 奇偶剪枝: 方格中起点(tx,ty)和终点(dx, dy)最小步骤是minstep=abs(tx-dx)+abs(ty-dy); 给定步数t,从起点走到终点