题目地址:

pid=5067">HDU 5067

经典的TSP旅行商问题模型。

状压DP。

先分别预处理出来每两个石子堆的距离。然后将题目转化成10个城市每一个城市至少经过一次的最短时间模型。然后简单的状压DP就可以。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define LL __int64
const int INF=0x3f3f3f3f;
int d[20][20], dp[1<<12][12];
struct node
{
int x, y;
} stone[20];
int main()
{
int n, m, i, j, x, cnt, y, tmp, k;
while(scanf("%d%d",&n,&m)!=EOF)
{
cnt=0;
stone[0].x=stone[0].y=0;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
scanf("%d",&x);
if(x)
{
stone[++cnt].x=i;
stone[cnt].y=j;
}
}
}
for(i=0; i<=cnt; i++)
{
for(j=0; j<=i; j++)
{
x=abs(stone[i].x-stone[j].x)+abs(stone[i].y-stone[j].y);
d[i][j]=d[j][i]=x;
}
}
/*for(i=0;i<=cnt;i++)
{
for(j=0;j<=cnt;j++)
{
printf("%d ",d[i][j]);
}
puts("");
}*/
memset(dp,INF,sizeof(dp));
y=1<<cnt;
dp[y-1][0]=0;
//printf("%d\n",y);
for(i=y-1; i>=0; i--)
{
for(j=0; j<cnt; j++)
{
if(i&(1<<j))
{
tmp=i-(1<<j);
if(i==y-1)
{
dp[tmp][j+1]=dp[i][0]+d[0][j+1];
continue ;
}
for(k=1;k<=cnt;k++)
{
if(dp[i][k]!=INF)
{
dp[tmp][j+1]=min(dp[tmp][j+1],dp[i][k]+d[k][j+1]);
}
}
}
}
}
int min1=INF;
for(i=1;i<=cnt;i++)
{
dp[0][i]+=d[0][i];
min1=min(min1,dp[0][i]);
}
printf("%d\n",min1==INF? 0:min1);
}
return 0;
}

HDU 5067 Harry And Dig Machine(状压DP)(TSP问题)的更多相关文章

  1. hdu 5067 Harry And Dig Machine (状态压缩dp)

    题目链接 bc上的一道题,刚开始想用这个方法做的,因为刚刚做了一个类似的题,但是想到这只是bc的第二题, 以为用bfs水一下就过去了,结果MLE了,因为bfs的队列里的状态太多了,耗内存太厉害. 题意 ...

  2. HDU 5067 Harry And Dig Machine(状压dp)

    HDU 5067 Harry And Dig Machine 思路:因为点才10个,在加上一个起点,处理出每一个点之间的曼哈顿距离,然后用状压dp搞,状态表示为: dp[i][s],表示在i位置.走过 ...

  3. HDU 1565 - 方格取数(1) - [状压DP][网络流 - 最大点权独立集和最小点权覆盖集]

    题目链接:https://cn.vjudge.net/problem/HDU-1565 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32 ...

  4. HDU 1074:Doing Homework(状压DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Problem Description Ignatius has just ...

  5. hdu 2167 方格取数 【状压dp】(经典)

    <题目链接> 题目大意: 给出一些数字组成的n*n阶矩阵,这些数字都在[10,99]内,并且这个矩阵的  3<=n<=15,从这个矩阵中随机取出一些数字,在取完某个数字后,该数 ...

  6. HDU 6149 Valley Numer II(状压DP)

    题目链接 HDU6149 百度之星复赛的题目……比赛的时候并没有做出来. 由于低点只有15个,所以我们可以考虑状压DP. 利用01背包的思想,依次考虑每个低点,然后枚举每个状态. 在每个状态里面任意枚 ...

  7. HDU 4917 Permutation(拓扑排序 + 状压DP + 组合数)

    题目链接 Permutation 题目大意:给出n,和m个关系,每个关系为ai必须排在bi的前面,求符合要求的n的全排列的个数. 数据规模为n <= 40,m <= 20. 直接状压DP空 ...

  8. HDU 2809 God of War (状压DP)

    God of War Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)

    题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...

随机推荐

  1. tensorflow笔记1:基础函数、embedding_lookup

    函数一:tf.nn.embedding_lookup() ERROR: I get this error: TypeError: Tensors in list passed to 'values' ...

  2. hive sequencefile导入文件遇到FAILED: SemanticException Unable to load data to destination table. Error: The file that you are trying to load does not match the file format of the destination table.错误

    hive sequencefile导入文件遇到FAILED: SemanticException Unable to load data to destination table. Error: Th ...

  3. MATLAB左除和右除

    矩阵的除法包括左除(A\B).右除(A/B)和点除(A./B)三种. 一般情况下,x = A\b是方程组A*x = b的解, 而x = b/A是方程组x*A = b的解, x = A./B表示同型矩阵 ...

  4. 【GitHub】常用命令

    克隆项目 git clone http://*git 把本地项目提交到远程 git add --all git commit -m "Initail commit" git pus ...

  5. django模型创建

    定义模型 模型,属性,表,字段之间的关系 一个模型类在数据库中对应一张表,在模型类中定义的属性,对应该模型对照表中的一个字段 定义属性:见下文 创建模型类 元选项 在模型类中定义Meta类,用于设置元 ...

  6. 使用线性回归识别sklearn中的手写数字digit

    从昨天晚上,到今天上午12点半左右吧,一直在调这个代码.最开始训练的时候,老是说loss:nan 查了资料,因为是如果损失函数使用交叉熵,如果预测值为0或负数,求log的时候会出错.需要对预测结果进行 ...

  7. mybatis中的.xml文件总结——mybatis的动态sql

    resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...

  8. 【转】ExtJS获取父子、兄弟容器元素方法

    原文地址:http://www.cnblogs.com/linxiong945/p/3961732.html 1.当前对象的父对象(上级对象) this.ownerCt: 2.当前对象的下一个相邻的对 ...

  9. Java实现图片添加水印

    参考别人的感觉挺好玩,还没仔细研究,先上代码. package WaterMark; import javax.imageio.ImageIO; import java.awt.*; import j ...

  10. Postgres客户端编码问题

    数据库编程的编码问题数据库编程设计的编码问题包括三个方面:    数据库服务器编码:    数据库客户端编码:    本地环境编码.(1)数据库服务器字符编码:数据库服务器支持某种编码,是指数据库服务 ...