Topic Links:
Huangjing
Test instructions: Give a picture, there are some points in the picture, then start from the 1th point, then route all the points of the stone, and finally go back to the origin, and then seek the minimum distance. I didn't know this was the classic problem of traveling business when I was playing. Came back to learn a bit.
Ideas:
State transition equation
Dp[k][i|base[k]]=min (Dp[k][i|base[k]],dp[j][i]+dis[j][k])
Dp[j][i] Indicates the minimum distance from the starting point to the J point in the I State..... Dp[j][i]+dis[j][k] Table from J to KDistance... The complexity of Time is(N?m+( t 2 )?( 2 t ) , then the problem is solved.
Topic:
Harry and Dig Machine
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 453 Accepted Submission (s): 164
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 have to gain knowledge from other certain subjects, such as language, mathematics, 中文版, 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 a 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 is useful, so we just move them to the Top-left cell. Taking it into account which Harry learned how to operate dig machine in Lanxiang School several years ago, Dumbledore Deci Des to let him does this job and wants it-done as quickly as possible. Harry needs one unit time to move he 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 are 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 Minim Al time Harry needs to finish it?
Inputthey is sever test cases, you should process to the end of file.
For each test case, there is integers n and M. (1≤n,m≤) .
The next n line contains M integer. The j-th number of < Span class= "Mrow" id= "mathjax-span-13" style= "" > i < Span class= "Mi" id= "mathjax-span-18" style= "" >t h Line A[i][j] means there is a[i][j] stones on the Jth Cell of the ith Line. ( 0≤a[i][J] ≤ , and no more than of a[i][j] would be positive integer).
Outputfor Each test case, just output one line, contains a integer indicate the minimal time that Harry can finish hi S job.
Sample Input
3 30 0 00 100 00 0 02 21 11 1
Sample Output
44
Sourcebestcoder Round #14
Recommendheyang | We have carefully selected several similar problems for you:5069 5068 5065 5064 5061
Statistic | Submit | Discuss | Note Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map > #include <vector> #include <cmath> #include <string> #include <queue> #define EPS 1e-9# Define ll long Long#define INF 0x3f3f3f3fusing namespace Std;const int Maxn=10+5;int dp[maxn][1<<maxn],dis[maxn][ Maxn],base[maxn];//dp[j][i] Indicates the minimum distance to reach J in the I state int p[maxn][2],n,m;int cal (int i,int j) {return abs (P[i][0]-p[j][0]) +abs (p I [1]-p[j][1]);} int main () {int cnt,k; while (~SCANF ("%d%d", &n,&m)) {cnt=0; Base[1]=1; for (int i=2;i<=14;i++) base[i]=base[i-1]<<1; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) {scanf ("%d", &k); if (k| | (i==1&&j==1)) {p[++cnt][0]=i; P[cnt][1]=j; }} memset (dis,0,sizeof (dis)); for (int i=1;i<=cnt;i++) for (int j=i+1;j<=cnt;j++) Dis[i][j]=dis[j][i]=cal (I,J); Memset (Dp,inf,sizeof (DP)); dp[1][0]=0; int lim=1<<cnt; for (int i=0;i<lim;i++)//state for (int j=1;j<=cnt;j++)//j point for start {if (Dp[j][i]==inf) Continue for (int k=1;k<=cnt;k++)//Transfer to the point {if (i&base[k]) continue; Dp[k][i|base[k]]=min (Dp[k][i|base[k]],dp[j][i]+dis[j][k]); }} printf ("%d\n", dp[1][lim-1]); } return 0;}
Hdu5067harry and Dig machine (TSP travel salesman problem)