hdu 5067 Harry And Dig Machine (TSP类,简单)

来源:互联网 发布:归并排序算法过程图解 编辑:程序博客网 时间:2024/05/15 23:58

题意:给出一幅图,图中有一些点,然后从第1个点出发,然后途径所有有石头的点,最后回到原点,然后求最小距离。


Harry And Dig Machine

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 841    Accepted Submission(s): 341


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 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 30 0 00 100 00 0 02 21 11 1
 

Sample Output
44
 

Source
BestCoder Round #14
 

Recommend
heyang   |   We have carefully selected several similar problems for you:  5717 5716 5715 5714 5713 
 

Statistic | Submit | Discuss | Note


#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)typedef long long ll;typedef pair<int, int> pii;const int INF =0x3f3f3f3f;int N,ed;struct POS{   int y,x;//个人习惯,将行数记为y,列数记为x   POS(){}   POS(int y,int x):y(y),x(x){}}pos[13];int dp[(1<<10)+3][12],n,m;int getdis(int i,int j)//点i到点j的距离,(曼哈顿距离){    return abs(pos[i].y-pos[j].y)+abs(pos[i].x-pos[j].x);}void TSP(){    int ans=INF;    for(int s=1;s<=ed;s++)    {        for(int i=0;i<N;i++) if( s&(1<<i) )//如果当前访问点包含了i        {            int s2=s^(1<<i);            if(!s2)  continue;//被continue的点已经被赋初值了            dp[s][i]=INF;            for(int j=0;j<N;j++)  if(s2&(1<<j))            {                dp[s][i]=min(dp[s][i],dp[s2][j]+getdis(i,j) );            }        }        if(s==ed)        {            for0(i,N)            {                ans=min(ans,dp[s][i]+ pos[i].y+pos[i].x);            }        }    }    printf("%d\n",ans);}int main(){    while(~scanf("%d%d",&n,&m))    {        N=0;int t;//N为所需访问点的个数        for0(i,n) for0(j,m)        {            scanf("%d",&t);            if(t)            {                pos[N]=POS(i,j);                dp[1<<N][N]= i+j;                N++;            }        }        if(!N)        {            puts("0");            continue;        }        ed=(1<<N)-1;//ed表示访问完所有点的状态        TSP();    }   return 0;}


0 0