Hdu 4568 Hunter 2013长沙邀请赛

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

Hunter

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


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
 


题意:给一个n*m的地图,地图里有K个宝藏,地图上每个点的数字代表 开发这个区域要花费的 金钱(重复走过重复计算花费)。Hunter 可以从地图外面任意位置进入地图,然后获得所有宝藏,离开地图,求最小的花费。

代码:

/***********************************************************
如果直接搜索 时间空间复杂度都会太大 
由于K很小 想到所谓取所有的宝藏 只不过是个顺序问题 
所以可以将这个问题转化为旅行商问题  
首先spfa预处理 每个宝藏到其他宝藏的最短路和到边界的最小距离 
剩下的就是TSP过程了 DP 
***********************************************************/
/*
the solution to TSP problem:
dp[i][j]: 从j出发,经过状态 i 上所有点的最短距离
*/
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAX 205
#define inf 99999999
int n,m;
struct pos{int x,y;}tr_pos[MAX];
int map[MAX * MAX],tr,val[14],dis[MAX * MAX];
int dir[4][2]={0,1,0,-1,-1,0,1,0};
int g[20][20],dp[1<<14][14];
int get_id(int x,int y){
	if(x>=0 && x<n && y>=0 && y<m)return x*m+y;
	return n*m;
}
void spfa(int x){	
	for (int i = 0; i <= n*m; i++)
		dis[i] = inf;//dis数组是对应当前财宝所在位置距外部的最短距离,需要每次都初始化
	dis[x] = (map[x]==-1?inf:map[x]);
	bool inq[205*205];
	memset(inq,0,sizeof(inq));
	queue<int>Q;
	while(!Q.empty())Q.pop();
	Q.push(x);
	inq[x] = 1;
	int now;
	while(!Q.empty()){
		now = Q.front();
		Q.pop();
		inq[now] = 0;
		if(now == n*m)continue;
		int r = now/m;
		int c = now%m;
		int nr,nc,nid;
		for (int i = 0; i < 4; i++)
		{
			nr = r + dir[i][0];
			nc = c + dir[i][1];
			nid = get_id(nr,nc);
			if(map[nid] == -1)continue;
			//更新 财宝点到边界的最短距离。
			if(dis[nid] > dis[now] + map[nid]){
				dis[nid] = dis[now] + map[nid];
				if(!inq[nid])	Q.push(nid);
				inq[nid] = 1;
			}
		}
	}
}
int done(){
	for (int i = 0; i < tr; i++)
	{
		spfa(get_id(tr_pos[i].x,tr_pos[i].y));
		for (int j = 0; j < tr; j++)
		{
			int jid = get_id(tr_pos[j].x,tr_pos[j].y);
			g[i][j] = dis[jid];
			if(g[i][j] == inf) return 0;
		}
		g[i][tr] = dis[n*m];//将n*m点看做一个花费为0的财宝点。
	}
	return 1;
}
int solve()
{
	if(tr == 0) return 0;
	int full = 1<<tr;
	for(int i = 0;i<full;i++)
		for(int j=0;j<tr;j++)
			dp[i][j] = inf;
	for (int i = 0; i < tr; i++)
		dp[1<<i][i] = g[i][tr];
	for(int i = 1; i < full; ++i){	//当前状态为 i 状态(用二进制表示经过了哪些财宝点)
    for(int j = 0; j < tr; ++j){
		if(i & (1<<j)){				//保证第j个财宝点在 i 状态中。
	for(int k = 0; k < tr; ++k){
		if( i & (1<<k) && j != k){	//第k个也要在,而且i!= j
			if(dp[i^(1<<k)][j] != inf) 
			dp[i][k] = min(dp[i][k], dp[i^(1<<k) ][j] + g[j][k] - val[j]);
		}
		}
	}
    }
    }
	int res = inf;
	for (int i = 0; i < tr; i++)
	{
		res = min(res,dp[full-1][i] + g[i][tr]-val[i]);
		//full-1表示所有财宝点都包含的状态,g[][]表示两财宝点之间的最短距离,因为i的val记了两遍,故减去
	}
	return res;
}
int main()
{
	int cas,i,j;
	scanf("%d",&cas);
	while(cas--){
		scanf("%d%d",&n,&m);
		for(i = 0;i < n;i++)
		for(j = 0;j < m;j++)
			scanf("%d",&map[i*m+j]);
		scanf("%d",&tr);
		for(i = 0;i < tr;i++){
			scanf("%d%d",&tr_pos[i].x,&tr_pos[i].y);
			int id = get_id(tr_pos[i].x,tr_pos[i].y);
			val[i] = map[id];
		}
		map[n*m] = 0;//从外部任意位置进入。

		if(done())printf("%d\n",solve());
		else printf("-1\n");
	}
	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值