hdu4848 DFS 暴搜+ 剪枝 ʕ •ᴥ•ʔ

Wow! Such Conquering!

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


 

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.

扫描二维码关注公众号,回复: 2826707 查看本文章

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 Planet2 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出发遍历所有的点的距离和是多少,但是要在规定的长度内(最后一行输入的数 就是距离 4个点对应三段路),这里的距离和是每一个点到1的距离的总和,不是选择一条遍历所有点的路径的总长度。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define N 0x3f3f3f3f
#define mod 1000000007
int vis[105];
int map[105][105];
int ans;
int s[105];
int n;
void floyd()
{
	for(int k=0;k<n;k++)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
			}
		}
	}
}
void dfs(int now,int ss,int t,int sum)
{
	if(ss==n)//当到达最后一个点时  
	{
		ans=min(ans,sum);
		return ;
	}
	if(sum>ans)
	//如果当前的sum的值已经比之前已经到达的ans到了 直接舍去 
	return ;
	for(int i=1;i<n;i++)
	{
		if(!vis[i]&&t+map[now][i]>s[i]) 
		//当前 t+这个点到i 的距离大于所要求的s[i]值 舍去 
		{
			return ;
		}
	}
	for(int i=1;i<n;i++)
	{
		if(!vis[i])//如果这个点没有放进去  
		{
			vis[i]=1;//放进去 
			dfs(i,ss+1,t+map[now][i],sum+map[now][i]*(n-ss));
			// i代表放进哪个数 ss代表放进去了几个数 
			// t+map[now][i]代表到当前的sum值 用于后面判断s[i]是否满足要求 
			// sum+map[now][i]*(n-ss) 代表我们需要求的最短路
			// 所有的路都要加(n-ss)个当前路的距离(很好理解 细细品味) 
			vis[i]=0;//拿出来 
		}
	}
}
int main()
{
	while(cin>>n)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			cin>>map[i][j];
		}
		memset(vis,0,sizeof(vis));
		for(int i=1;i<n;i++)
		{
			cin>>s[i];
		}
		floyd();//首先用 floyd 排出某点到各个点的最短距离 
		ans=N;
		dfs(0,1,0,0);
		if(ans==N)
		cout<<-1<<endl;
		else
		cout<<ans<<endl;
	}
	return 0;
}

 

猜你喜欢

转载自blog.csdn.net/henucm/article/details/81633157