Hdu 4568 Hunter【spfa最短路 tsp状态压缩】

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.
<center> </center>
 

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
 

这题太麻烦了==尤其是tsp没好好学的情况下  其中dp[i][j]: 从j出发,经过状态 i 上所有点的最短距离

    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    using namespace std;
    #define MAX 205
    #define inf 99999999
    int n,m;
    struct pos{int x,y;}tr_pos[MAX];
    int map[MAX * MAX],tr,val[14],dis[MAX * MAX];
    int dir[4][2]={0,1,0,-1,-1,0,1,0};
    int g[20][20],dp[1<<14][14];
    int get_id(int x,int y){
        if(x>=0 && x<n && y>=0 && y<m)return x*m+y;
        return n*m;
    }
    void spfa(int x){
        for (int i = 0; i <= n*m; i++)
            dis[i] = inf;//dis数组是对应当前财宝所在位置距外部的最短距离,需要每次都初始化
        dis[x] = (map[x]==-1?inf:map[x]);
        bool inq[205*205];
        memset(inq,0,sizeof(inq));
        queue<int>Q;
        while(!Q.empty())Q.pop();
        Q.push(x);
        inq[x] = 1;
        int now;
        while(!Q.empty()){
            now = Q.front();
            Q.pop();
            inq[now] = 0;
            if(now == n*m)continue;
            int r = now/m;
            int c = now%m;
            int nr,nc,nid;
            for (int i = 0; i < 4; i++)
            {
                nr = r + dir[i][0];
                nc = c + dir[i][1];
                nid = get_id(nr,nc);
                if(map[nid] == -1)continue;
                //更新 财宝点到边界的最短距离。
                if(dis[nid] > dis[now] + map[nid]){
                    dis[nid] = dis[now] + map[nid];
                    if(!inq[nid])   Q.push(nid);
                    inq[nid] = 1;
                }
            }
        }
    }
    int done(){
        for (int i = 0; i < tr; i++)
        {
            spfa(get_id(tr_pos[i].x,tr_pos[i].y));
            for (int j = 0; j < tr; j++)
            {
                int jid = get_id(tr_pos[j].x,tr_pos[j].y);
                g[i][j] = dis[jid];
                if(g[i][j] == inf) return 0;
            }
            g[i][tr] = dis[n*m];//将n*m点看做一个花费为0的财宝点。
        }
        return 1;
    }
    int solve()
    {
        if(tr == 0) return 0;
        int full = 1<<tr;
        for(int i = 0;i<full;i++)
            for(int j=0;j<tr;j++)
                dp[i][j] = inf;
        for (int i = 0; i < tr; i++)
            dp[1<<i][i] = g[i][tr];
        for(int i = 1; i < full; ++i){   //当前状态为 i 状态(用二进制表示经过了哪些财宝点)
        for(int j = 0; j < tr; ++j){
            if(i & (1<<j)){               //保证第j个财宝点在 i 状态中。
        for(int k = 0; k < tr; ++k){
            if( i & (1<<k) && j != k){    //第k个也要在,而且i!= j
                if(dp[i^(1<<k)][j] != inf)
                dp[i][k] = min(dp[i][k], dp[i^(1<<k) ][j] + g[j][k] - val[j]);
            }
            }
        }
        }
        }
        int res = inf;
        for (int i = 0; i < tr; i++)
        {
            res = min(res,dp[full-1][i] + g[i][tr]-val[i]);
            //full-1表示所有财宝点都包含的状态,g[][]表示两财宝点之间的最短距离,因为i的val记了两遍,故减去
        }
        return res;
    }
    int main()
    {
        int cas,i,j;
        scanf("%d",&cas);
        while(cas--){
            scanf("%d%d",&n,&m);
            for(i = 0;i < n;i++)
            for(j = 0;j < m;j++)
                scanf("%d",&map[i*m+j]);
            scanf("%d",&tr);
            for(i = 0;i < tr;i++){
                scanf("%d%d",&tr_pos[i].x,&tr_pos[i].y);
                int id = get_id(tr_pos[i].x,tr_pos[i].y);
                val[i] = map[id];
            }
            map[n*m] = 0;//从外部任意位置进入。

            if(done())printf("%d\n",solve());
            else printf("-1\n");
        }
        return 0;
    }


全部评论

相关推荐

点赞 评论 收藏
分享
09-01 09:00
已编辑
四川旅游学院 运营
牛客55195891...:主要是专业不好,别的没毛病
牛客解忧铺
点赞 评论 收藏
分享
头像
10-22 20:13
中南大学 Java
序言大家好呀。我是希晨er,一个初入职场的程序猿小登最近上班摸鱼刷到了一篇文章:10年深漂,放弃高薪,回长沙一年有感,还有聊聊30岁大龄程序员过往的心路历程,突然就有点感慨。我如今也做出了和大明哥一样的抉择,只是更早。此外我22年的人生,好像从来没好好记录过。正好现在工作不太忙,就想把这些经历写下来,也希望能得到社区里各位前辈的指点个人背景我是03年出生的西安娃,父母都是普通打工人。刚从中南大学软件工程专业毕业半年,现在在老家的央企过着躺平摆烂的日子成长轨迹从农村到城市的童年我家并不是西安的,只是爸妈在西安上班,从小学之后就把我接到了西安。后来老家房子拆了,爷爷奶奶也搬了过来。农村的生活,我觉...
Yki_:看哭了,恋爱那一段你女朋友说你不够关心她,可你毕竟也愿意遇到矛盾写几千字来和她慢慢分析;说不愿意给她花钱,我感觉可能只是消费观不一样;如果她想留在长沙,也应该提前跟你说开。不过她也许会心疼你放弃大厂offer转向数字马力?我也因为同样的原因有过一段幸福而充满遗憾的感情,不过跟爱情相比确实前途更重要一点。至于offer的选择,换我我也会这么选。把这些旧事记录下来以后,接下来就好好向前看吧,加油兄弟
🍊晨光随笔
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务