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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4848

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.

题目大意:一个将军去其他星球检查,给出一个到其他星球的时间图,从第一个星球出发,下面是每个星球的时间限制(要在时间以内到这个星球),输出遍历完的最短距离,如果没有办法按要求遍历完,输出-1;

注意这个样例分析!!它的距离和是所选路线的该星球到起点星球的距离,

样例一:最短的路线是1 -> 3 -> 4->2

那么他经过的路程就是

1-》3:8(直接到)

1-》4:8+2(经过点3到点4)

1-》2:8+2+8(经过点3到点4再(选最短的路)到点2(直接到点2是9,但是绕道点3,再走点2是8,所以选择8)选最短的路)

啊。。。。样例好难懂

然后就好写了:

ac:

//一页 27行就很舒服 
#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 998244353
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);

int n,ans,dis[55][55],tim[55];
bool ves[55];

void dfs(int u,int num,int sum,int ti)
{
	if(num==n)
	{
		ans=min(ans,sum);
		return ;
	}
	if(sum>ans)
		return ;
	for(int i=2;i<=n;++i)
	{//没有找到过 && 到目标点的时间要大于要求的时间 
		if(ves[i]==0&&dis[u][i]+ti>tim[i])
			return ;
	}
	for(int i=2;i<=n;++i)
	{
		if(ves[i]==0)//没有到这过里 
		{
			ves[i]=1;
			dfs(i,num+1,sum+dis[u][i]*(n-num),ti+dis[u][i]);
			ves[i]=0;
		}
	}
}

int main()
{
	std::ios::sync_with_stdio(false);
	while(cin>>n)
	{
		for(int i=1;i<=n;++i)
		{
			for(int j=1;j<=n;++j)
				cin>>dis[i][j];
		}
		for(int i=2;i<=n;++i)
			cin>>tim[i];
		//每个点到其他点的最小值 
		for(int k=1;k<=n;++k)//fff
		{
			for(int i=1;i<=n;++i)
			{
				for(int j=1;j<=n;++j)
				{
					if(dis[i][j]>dis[i][k]+dis[k][j])
						dis[i][j]=dis[i][k]+dis[k][j];
				}
			}
		}
//		for(int i=1;i<=n;++i)
//		{
//			for(int j=1;j<=n;++j)
//				cout<<dis[i][j]<<" ";
//			cout<<endl;
//		}
		ans=INF;
		clean(ves,0);
		dfs(1,1,0,0);//从第一个城市开始走 
		if(ans<INF)
			cout<<ans<<endl;
		else
			cout<<-1<<endl;		
		
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81669758
今日推荐