Hdu 4568 Hunter(状压dp)

题目链接

Hunter

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


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

题意:略。

题解:因为K<=13,所以预处理每个宝石所在的点到其它宝石所在的点的花费,剩下的问题就是TSP了。

代码如下:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>
#include<string>
#include<set>
#include<map>
#define nn 610000
#define inff 0x3fffffff
using namespace std;
typedef __int64 LL;
typedef unsigned __int64 ULL;
int n,m;
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int tu[210][210];
int dis[210][210];
int k;
int x[20],y[20];
struct node
{
    int h,l;
    node(){}
    node(int hh,int ll)
    {
        h=hh,l=ll;
    }
};
queue<node>que;
bool inque[210][210];
void spfa(int x,int y)
{
    int i,j;
    for(i=0;i<=n+1;i++)
    {
        for(j=0;j<=m+1;j++)
        {
            inque[i][j]=false;
            dis[i][j]=inff;
        }
    }
    dis[x][y]=0;
    que.push(node(x,y));
    node sta;
    int dx,dy;
    while(que.size())
    {
        sta=que.front();
        que.pop();
        inque[sta.h][sta.l]=false;
        for(i=0;i<4;i++)
        {
            dx=sta.h+dir[i][0],dy=sta.l+dir[i][1];
            if(dx>=1&&dx<=n&&dy>=1&&dy<=m&&tu[dx][dy]!=-1)
            {
                if(dis[dx][dy]>dis[sta.h][sta.l]+tu[dx][dy])
                {
                    dis[dx][dy]=dis[sta.h][sta.l]+tu[dx][dy];
                    if(!inque[dx][dy])
                    {
                        inque[dx][dy]=true;
                        que.push(node(dx,dy));
                    }
                }
            }
            else if(dis[dx][dy]>dis[sta.h][sta.l])
                dis[dx][dy]=dis[sta.h][sta.l];
        }
    }
}
int bao[20][20];
int dp[20][(1<<13)+10];
int main()
{
    int t,i,j,e;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(tu,0,sizeof(tu));
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                scanf("%d",&tu[i][j]);
            }
        }
        scanf("%d",&k);
        for(i=0;i<k;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            x[i]++,y[i]++;
        }
        for(i=0;i<k;i++)
        {
            spfa(x[i],y[i]);
            for(j=0;j<k;j++)
            {
                bao[i][j]=dis[x[j]][y[j]];
            }
            bao[i][k]=inff;
            for(j=1;j<=n;j++)
            {
                bao[i][k]=min(bao[i][k],dis[j][0]);
                bao[i][k]=min(bao[i][k],dis[j][m+1]);
            }
            for(j=1;j<=m;j++)
            {
                bao[i][k]=min(bao[i][k],dis[0][j]);
                bao[i][k]=min(bao[i][k],dis[n+1][j]);
            }
            bao[k][i]=bao[i][k]+tu[x[i]][y[i]];
        }
        for(i=0;i<k;i++)
        {
            for(j=0;j<(1<<k);j++)
                dp[i][j]=inff;
        }
        for(i=0;i<k;i++)
        {
            dp[i][(1<<i)]=bao[k][i];
        }
        int ix;
        for(j=0;j<(1<<k);j++)
        {
            for(i=0;i<k;i++)
            {
                if(dp[i][j]<inff)
                {
                    for(e=0;e<k;e++)
                    {
                        if((1<<e)&j)
                            continue;
                        ix=j+(1<<e);
                        if(dp[e][ix]>dp[i][j]+bao[i][e])
                        {
                            dp[e][ix]=dp[i][j]+bao[i][e];
                        }
                    }
                }
            }
        }
        int ans=inff;
        for(i=0;i<k;i++)
        {
            ans=min(ans,dp[i][(1<<k)-1]+bao[i][k]);
        }
        if(ans==inff)
            ans=0;
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值