Hunter
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1110 Accepted Submission(s): 312
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
先A*搜索求出每个点之间的最短路,把矩阵外面看成第0点,所以一共有k+1个点,注意是有向图!!!!!从a进入b,和b进入a不一样。
然后就是TSP。。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <math.h>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define clr(a,v) memset(a,v,sizeof a)
#define max(a,b) (a<b?b:a)
#define min(a,b) (a<b?a:b)
#define abs(a) ((a)<0?-(a):(a))
#define L t<<1
#define R t<<1|1
#define MID int mid=(l+r)>>1
inline int input(){
int ret=0;bool isN=0;char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') isN=1;
c=getchar();
}
while(c>='0' && c<='9'){
ret=ret*10+c-'0';
c=getchar();
}
return isN?-ret:ret;
}
inline void output(int x){
if(x<0){
putchar('-');x=-x;
}
int len=0,data[11];
while(x){
data[len++]=x%10;x/=10;
}
if(!len) data[len++]=0;
while(len--)
putchar(data[len]+48);
putchar('\n');
}
const int N=14;
const int MAXN=210;
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
int map[MAXN][MAXN];
int dis[N][N];
int id[MAXN][MAXN];
int dp[1<<N][N];
int t,n,m,k,x[N],y[N];
bool vis[N];
bool has[MAXN][MAXN];
struct node{
int x,y,dis;
bool operator < (const node &a) const{
return dis>a.dis;
}
};
priority_queue<node>q;
inline void bfs(int x,int y){
while(!q.empty()) q.pop();
clr(vis,0);clr(has,0);
node vn,vw;
vn.x=x,vn.y=y,vn.dis=0;
has[x][y]=1;
q.push(vn);
int num=0;
while(!q.empty()){
vn=q.top();q.pop();
if(vn.x==-1 || vn.x==n || vn.y==-1 || vn.y==m){
if(!vis[0]){
dis[id[x][y]][0]=vn.dis;
vis[0]=1;
num++;
}
continue;
}
else if(id[vn.x][vn.y]!=-1 && !vis[id[vn.x][vn.y]]){
dis[id[x][y]][id[vn.x][vn.y]]=vn.dis;
vis[id[vn.x][vn.y]]=1;
num++;
}
if(num == k+1) break;
rep(i,0,4){
int a=vn.x+dx[i],b=vn.y+dy[i];
if(a>=0&&a<n&&b>=0&&b<m&&!has[a][b]&&map[a][b]!=-1){
vw.x=a,vw.y=b,vw.dis=vn.dis+map[a][b];
has[a][b]=1;
q.push(vw);
}
else if(a==-1 || a==n || b==-1 || b==m){
vw.x=a,vw.y=b,vw.dis=vn.dis;
q.push(vw);
}
}
}
}
int main(){
t=input();
rep(ca,0,t){
n=input();m=input();
rep(i,0,n) rep(j,0,m) map[i][j]=input();
clr(id,-1);k=input();
rep(i,1,k+1){
x[i]=input(),y[i]=input();
id[x[i]][y[i]]=i;
}
clr(dis,0);
rep(i,1,k+1){
bfs(x[i],y[i]);
}
rep(i,1,k+1){
dis[0][i]=dis[i][0]+map[x[i]][y[i]];
}
/* rep(i,0,k+1){
rep(j,0,k+1){
printf("%d ",dis[i][j]);
}
printf("\n");
}*/
clr(dp,-1);
k++;
rep(i,0,k) dp[1<<i][i]=dis[0][i];
int all=(1<<k);
rep(i,0,all){
rep(j,0,k){
if(i&(1<<j) && dp[i][j]!=-1){
rep(r,0,k){
if(!(i&(1<<r))){
int val=dp[i][j]+dis[j][r];
int nxt=(i|(1<<r));
if(dp[nxt][r]==-1 || val<dp[nxt][r]){
dp[nxt][r]=val;
}
}
}
}
}
}
output(dp[all-1][0]);
}
return 0;
}