BestCoder Round #14 B 题 Harry And Dig Machine 【TSP】
来源:互联网 发布:js splice 数组 编辑:程序博客网 时间:2024/06/05 06:42
题目:Harry And Dig Machine
哈哈 终于涨边粉色了,不容易呀。顺便写一道题解吧
题意:给一个m*n的矩阵,然后其中最多由10个有值,求总左上角把所有的值都拿上回到左上角的最小步数。
标准的TSP回到原点问题,需要先预处理出图来,然后TSP即可。
AC代码:
#include <cstdio>#include <algorithm>#include <cstring>#include <string>#include <iostream>#include <vector>#include <cmath>using namespace std;const int inf = 0x3f3f3f3f;const int N = 15;int mp[N][N];struct Node{ int x,y;};vector<Node> vv;int n,m;int dp[1<<N][N];int main(){ while(~scanf("%d%d",&n,&m)) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++){ int x; scanf("%d",&x); if(x) vv.push_back((Node){i,j}); } } int okk = 0; for(int i=0;i<vv.size();i++) { if(vv[i].x==0 && vv[i].y==0) { okk=1; continue; } } if(okk==0) vv.push_back((Node){0,0}); for(int i=0;i<vv.size();i++) { for(int j=0;j<vv.size();j++) { mp[i][j] = 0; if(i==j) continue; mp[i][j] = abs(vv[i].x-vv[j].x) + abs(vv[i].y-vv[j].y); } } int len = vv.size(); n = len; for(int st=0;st<(1<<n);st++) //TSP { for(int i=0;i<n;i++) { if((st&(1<<i))==0) //Ϊ0 continue; if(st==(1<<i)){ dp[st][i]=mp[0][i];continue; } dp[st][i]=inf; for(int j=0;j<n;j++) { if((st&(1<<j)) && i!=j)//Ϊ1 { dp[st][i]=min(dp[st&~(1<<i)][j]+mp[j][i],dp[st][i]); } } } } int ans=inf; for(int i=0;i<n;i++){ ans=min(ans,dp[(1<<n)-1][i]+mp[i][0]); } printf("%d\n",ans); vv.clear(); } return 0;}
0 0
- BestCoder Round #14 B 题 Harry And Dig Machine 【TSP】
- hdu 5067 Harry And Dig Machine(BestCoder Round #14)
- (BestCoder Round #14) 1002 Harry And Dig Machine(状态压缩+DP;剪枝+搜索;全排列)
- 【BestCoder】 HDOJ 5067 Harry And Dig Machine
- hdu 5067 Harry And Dig Machine (TSP类,简单)
- Harry And Dig Machine
- hdu5067:Harry And Dig Machine
- Harry And Dig Machine HDU
- 【CUGBACM15级BC第14场 B】Harry And Dig Machine
- HDU 5067 Harry And Dig Machine(状压DP)(TSP问题)
- HDOJ 题目5067 Harry And Dig Machine(状压dp,TSP)
- hdu5067 Harry And Dig Machine(裸TSP问题 状压dp )
- BestCoder Round #14 A题 Harry And Physical Teacher
- hdu 5067 Harry And Dig Machine
- hdu 5067 Harry And Dig Machine
- HDOJ 5067 Harry And Dig Machine
- HDU 5067 Harry And Dig Machine
- hdu5067——Harry And Dig Machine
- Android WifiDisplay分析一:相关Service的启动
- LINQ之路 1: LINQ介绍
- 第一次写文章
- Android Widget所占单元格计算
- 开博问好
- BestCoder Round #14 B 题 Harry And Dig Machine 【TSP】
- Bestcoder Round 14# 1001 Harry And Physical Teacher
- 算法导论15.4-6 求一个n个数的序列的最长单调递增子序列 O(n*logn)
- Android系统内置第三方应用
- 下一步学习的计划
- 例解GNU C之匿名联合或结构体
- C++调用CMD,等CMD运行完后继续运行C++
- C#设计模式之原型模式(Prototype Pattern)
- C#FlappyBird