hdu 4568 Hunter (旅行商问题)

本文介绍了一种解决特定旅行商问题的方法,通过预处理不同点间的最短路径,并使用状态压缩来寻找经过所有指定点的最小代价路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

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
题意:给你一张n*m的图,经过每个点都会花费一定的值,当值为-1时代表不能经过,再给你k个宝藏的坐标位置(从0开始的坐标),你可以从这张图边缘任一点作为起始地进入地图,经过所有的宝藏坐标后从边缘任一点出来,问你最小的值,如果所有的宝藏坐标不能走完输出0。
分析:做的时候,我看了下k的大小是13,一想就是旅行商问题,把这13个点走完花费的最小值,但是点与点之间的最小花费需要预处理,由于自己太懒,没有去研究更好的预处理方式,所以预处理太过繁琐,看着就烦,还好A了,预处理时建一个超级起点,然后预处理超级起点到各个宝藏位置的最小值(进去),在预处理所有宝藏到超级起点的最小值(出来),再预处理所有宝藏之间的最小值,最后状压跑就行了。
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
#define nn 220
#define inff 0x3f3f3f
using namespace std;
int a[nn][nn],n,m,K;
int id[nn][nn],ma[15][15];
int p[nn*nn],num;
struct node
{
    int en,next,len;
}E[nn*nn*5];
void init()
{
    num=0;
    memset(p,-1,sizeof(p));
}
void add(int st,int en,int len)
{
    E[num].en=en;
    E[num].len=len;
    E[num].next=p[st];
    p[st]=num++;
}
int dis[nn*nn];
bool vis[nn*nn];
struct Node
{
    int id,val;
    friend bool operator<(Node x,Node y)
    {
        return x.val>y.val;
    }
}sta,tem;
void dij(int st)
{
    priority_queue<Node> que;
    int i;
    for(i=0;i<nn*nn;i++) dis[i]=inff,vis[i]=false;
    dis[st]=0;
    tem.id=st,tem.val=0;
    que.push(tem);
    while(que.size())
    {
        sta=que.top();
        que.pop();
        if(dis[sta.id]<sta.val) continue;
        for(i=p[sta.id];i+1;i=E[i].next)
        {
            int w=E[i].en;
            if(dis[w]>dis[sta.id]+E[i].len)
            {
                dis[w]=dis[sta.id]+E[i].len;
                tem.id=w,tem.val=dis[w];
                que.push(tem);
            }
        }
    }
}
int x[15],y[15];
int dp[9000][15];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                scanf("%d",&a[i][j]);
        int k=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            id[i][j]=++k;
        init();
        for(int i=1;i<=n;i++)
        {
            if(a[i][1]!=-1)
            {
                add(0,id[i][1],a[i][1]);
                add(id[i][1],0,0);
            }
            if(a[i][m]!=-1)
            {
                add(0,id[i][m],a[i][m]);
                add(id[i][m],0,0);
            }
        }
        for(int j=2;j<m;j++)
        {
            if(a[1][j]!=-1)
            {
                add(0,id[1][j],a[1][j]);
                add(id[1][j],0,0);
            }
            if(a[n][j]!=-1)
            {
                add(0,id[n][j],a[n][j]);
                add(id[n][j],0,0);
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]==-1) continue;
                if(j>1&&a[i][j-1]!=-1) add(id[i][j],id[i][j-1],a[i][j-1]);
                if(j<m&&a[i][j+1]!=-1) add(id[i][j],id[i][j+1],a[i][j+1]);
                if(i>1&&a[i-1][j]!=-1) add(id[i][j],id[i-1][j],a[i-1][j]);
                if(i<n&&a[i+1][j]!=-1) add(id[i][j],id[i+1][j],a[i+1][j]);
            }
        }
        dij(0);
        scanf("%d",&K);
        for(int i=0;i<=K;i++)
            for(int j=0;j<=K;j++)
            ma[i][j]=inff;
        for(int i=1;i<=K;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            x[i]++,y[i]++;
        }
        for(int i=1;i<=K;i++)
            ma[0][i]=dis[id[x[i]][y[i]]];
        for(int i=1;i<=K;i++)
        {
            dij(id[x[i]][y[i]]);
            ma[i][0]=dis[0];
            for(int j=1;j<=K;j++)
            {
                ma[i][j]=dis[id[x[j]][y[j]]];
            }
        }
        init();///重新建图是因为超级起点会影响点与点直接的距离(只是我这个本方法有会影响)
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]==-1) continue;
                if(j>1&&a[i][j-1]!=-1) add(id[i][j],id[i][j-1],a[i][j-1]);
                if(j<m&&a[i][j+1]!=-1) add(id[i][j],id[i][j+1],a[i][j+1]);
                if(i>1&&a[i-1][j]!=-1) add(id[i][j],id[i-1][j],a[i-1][j]);
                if(i<n&&a[i+1][j]!=-1) add(id[i][j],id[i+1][j],a[i+1][j]);
            }
        }
        for(int i=1;i<=K;i++)
        {
            dij(id[x[i]][y[i]]);
            for(int j=1;j<=K;j++)
            {
                ma[i][j]=dis[id[x[j]][y[j]]];
            }
        }
        int N=(1<<K);
        for(int i=0;i<N;i++)
            for(int j=0;j<K;j++)
            dp[i][j]=inff;
        for(int i=0;i<K;i++) dp[(1<<i)][i]=ma[0][i+1];
        int ans=inff;
        for(int i=1;i<N;i++)
        {
            int flog=1;
            for(int j=0;j<K;j++)
            {
                if(!(i&(1<<j)))
                {
                    flog=0;
                    continue;
                }
                for(int k=0;k<K;k++)
                {
                    if(j==k||!(i&(1<<k))) continue;
                    dp[i][j]=min(dp[i][j],dp[i^(1<<j)][k]+ma[k+1][j+1]);
                }
            }
            if(flog)
            {
                for(int j=0;j<K;j++) ans=min(ans,dp[i][j]+ma[j+1][0]);
            }
        }
        if(ans==inff) puts("0");
        else printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值