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. 在VS2010中,引用了同一解决方案的另一个项目的dll,却不能正常调用(转)

    目前发现的原因是,dll的.net 版本比我的程序的高 dll用的.net 4  而程序用的.net 4 client profile 转载源:http://www.cnblogs.com/szyic ...

  2. target不起作用了

    原因是 <a href="",target></a>中间多了个逗号.

  3. 报错解决:No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    大概分析一般使用了注解才会报这方面的错 1.没有在spring的ApplicationContext.xml中开启注解事务 <!-- 开启注解事务 --> <tx:annotatio ...

  4. I-MooFest(POJ 1990)

    MooFest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5697   Accepted: 2481 Descripti ...

  5. DOM解析和SAX解析的区别

    DOM解析和SAX解析的区别 博客分类: XML DOM SAX  DOM解析和SAX解析的区别 No 区 别 DOM解析 SAX解析 1 操作 将所有文件读取到内存中形成DOM树,如果文件量过大,则 ...

  6. iBatisSQL中prepend的问题

    是前向声明还是后向声明? 官方文档那个给出:“the overridable SQL part that will be prepended to the statement”.可见是前向声明. -- ...

  7. BZOJ2280 [Poi2011]Plot

    恩..这题真是sxbk 我们先二分答案,然后判断答案是否满足要求 判断方法是二分当前段的长度一直做到底,当然我们可以用倍增这样快一点,直接随机增量就可以了 然后就是卡常..... 然后就是卡精度QAQ ...

  8. HDU 2676 Network Wars 01分数规划,最小割 难度:4

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1676 对顶点i,j,起点s=1,终点t=n,可以认为题意要求一组01矩阵use ...

  9. android自定义控件实例(Linearlayout组合TextView和ImageView)

    2013-12-18 11:25:22 转载自: http://www.open-open.com/lib/view/open1328836804515.html 很多时候android常用的控件不能 ...

  10. 通过代码自定义cell 新浪微博页面显示

    通过代码自定义cell(cell的高度不一致)(如果高度一致的cell 用xib实现) 1.新建一个集成自UItableVIewCell的类 2.重写initWithStle :方法 - (insta ...