http://acm.hdu.edu.cn/showproblem.php?pid=5067

规定起点和终点的tsp问题,解法依然是状态压缩dp,在初始化和计算答案的时候略做改动即可

#include <iostream>
#include <cstdio>
#include <map>
#include <cstring> using namespace std ; const int INF=0xfffffff ; int n,m ; int M[][] ;
int dis[][] ;
int dp[<<][] ;
int ABS(int x){return x>?x:-x ;} struct point
{
int x,y ;
}p[] ; int cal(point a,point b)
{
return ABS(a.x-b.x)+ABS(a.y-b.y) ;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i= ;i<n ;i++)
{
for(int j= ;j<m ;j++)
{
scanf("%d",&M[i][j]) ;
}
}
int cnt= ;
for(int i= ;i<n ;i++)
{
for(int j= ;j<m ;j++)
{
if(i== && j==)continue ;
if(M[i][j])
{
p[cnt].x=i ;
p[cnt++].y=j ;
}
}
}
for(int i= ;i< ;i++)
for(int j= ;j< ;j++)
dis[i][j]=INF ;
for(int i= ;i<cnt ;i++)
{
for(int j= ;j<cnt ;j++)
{
if(i==j)
{
dis[i][j]= ;
continue ;
}
dis[i][j]=cal(p[i],p[j]) ;
}
}
point s ;
s.x= ;s.y= ;
for(int i= ;i<(<<) ;i++)
for(int j= ;j< ;j++)
dp[i][j]=INF ;
for(int i= ;i<cnt ;i++)
{
dp[<<i][i]=cal(s,p[i]) ;
}
for(int i= ;i<(<<cnt) ;i++)
{
for(int j= ;j<cnt ;j++)
{
if(i&(<<j))
{
for(int k= ;k<cnt ;k++)
{
if(dis[k][j]==INF || !(i&(<<k)))continue ;
dp[i][j]=min(dp[i][j],dp[i^(<<j)][k]+dis[k][j]) ;
}
}
}
}
int ans=INF ;
for(int i= ;i<cnt ;i++)
{
ans=min(ans,dp[(<<cnt)-][i]+cal(p[i],s)) ;
}
if(ans==INF)
{
puts("") ;
continue ;
}
printf("%d\n",ans) ;
}
return ;
}

HDU 5067的更多相关文章

  1. HDU 5067 Harry And Dig Machine(状压DP)(TSP问题)

    题目地址:pid=5067">HDU 5067 经典的TSP旅行商问题模型. 状压DP. 先分别预处理出来每两个石子堆的距离.然后将题目转化成10个城市每一个城市至少经过一次的最短时间 ...

  2. HDU - 5067 / HDU - 5418 TSP

    集合表示多用[0,n)表示方法 HDU - 5067 经典TSP,每个顶点恰经过一次最优 #include<bits/stdc++.h> #define rep(i,j,k) for(in ...

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

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

  4. BestCoder14 1002.Harry And Dig Machine(hdu 5067) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5067 题目意思:给出一个 n * m 的方格,每一个小方格(大小为1*1)的值要么为 0 要么为一个正 ...

  5. HDU 5067 (状态压缩DP+TSP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5067 题目大意:蓝翔挖掘机挖石子.把地图上所有石子都运回起点,问最少耗时. 解题思路: 首先得YY出 ...

  6. hdu 5067 Harry And Dig Machine

    http://acm.hdu.edu.cn/showproblem.php?pid=5067 思路:问题可以转化成:从某一点出发,遍历网格上的一些点,每个点至少访问一次需要的最小时间是多少.这就是经典 ...

  7. HDU 5067 Harry And Dig Machine:TSP(旅行商)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5067 题意: 给你一个n*m的地图,地图上标着对应位置的石子数.你从左上角出发,每次可以向上下左右四个 ...

  8. hdu 5067 遍历指定点集最小时间

    http://acm.hdu.edu.cn/showproblem.php?pid=5067 贴题解 由于Harry的dig machine是无限大的,而装载石头和卸载石头是不费时间的,所以问题可以转 ...

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

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

随机推荐

  1. 继承多态绕点 Java篇

    上一篇把C#语言的继承,多态里的特殊的情况做了一下总结,其实那一部分代码都是从Java翻译过去的,今天来总结一下Java在这种情况下是怎么调用的. 上一篇我们说的是:1.多态,只在多态系里方法调用,很 ...

  2. centos 6.5 64位编译 apache2.4

    apache 2.4的安装和 apache2.2的安装有所不同 首先进入 http://apr.apache.org/download.cgi 下载 apr 和 apr-util 两个软件包 yum ...

  3. Scala 入门——Eclipse开发环境搭建

    Come From: http://lidrema.blog.163.com/blog/static/209702148201461145859142/ Scala: 一种类似java的编程.集成了面 ...

  4. bzoj 2243: [SDOI2011]染色

    #include<cstdio> #include<iostream> #define M 1000006 #define N 1000006 using namespace ...

  5. CodeForces 86D(Yandex.Algorithm 2011 Round 2)

    思路:莫队算法,离线操作,将所有询问的左端点进行分块(分成sqrt(n) 块每块sqrt(n)个),用左端点的块号进行排序小的在前,块号相等的,右端点小的在前面. 这样要是两个相邻的查询在同一块内左端 ...

  6. ARM安装ROS- indigo

    Ubuntu ARM install of ROS Indigo 溪西创客小屋 There are currently builds of ROS for Ubuntu Trusty armhf. T ...

  7. python 操作json

    认识 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - Dece ...

  8. SeGue 多控制器跨界面传递数据原理

    多控制器跨界面传递数据原理

  9. Xutils的使用 转载 带自己细细研究

    单例模式static DbUtils db = null; public static DbUtils getDb(Context context) { if (context == null) { ...

  10. 后台获取不规则排列RadioButton组的值

    获取多个RadioButton的值,我们一般会使用服务器控件RadioButtonList: <asp:RadioButtonList ID="rbl" runat=&quo ...