hdu 5067 Harry And Dig Machine

 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.(1n,m50).
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.( 0a[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
 
题意:给你一个n*m得图,每个点有一定数量的石子,问你将石子转移到(0.0)点所需最最少时间,每次转移联不限,相邻对的转移时间为1;
  
题解:由题目分析可知,就是求经过每个石堆一次最后回到起点的最小时间,由于石子最多有10堆,处理出图后我们便可使用状压的方法来求解,dp[s][i]表是当前状态下的终点在j点, 因此不难得出转移方程dp[s][j]=min(dp[s][j],dp[]s^(1<<j)[i]+map[i][j])

代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 9999999




int dp[(1<<11)+5][15];
int ma[15][15];
struct node
{
    int x,y;
}e[15];
int main()
{
    int n,m,xx;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int k=1;
        e[0].x=0;
        e[0].y=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%d",&xx);
                if(i==0&&j==0)continue;
                if(xx!=0)
                {
                    e[k].x=i;
                    e[k].y=j;
                    k++;
                }
            }
        }
        for(int i=0;i<k;i++)
        {
            for(int j=0;j<k;j++)
            {
                if(i!=j)
                ma[i][j]=inf;
            }
        }
        for(int i=0;i<k;i++)
        {
            for(int j=0;j<k;j++)
            {
                if(i==j)continue;
                if(ma[i][j]>(abs(e[i].x-e[j].x)+abs(e[i].y-e[j].y)))
                {
                    ma[i][j]=ma[j][i]=abs(e[i].x-e[j].x)+abs(e[i].y-e[j].y);
                }
            }
        }
        for(int s=0;s<(1<<k);s++)
        {
            for(int j=0;j<k;j++)
            {
                dp[s][j]=inf;
            }
        }
        for(int s=0;s<(1<<k);s++)
        {
            for(int now=0;now<k;now++)
            {
                if(!(s&(1<<now)))continue;
                if((1<<now)==s)
                {
                    dp[s][now]=ma[0][now];//当此转贷s中只含一个点时,一定是从0点到词此点
                    continue;
                }
                for(int i=0;i<k;i++)
                {
                    if(i==now)continue;
                    if(!(s&(1<<i)))continue;
                    int snow=(1<<now)^s;
                    if(dp[snow][i]==inf)continue;
                    dp[s][now]=min(dp[snow][i]+ma[i][now],dp[s][now]);
                }
            }
        }
        int ans=inf;
        for(int i=0;i<k;i++)
            ans=min(dp[(1<<k)-1][i]+ma[0][i],ans);
        if(ans!=inf)
        printf("%d\n",ans);
        else
        printf("0\n");
    }
    return 0;
}


发布了34 篇原创文章 · 获赞 0 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章