Hunter
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1021 Accepted Submission(s): 289
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
先对每个必须经过的点SPFA一下到其他必须经过的点和边界,然后将这些点独立出来,就转化成旅行商问题,用状压dp来求解
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#define inf 100000000
using namespace std;
int map[205][205];
int dis[205][205];
int vis[205][205];
int qqq[205][205];
int dd[20][20];
int a[4]={1,-1,0,0};
int b[4]={0,0,1,-1};
int dp[20][1<<15];
struct pi
{
int x;
int y;
int cost;
}pp[15];
queue<pi>q;
int k;
int min(int a,int b)
{
int p;
if(a<b)
p=a;
else
p=b;
return p;
}
void bfs(int n,int m,int cur)
{
int i,x,y;
struct pi pp1,pp2;
while(!q.empty())
q.pop();
pp1.x=pp[cur].x;
pp1.y=pp[cur].y;
pp1.cost=0;
q.push(pp1);
memset(dis,0x3f,sizeof(dis));
dis[pp1.x][pp1.y]=0;
while(!q.empty())
{
pp1=q.front();
q.pop();
if(pp1.x==0||pp1.y==0||pp1.x==n-1||pp1.y==m-1)
{
dd[k+1][cur]=min(dd[k+1][cur],dis[pp1.x][pp1.y]);
dd[cur][k+1]=dd[k+1][cur];
}
x=pp1.x;
y=pp1.y;
if(qqq[x][y])
{
dd[cur][qqq[x][y]]=min(dd[cur][qqq[x][y]],dis[x][y]);
}
if(pp1.cost!=dis[pp1.x][pp1.y])
continue;
for(i=0;i<4;i++)
{
x=pp1.x+a[i];
y=pp1.y+b[i];
if(x<0||y<0||x>=n||y>=m)
continue;
if(map[x][y]==-1)
continue;
if(dis[x][y]>dis[pp1.x][pp1.y]+map[x][y])
{
dis[x][y]=map[x][y]+dis[pp1.x][pp1.y];
pp2.x=x;
pp2.y=y;
pp2.cost=dis[x][y];
q.push(pp2);
}
}
}
}
int main()
{
int n,m,i,j,t,f,maxn,ss;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&map[i][j]);
qqq[i][j]=0;
}
}
memset(dd,0x3f,sizeof(dd));
scanf("%d",&k);
ss=0;
for(i=1;i<=k;i++)
{
scanf("%d%d",&pp[i].x,&pp[i].y);
if(map[pp[i].x][pp[i].y]==-1)
ss=1;
qqq[pp[i].x][pp[i].y]=i;
}
for(i=1;i<=k;i++)
{
bfs(n,m,i);
}
memset(dp,0x3f,sizeof(dp));
for(i=0;i<k;i++)
{
dp[i][1<<(i)]=dd[i+1][k+1]+map[pp[i+1].x][pp[i+1].y];
}
for(j=1;j<1<<k;j++)
{
for(i=0;i<k;i++)
{
if((j>>i)&1)
{
for(f=0;f<k;f++)
{
if(f==i)
continue;
if((j>>f)&1)
{
dp[i][j]=min(dp[i][j],dp[f][j-(1<<i)]+dd[f+1][i+1]);
}
}
}
}
}
maxn=inf;
for(i=0;i<k;i++)
{
maxn=min(maxn,dp[i][(1<<k)-1]+dd[i+1][k+1]);
}
printf("%d\n",maxn);
}
return 0;
}