BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】

称号:Harry And Dig Machine

哈哈  最终涨边粉色了,不easy呀。顺便写一道题解吧

题意:给一个m*n的矩阵,然后当中最多由10个有值,求总左上角把全部的值都拿上回到左上角的最小步数。

标准的TSP回到原点问题,须要先预处理出图来。然后TSP就可以。

AC代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
const int inf = 0x3f3f3f3f;
const int N = 15;
int mp[N][N];
struct Node
{
    int x,y;
};
vector<Node> vv;
int n,m;
int dp[1<<N][N];
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++){
                int x;
                scanf("%d",&x);
                if(x)
                    vv.push_back((Node){i,j});
            }
        }
        int okk = 0;
        for(int i=0;i<vv.size();i++)
        {
            if(vv[i].x==0 && vv[i].y==0)
            {
                okk=1;
                continue;
            }
        }
        if(okk==0)
            vv.push_back((Node){0,0});
        for(int i=0;i<vv.size();i++)
        {
            for(int j=0;j<vv.size();j++)
            {
                mp[i][j] = 0;
                if(i==j)
                    continue;
                mp[i][j] = abs(vv[i].x-vv[j].x) + abs(vv[i].y-vv[j].y);
            }
        }
        int len = vv.size();
        n = len;
        for(int st=0;st<(1<<n);st++)  //TSP
        {
            for(int i=0;i<n;i++)
            {
                if((st&(1<<i))==0)  //?0
                    continue;
                if(st==(1<<i)){
                    dp[st][i]=mp[0][i];continue;
                }
                dp[st][i]=inf;
                for(int j=0;j<n;j++)
                {
                    if((st&(1<<j)) && i!=j)//?1
                    {
                        dp[st][i]=min(dp[st&~(1<<i)][j]+mp[j][i],dp[st][i]);
                    }
                }
            }
        }
        int ans=inf;
        for(int i=0;i<n;i++){
            ans=min(ans,dp[(1<<n)-1][i]+mp[i][0]);
        }
        printf("%d\n",ans);
        vv.clear();
    }
    return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

时间: 2024-11-08 17:36:25

BestCoder Round #14 B 称号 Harry And Dig Machine 【TSP】的相关文章

BestCoder Round #14 B 题 Harry And Dig Machine 【TSP】

题目:Harry And Dig Machine 哈哈  终于涨边粉色了,不容易呀.顺便写一道题解吧 题意:给一个m*n的矩阵,然后其中最多由10个有值,求总左上角把所有的值都拿上回到左上角的最小步数. 标准的TSP回到原点问题,需要先预处理出图来,然后TSP即可. AC代码: #include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <io

hdu 5067 Harry And Dig Machine(BestCoder Round #14)

Harry And Dig Machine Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 332    Accepted Submission(s): 104 Problem Description As we all know, Harry Porter learns magic at Hogwarts School. Howeve

BestCoder Round #14

Harry And Physical Teacher Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 38    Accepted Submission(s): 34 Problem Description As we all know, Harry Porter learns magic at Hogwarts School. How

hdu 5066 Harry And Physical Teacher(Bestcoder Round #14)

Harry And Physical Teacher Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 91    Accepted Submission(s): 68 Problem Description As we all know, Harry Porter learns magic at Hogwarts School. How

hdu5067Harry And Dig Machine(TSP旅行商问题)

题目链接: huangjing 题意:给出一幅图,图中有一些点,然后从第1个点出发,然后途径所有有石头的点,最后回到原点,然后求最小距离.当初作比赛的时候不知道这就是旅行商经典问题.回来学了一下. 思路: 状态转移方程 DP[k][i|base[k]]=min(DP[k][i|base[k]],DP[j][i]+dis[j][k]) DP[J][I]表示从起点到j点在i状态下的最小距离...DP[j][i]+dis[j][k]表从j到k的距离...时间复杂度是(n?m+(t2)?(2t)),那么

HDU 5067 Harry And Dig Machine(BestCoder Round #14)

Problem Description: As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as

HDOJ 5067 Harry And Dig Machine 状压DP

状压DP....dp[i][j]已经走过的点的状态,目前再j点的最小距离 Harry And Dig Machine Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 560    Accepted Submission(s): 210 Problem Description As we all know, Harry Porter le

HDU 5067 Harry And Dig Machine(状压dp)

感觉这两天怎么老是遇到状压啊.... 数字20以下,首想状压啊... 不过这题犯抽忘记考虑没有石头的时候了啊. 简单的状压:表示状态为j时以第i的作为结束. PS:这题也在表扬大蓝翔的挖掘机技术啊.醉了啊... Harry And Dig Machine Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 468    Accepted S

BestCoder Round #11 (Div. 2) 题解

HDOJ5054 Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 302    Accepted Submission(s): 229 Problem Description Bob and Alice got separated in the Square, they agreed that if they