hdu 4568 Hunter (旅行商问题)

81 篇文章 2 订阅

Hunter

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


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
 

Source


思路:
蛮裸的旅行商问题,将珠宝抽象出来,bfs预处理珠宝间的距离,然后状压dp求答案就够了。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
//#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 205
#define MAXN 100005
#define mod 1000000000
#define INF 0x3f3f3f3f
using namespace std;

int n,m,ans,k,cnt,est;
int dp[1<<14][14];
int u[14],v[14];
int id[maxn][maxn],dist[14][14];
bool vis[maxn][maxn];
int mp[maxn][maxn];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
struct Node
{
    int x,y,step;
    bool operator <(const Node&x1) const
    {
        return step>x1.step;
    }
}cur,now;
priority_queue<Node>q;

void bfs(int bx,int by,int xx)
{
    int i,j,nx,ny,tx,ty,flag=0;
    memset(vis,0,sizeof(vis));
    while(!q.empty()) q.pop();
    cur.x=bx;
    cur.y=by;
    cur.step=0;
    vis[bx][by]=1;
    q.push(cur);
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        nx=now.x;
        ny=now.y;
        if(id[nx][ny]!=-1) dist[xx][id[nx][ny]]=now.step;
        for(i=0;i<4;i++)
        {
            tx=nx+dx[i];
            ty=ny+dy[i];
            if(tx<1||tx>n||ty<1||ty>m)
            {
                if(!flag) flag=1,dp[1<<xx][xx]=now.step+mp[u[xx]][v[xx]];
                continue ;
            }
            if(vis[tx][ty]||mp[tx][ty]==-1) continue ;
            cur.x=tx;
            cur.y=ty;
            cur.step=now.step+mp[tx][ty];
            vis[tx][ty]=1;
            q.push(cur);
        }
    }
}
void solve()
{
    int i,j,p,ma,st;
    ma=1<<k;
    for(i=0;i<ma;i++)
    {
        for(j=0;j<k;j++)
        {
            if(dp[i][j]>=INF) continue ;
            for(p=0;p<k;p++)
            {
                if(p==j||(i&(1<<p))) continue ;
                st=i|(1<<p);
                dp[st][p]=min(dp[st][p],dp[i][j]+dist[j][p]);
            }
        }
    }
    ans=INF;
    for(i=0;i<k;i++)
    {
        ans=min(ans,dp[est][i]+dp[1<<i][i]-mp[u[i]][v[i]]);
    }
    if(ans>=INF) ans=0;
}
int main()
{
    int i,j,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                scanf("%d",&mp[i][j]);
            }
        }
        memset(id,-1,sizeof(id));
        memset(dp,0x3f,sizeof(dp));
        scanf("%d",&k);
        for(i=0;i<k;i++)
        {
            scanf("%d%d",&u[i],&v[i]);
            u[i]++,v[i]++;
            id[u[i]][v[i]]=i;
        }
        est=(1<<k)-1;
        for(i=0;i<k;i++)
        {
            if(mp[u[i]][v[i]]==-1)  // 汗 去掉也能A
            {
                est=est&(~(1<<i));
                continue ;
            }
            bfs(u[i],v[i],i);
            if(dp[1<<i][i]==INF) est=est&(~(1<<i));  // 汗 去掉也能A  没有设track
        }
        solve();
        printf("%d\n",ans);
    }
    return 0;
}



 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值