HDU 5067 Harry And Dig Machine(状压DP)(TSP问题)
题目地址: 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问题)的更多相关文章
- hdu 5067 Harry And Dig Machine (状态压缩dp)
题目链接 bc上的一道题,刚开始想用这个方法做的,因为刚刚做了一个类似的题,但是想到这只是bc的第二题, 以为用bfs水一下就过去了,结果MLE了,因为bfs的队列里的状态太多了,耗内存太厉害. 题意 ...
- HDU 5067 Harry And Dig Machine(状压dp)
HDU 5067 Harry And Dig Machine 思路:因为点才10个,在加上一个起点,处理出每一个点之间的曼哈顿距离,然后用状压dp搞,状态表示为: dp[i][s],表示在i位置.走过 ...
- HDU 1565 - 方格取数(1) - [状压DP][网络流 - 最大点权独立集和最小点权覆盖集]
题目链接:https://cn.vjudge.net/problem/HDU-1565 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32 ...
- HDU 1074:Doing Homework(状压DP)
http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Problem Description Ignatius has just ...
- hdu 2167 方格取数 【状压dp】(经典)
<题目链接> 题目大意: 给出一些数字组成的n*n阶矩阵,这些数字都在[10,99]内,并且这个矩阵的 3<=n<=15,从这个矩阵中随机取出一些数字,在取完某个数字后,该数 ...
- HDU 6149 Valley Numer II(状压DP)
题目链接 HDU6149 百度之星复赛的题目……比赛的时候并没有做出来. 由于低点只有15个,所以我们可以考虑状压DP. 利用01背包的思想,依次考虑每个低点,然后枚举每个状态. 在每个状态里面任意枚 ...
- HDU 4917 Permutation(拓扑排序 + 状压DP + 组合数)
题目链接 Permutation 题目大意:给出n,和m个关系,每个关系为ai必须排在bi的前面,求符合要求的n的全排列的个数. 数据规模为n <= 40,m <= 20. 直接状压DP空 ...
- HDU 2809 God of War (状压DP)
God of War Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)
题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...
随机推荐
- 【Spring】Spring Session的简单搭建与源码阅读
搭建一个简单的Spring Session例子 引入依赖包 <dependencies> <dependency> <groupId>org.springframe ...
- Android自定义AlertDialog
常见的一种方法: [html] view plaincopyprint? AlertDialog.Builder builder; AlertDialog alertDialog; LayoutInf ...
- JMeter测试工具中的参数化使用[函数助手]
下面是我使用JMeter测试工具对某项目中的一个http接口进行测试时的配置参数,使用到了JMeter的随机函数: 在发送POST请求时,需要发送json格式的数据,其中使用到JMeter的" ...
- CSS实现:标题两侧画线效果
如图: html代码: <div class="detail-news"> <h5 class="detail-news-title"> ...
- adb install使用说明
[时间:2016-08] [状态:Open] [关键词:android, apk,adb install] 将apk安装到模拟器或者手机上. Pushes an Android application ...
- model number
// test.cpp : Defines the entry point for the console application. // #include "stdafx.h" ...
- iOS 设置导航栏全透明
- (void)viewWillAppear:(BOOL)animated{ //设置导航栏背景图片为一个空的image,这样就透明了 [self.navigationController.navig ...
- 基于jQuery鼠标悬停上下滑动导航条
基于jQuery鼠标悬停上下滑动导航条.这是一款蓝色好看的鼠标响应式网站导航菜单特效.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div id="menu2& ...
- Error: Java heap space
在chd中 的hive中执行 (select count (*)) 或者mr程序都报Error: Java heap space 4.io.sort.mb 的作用 排序所使用的内存数量. 默认值 ...
- Java中ArrayList实现原理
简述: ArrayList可以理解为动态数组,与Java中的数组相比,它的容量能动态增长.超出限制时会增加50%容量,用System.arraycopy()复制到新的数组中,因此最好能给出数组大小的预 ...