Hunter
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 529 Accepted Submission(s): 153
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.
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
题意:
一个迷宫,里面有一些格子有宝藏。每个格子都有一个值,代表访问这个格子的代价,如果是-1,则表示
不能访问。可以从任意一个格子的外面进和出一次。问带出所有宝藏的最小代价。如果他什么都拿不到,
输出-1.
每个格子可以访问任意次数,因为他不记得自己是否走过。
分析:
就是用松弛操作找出所有宝藏两两之间的最小代价 和 从每个宝藏出边界的最小代价。
然后用动态规划来更新状态和找指定状态。
感想:
虽然敲出来了,但是对dis[u]的更新过程还是理解的很模糊。。。。
虽然这题和poj 3229蛮像的,但是好难理解。因为有宝藏间的最短距离 和 宝藏和边界的最短距离。
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define INF 1e9
using namespace std;
struct Node
{
int id,dis;
Node(){}
Node(int a,int b)
{
id=a,dis=b;
}
bool operator <(const Node &dd) const
{
return dis>dd.dis;
}
}t1;
int map[230][230],g[32][32],dp[20][1<<16];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int node[32],sum,tre[232][232];
int n,m,cos[32],vis[210*210],dis[210*210];
void dij(int st,int pos)
{
int i;
memset(vis,0,sizeof(vis));
priority_queue<Node> Q;
for(i=0;i<sum;i++)
dis[i]=INF;
dis[st]=0;
Q.push(Node(st,0));
Node t1;
while(!Q.empty())
{
t1=Q.top();
Q.pop();
int u=t1.id;
if(vis[u]) continue;
vis[u]=1;
int x=u/m,y=u%m;
if(tre[x][y]!=-1) //如果是其他宝藏,更新当前宝藏到这个宝藏间的距离
g[pos][tre[x][y]]=dis[u];
if(x==n-1||x==0||y==m-1||y==0) //如果是边界,更新宝藏到边界的距离
{
cos[pos]=min(cos[pos],dis[u]);
}
for(int i=0;i<4;i++)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(map[xx][yy]==-1) continue;
if(xx<0||xx>=n||yy<0||yy>=m) continue;
int uu=xx*m+yy;
if(dis[uu]>dis[u]+map[xx][yy])
{
dis[uu]=dis[u]+map[xx][yy];
Q.push(Node(uu,dis[uu]));
}
}
}
}
int main()
{
int T,i,j,k,p,ans,x,y;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
sum=n*m;
memset(tre,-1,sizeof(tre));
memset(dp,0x3f,sizeof(dp));
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
scanf("%d",&map[i][j]);
}
scanf("%d",&k);
for(i=0;i<k;i++)
{
scanf("%d%d",&x,&y);
tre[x][y]=i;
node[i]=x*m+y;
}
memset(g,0x3f,sizeof(g));
for(i=0;i<k;i++)
{
g[i][i]=0;
cos[i]=INF;
dij(node[i],i);
}
for(i=0;i<k;i++)
{
int x1=node[i]/m;
int y1=node[i]%m;
dp[i][1<<i]=map[x1][y1]+cos[i]; //直接访问i点并出边界的代价
}
for(j=0;j<(1<<k);j++)
{
for(i=0;i<k;i++)
{
if(j&(1<<i)&&j!=(1<<i))
{
for(p=0;p<k;p++)
{
if(j&(1<<p)&&j!=(1<<p)&&i!=p)
{
dp[i][j]=min(dp[i][j],dp[p][j-(1<<i)]+g[p][i]);
}
}
}
}
}
int ans=INF;
for(i=0;i<k;i++)
{
ans=min(ans,dp[i][(1<<k)-1]+cos[i]);
}
printf("%d\n",ans);
}
return 0;
}
//AC
//140MS
/*
tre[][]记录宝藏的编号
g[][]记录两个宝藏间的距离
cos[i]记录从i宝藏出边界的代价
node[i]记录宝藏对于整个矩阵的相对位置
*/