hdu 5067 Harry And Dig Machine (状态压缩dp)
2016-07-20 15:13
260 查看
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.$(1 \leq n, m \leq 50)$.
The next n line, each line contains m integer. The j-th number of $i_{th}$ line a[i][j] means there are a[i][j] stones on the $j_{th}$ cell of the $i_{th}$ line.( $0 \leq a[i][j] \leq 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个位置,求从起点(0,0)出发遍历所有位置再回到原点得最短路。
解题思路:
1.DP+状态压缩,以二进制保存当前遍历状态,如果此状态下某个点未被经过,则用此状态转移到经过该店的状态,更新其值。
2.注意到n很小,DFS搜一遍~。
代码1:
代码2:
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 \leq n, m \leq 50)$.
The next n line, each line contains m integer. The j-th number of $i_{th}$ line a[i][j] means there are a[i][j] stones on the $j_{th}$ cell of the $i_{th}$ line.( $0 \leq a[i][j] \leq 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个位置,求从起点(0,0)出发遍历所有位置再回到原点得最短路。
解题思路:
1.DP+状态压缩,以二进制保存当前遍历状态,如果此状态下某个点未被经过,则用此状态转移到经过该店的状态,更新其值。
2.注意到n很小,DFS搜一遍~。
代码1:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int N=55; const int INF=0x3f3f3f3f; int dp[(1<<11)+15] ; int dist ; struct Node { int x,y; }p ; int main() { int n,m,t; int cnt; while(cin>>n>>m) { cnt=0; for(int i=0; i<n; i++) for(int j=0; j<n; j++) { cin>>t; if(t||(i==0&&j==0)) { p[cnt].x=i; p[cnt++].y=j; } } for(int i=0; i<cnt; i++) { for(int j=i+1; j<cnt; j++) dist[i][j]=dist[j][i]=abs(p[i].x-p[j].x)+abs(p[i].y-p[j].y); dist[i][i]=0; } memset(dp,INF,sizeof(dp)); dp[0][0]=0; for(int i=0; i<(1<<cnt); i++) { for(int j=0; j<cnt; j++) { if(dp[i][j]==INF||(i&(1<<j))==0) continue; for(int k=0; k<cnt; k++) { if(i&(1<<k)) continue; dp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],dp[i][j]+dist[j][k]); } } } cout<<dp[(1<<cnt)-1][0]<<endl; } }
代码2:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <deque> #include <list> #include <cctype> #include <algorithm> #include <climits> #include <queue> #include <stack> #include <cmath> #include <set> #include <iomanip> #include <cstdlib> #include <ctime> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; struct node { int x; int y; }; struct node p[15]; int ans; bool vis[15]; int path[15]; bool flag; void dfs ( int be, int en, int sum ) { if ( en == sum ) { ans = min ( ans, path[be] + p[be].x + p[be].y ); return; } for ( int i = 0; i <= en; i ++ ) { if ( ! vis[i] ) { int dis = abs ( p[be].x - p[i].x ) + abs ( p[be].y - p[i].y ); if ( path[i] > path[be] + dis ) { int tmp = path[i]; path[i] = path[be] + dis; vis[i] = 1; if ( path[i] < ans ) dfs ( i, en, sum + 1 ); vis[i] = 0; path[i] = tmp; } } } } int main() { int m, n; while ( cin >> m >> n ) { int cnt = 0; ans = 0x3f3f3f3f; flag = 0; int tmp; memset ( vis, 0, sizeof ( vis ) ); memset ( path, 0x3f3f3f3f, sizeof ( path ) ); for ( int i = 0; i < m; i ++ ) { for ( int j = 0; j < n; j ++ ) { cin >> tmp; if ( tmp != 0 ) { p[++cnt].x = i; p[cnt].y = j; } } } p[0].x = 0; p[0].y = 0; path[0] = 0; dfs ( 0, cnt, 0 ); cout << ans << endl; } return 0; }
相关文章推荐
- 更复杂的查询
- hadopp 的机架感知
- Java资源网站
- hdu 5730 Shell Necklace(2016多校第一场)FFT+分治
- Git版本恢复命令reset
- 一些常用控件属性&amp;amp;其他
- hdu 1540 Tunnel Warfare 线段数区间合并
- 二维数组的创建与遍历(php)
- 一起深入理解BFC~
- oracle 11g 测试服务器修改system密码时显示???解决方案
- Codeforces Round #363 (Div. 2)
- JAVA常见面试题及解答
- 一个月开发微信蓝牙四旋翼飞机
- 简单实现断点下载
- 手机直播可二次开发、萌颜、连麦、弹幕,首选云豹直播
- NOIPTG A组总结&T2题解
- C# 数组拆分
- 盛水最大量---贪心算法
- mysql 编译参数详解
- Javascript 基础之Number对象