hdu4568 Hunter


Hunter

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


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.
hdu4568 Hunter_第1张图片
 

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
2013 ACM-ICPC长沙赛区全国邀请赛——题目重现
这题,我们必须要从宝藏向外搜,否刚一定会超时,先用bfs,搜出,宝藏到边界要用的路程,和宝藏与宝藏之间的路程,这样,我们就可以转化成了dp,相当于,是一个行商问题
,这样就可以解决了!
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
#define MAXN 230
#define inf 100000000
#define M 1<<14
struct node {
    int step,x,y;
    bool operator <(node a)const{return step>a.step;}
};
priority_queue<node> q;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int ask,dp[M][MAXN],n,m,visit[MAXN][MAXN],valmap[MAXN][MAXN],map[MAXN][MAXN],diso[MAXN],disp[MAXN][MAXN],valx[MAXN],valy[MAXN];
int fmin(int a,int b){if(a<b)return a;return b;}
void bfs(int start)
{
    memset(visit,0,sizeof(visit));
    int s=0,e=0,valnum=1,i;bool outcan=false;
    while(!q.empty())
    q.pop();
    node qfront,temp;
    temp.step=0,temp.x=valx[start],temp.y=valy[start];
    if(temp.x==0||temp.x==n-1||temp.y==0||temp.y==m-1)
    {
         outcan=true;diso[start]=0;
    }
    visit[valx[start]][valy[start]]=1;
    q.push(temp);
    while(!q.empty())
    {
        qfront=q.top();
        q.pop();
        temp=qfront;
        if((temp.x==0||temp.x==n-1||temp.y==0||temp.y==m-1))
        {
            diso[start]=fmin(diso[start],temp.step);
            outcan=true;
        }
        if(valmap[temp.x][temp.y]!=-1)
        {
            valnum++;
            disp[start][valmap[temp.x][temp.y]]=temp.step;
        }
        for(i=0;i<4;i++)
        {
           temp=qfront;
           temp.x+=dir[i][0],temp.y+=dir[i][1];
           if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<m&&!visit[temp.x][temp.y]&&map[temp.x][temp.y]!=-1)
           {
               visit[temp.x][temp.y]=1;
               temp.step+=map[temp.x][temp.y];
               q.push(temp);
           }
        }
    }
}
int main()
{
    int tcase,i,j,all,k;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%d%d",&n,&m);
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
                scanf("%d",&map[i][j]);
        }
        scanf("%d",&ask);
        all=(1<<ask)-1;
        for(i=0;i<=ask;i++)
        {
            diso[i]=inf;
            for(j=0;j<=ask;j++)
            {
                if(i!=j)
                disp[i][j]=inf;
                else
                disp[i][j]=0;
            }
        }
        for(i=0;i<=all;i++)
           for(j=0;j<ask;j++)
            dp[i][j]=inf;
        memset(valmap,-1,sizeof(valmap));
        for(i=0;i<ask;i++)
        {
            scanf("%d%d",&valx[i],&valy[i]);
            valmap[valx[i]][valy[i]]=i;
        }
        for(i=0;i<ask;i++)
            bfs(i);
        for(i=0;i<ask;i++)
            dp[1<<i][i]=diso[i]+map[valx[i]][valy[i]];
        for(i=0;i<=all;i++)
        {
            for(j=0;j<ask;j++)
            {
                if(dp[i][j]==inf)
                continue;
                if((i&(1<<j))==0)
                continue;
                for(k=0;k<ask;k++)
                {
                    if(i&(1<<k))
                    continue;
                    dp[i|(1<<k)][k]=fmin(dp[i|(1<<k)][k],dp[i][j]+disp[j][k]);
                }
            }
        }
        int maxx=inf;
        for(i=0;i<ask;i++)
        {
            maxx=fmin(maxx,dp[all][i]+diso[i]);
        }
        if(maxx!=inf)
        printf("%d\n",maxx);
        else
        printf("0\n");
    }
    return 0;
}

 

你可能感兴趣的:(hdu4568 Hunter)