Harry And Dig Machine HDU - 5067(TSP旅行商问题)

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 language, mathematics, English, and even algorithm.
Dumbledore, the headmaster of Hogwarts, is planning to construct a new teaching building in his school. The area he selects can be considered as an n*m grid, some (but no more than ten) cells of which might contain stones. We should remove the stones there in order to save place for the teaching building. However, the stones might be useful, so we just move them to the top-left cell. Taking it into account that Harry learned how to operate dig machine in Lanxiang School several years ago, Dumbledore decides to let him do this job and wants it done as quickly as possible. Harry needs one unit time to move his dig machine from one cell to the adjacent one. Yet skilled as he is, it takes no time for him to move stones into or out of the dig machine, which is big enough to carry infinite stones. Given Harry and his dig machine at the top-left cell in the beginning, if he wants to optimize his work, what is the minimal time Harry needs to finish it?
Input
They are sever test cases, you should process to the end of file.
For each test case, there are two integers n and m.(1≤n,m≤50).
The next n line, each line contains m integer. The j-th number of ith line a[i][j] means there are a[i][j] stones on the jth cell of the ith line.( 0≤a[i][j]≤100 , and no more than 10 of a[i][j] will be positive integer).
Output
For each test case, just output one line that contains an integer indicate the minimal time that Harry can finish his job.
Sample Input
3 3
0 0 0
0 100 0
0 0 0
2 2
1 1
1 1
Sample Output
4
4

题意: 从(1,1)出发,经过所有权值大于0的点,最后回到起点,求最小花费。
思路:
经典的TSP(哈密顿回路)问题,定义 dp[sta][k],为状态为sta的时候,当前在点k的情况下的最短距离。
结果就是min{dp[1 << cnt - 1][k] + dist[k][1]}

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;

const int maxn = 1005;
struct Point
{
    int x,y;
}p[maxn];

int dist[55][56],dp[1 << 11][12];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int cnt = 1;
        p[cnt].x = 1;p[cnt].y = 1;
        for(int i = 1;i <= n;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                int tmp;scanf("%d",&tmp);
                if(tmp > 0)
                {
                    p[++cnt].x = i;
                    p[cnt].y = j;
                }
            }
        }
        for(int i = 1;i <= cnt;i++)
        {
            for(int j = 1;j <= cnt;j++)
            {
                dist[i][j] = abs(p[i].x - p[j].x) + abs(p[i].y - p[j].y);
            }
        }
        
        memset(dp,INF,sizeof(dp));
        dp[1][1] = 0;
        for(int i = 1;i < (1 << cnt);i++)
        {
            for(int pre = 1;pre <= cnt;pre++)
            {
                if((1 << (pre - 1)) & i)
                {
                    if(dp[i][pre] != INF)
                    {
                        for(int now = 1;now <= cnt;now++)
                        {
                            if(!((1 << (now - 1)) & i))
                            {
                                dp[i | (1 << (now - 1))][now] = min(dp[i | (1 << (now - 1))][now],dp[i][pre] + dist[pre][now]);
                            }
                        }
                    }
                }
            }
        }
        
        int ans = INF;
        for(int i = 1;i <= cnt;i++)
        {
            ans = min(ans,dp[(1 << cnt) - 1][i] + dist[i][1]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

发布了594 篇原创文章 · 获赞 16 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/103797764
dig