hdu4568Hunter(BFS+TSP)

Hunter

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 725    Accepted Submission(s): 200


Problem Description
  One day, a hunter named James went to a mysterious area to find the treasures. James wanted to research the area and brought all treasures that he could.
  The area can be represented as a N*M rectangle. Any points of the rectangle is a number means the cost of research it,-1 means James can't cross it, James can start at any place out of the rectangle, and explore point next by next. He will move in the rectangle and bring out all treasures he can take. Of course, he will end at any border to go out of rectangle(James will research every point at anytime he cross because he can't remember whether the point are researched or not).
  Now give you a map of the area, you must calculate the least cost that James bring out all treasures he can take(one point up to only one treasure).Also, if nothing James can get, please output 0.
 

Input
  The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case begins with a line containing 2 integers N M , (1<=N,M<=200), that represents the rectangle. Each of the following N lines contains M numbers(0~9),represent the cost of each point. Next is K(1<=K<=13),and next K lines, each line contains 2 integers x y means the position of the treasures, x means row and start from 0, y means column start from 0 too.
 

Output
  For each test case, you should output only a number means the minimum cost.
 

Sample Input
  
  
2 3 3 3 2 3 5 4 3 1 4 2 1 1 1 3 3 3 2 3 5 4 3 1 4 2 2 1 1 2 2
 

Sample Output
  
  
8 11
 

Source
 

Recommend
zhoujiaqi2010
 

Statistic |  Submit |  Discuss |  Note

刚看到这道题时,我是立马就想到了BFS,因为是图和最短路的结合,这时候用BFS准没错。

但是我就着BFS的常规思路想下去想了一天。没有任何结果。。。

后来看了一下最小的数字是n,就是宝藏的点数才到13,我就知道突破点在这里了。。

后来总结,一般看到一道题当自己完全没思路时,可以按题目最小的数字想。突破点一定在这里,特别是动态规划题。。

然后就把每个节点的最短路径求出来。然后那200*200的迷宫地图对于我们就没用了。

我们只需要是点与点的最短路径。

然后就是有13个点的TSP问题了(外面算一个点)

但是悲哀的是,用BFS把没两个节点最短路径求出来之后就不会做了,如果是枚举 O(N !)复杂度就绝对超时。

然后就上网找TSP的高效解法。选中了动态规划。

后来只能将TSP动态规划的模板原封不动地抄下来。。。

终于A了。

动态规划方程解释:

        假设从顶点s出发,令d(i, V’)表示从顶点i出发经过V’(是一个点的集合)中各个顶点一次且仅一次,最后回到出发点s的最短路径长度。

        推导:(分情况来讨论)

        ①当V’为空集,那么d(i, V’),表示从i不经过任何点就回到s了,如上图的 城市3->城市0(0为起点城市)。此时d(i, V’)=Cis(就是 城市i 到 城市s 的距离)、

        ②如果V’不为空,那么就是对子问题的最优求解。你必须在V’这个城市集合中,尝试每一个,并求出最优解。

           d(i, V’)=min{Cik +  d(k, V’-{k})}

           注:Cik表示你选择的城市和城市i的距离,d(k, V’-{k})是一个子问题。

        综上所述,TSP问题的动态规划方程就出来了:

         image

dp[i][j]的解释:i是一种状态如:i=3 则i为00000000000011,就是说只有点一和点二在集合中。那这样就可以表示V’这个集合了。。。。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
int path[202][202];
int f[202][202],flag[202][202];
int minpath[15][15];
int dir[][2] = {{-1,0},{0,-1},{1,0},{0,1}};
int row,col;
int n;
int pf[15];
int dp[1<<16][20];
struct node
{
	int x;
	int y;
	int min;
	bool operator < (const node& a) const
	{
		return min>a.min;
	}
};
struct node1
{
	int x;
	int y;
}node1[20];
int Min(int a,int b)
{
	return a>b?b:a;
}
void BFS(int x,int y,int num)
{
	memset(flag,0,sizeof(flag));
	priority_queue<node> pq;
	while(!pq.empty())
		pq.pop();
	struct node nn,var;
	nn.x = x;
	nn.y = y;
	nn.min = 0;
	pq.push(nn);
	flag[x][y] = 1;
	while(!pq.empty())
	{
		nn = pq.top();
		pq.pop();
		if(f[nn.x][nn.y]>0)
		{
			int i = f[nn.x][nn.y];
			minpath[num][i] = nn.min;
			pf[num]++;
		}
		if(minpath[num][0]==-1 && (nn.x==1 || nn.x==row || nn.y==1 || nn.y==col))
		{
			minpath[num][0] = nn.min;
			minpath[0][num] = nn.min+path[x][y];
			pf[num]++;
		}
		if(pf[num]>n )
			return ;
		for(int j=0;j<4;j++)
		{
			int xx = nn.x+dir[j][0];
			int yy = nn.y+dir[j][1];
			if(path[xx][yy]!=-1 && flag[xx][yy]==0)
			{
				var.min = nn.min+path[xx][yy];
				var.x = xx;
				var.y = yy;
				pq.push(var);
				flag[xx][yy] = 1;
			}
		}
	}
}
int main()
{
	int T,i,j;
	scanf("%d",&T);
	while(T--)
	{
		memset(path,-1,sizeof(path));
		memset(f,0,sizeof(f));
		memset(minpath,-1,sizeof(minpath));
		scanf("%d%d",&row,&col);
		for(i=1;i<=row;i++)
			for(j=1;j<=col;j++)
			scanf("%d",&path[i][j]);
		
			scanf("%d",&n);
			for(i=1;i<=n;i++)
			{
				scanf("%d%d",&node1[i].x,&node1[i].y);
				node1[i].x++;
				node1[i].y++;
				f[node1[i].x][node1[i].y] = i;
			}
		memset(pf,0,sizeof(pf));
		for(i=1;i<=n;i++)
			BFS(node1[i].x,node1[i].y,i);  //每次求出node1[i]和其他点的最短距离,包括出去的最短距离

		memset(dp,0x3f,sizeof(dp));
		minpath[0][0] = 0;
        if(!n) {printf("0\n");continue;}
 
        for( i=0;i<=n;i++)					//这里到下面都是套用别人的模板
        {
            dp[1<<i][i]=minpath[0][i];      //自己对TSP的动态规划不熟,不能自己写~~~
        }
		int end=(1<<(n+1));
        for( i=0;i<end;i++)
        {
            for( j=0;j<=n;j++)
            {
                if((i>>j)&1)  //满足j要在i状态里面
                {
                    for(int k=0;k<=n;k++)
                    {
                        if((i>>k)&1)  //满足k要在i状态里面
                        {
                            dp[i][j]=Min(dp[i][j],dp[i&(~(1<<j))][k]+minpath[k][j]);
                        }
                    }
                }
            }
        }
        printf("%d\n",dp[end-1][0]);

		
	}
	return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值