题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4856

题目大意:有一个迷宫。迷宫里有些隧道,每个隧道有起点和终点,在隧道里不耗时。出隧道就耗时,你的任务是访问完所有隧道且仅一次,求最短耗时。

解题思路:

暑假练习的时候。把英文读了N遍也没理解题意。

其实就是个最后不回到开头的TSP。

首先求BFS求两两隧道之间的最短路,注意BFS的起点是隧道i的终点,BFS的终点是隧道j的起点。

一定要特判一下两个隧道终点和起点是否一样,如果一样话dis=0, 我因为BFS没注意这个WA了10几次。

PS. 根据esxgx大神的说法,所有最短路算法(Dijkstra or Bellman-Ford)在每两个点距离为1的时候都会退化成普通的BFS,于是你在这里相当于手艹了个最短路。

然后就是对所有隧道进行TSP。

dp[i][j]表示状态i时,在j点的最少耗时,然后就是赤裸裸的TSP了。TSP的写法不唯一,我从别人那扒的这种TSP稍微快点。

#include "cstdio"
#include "string"
#include "iostream"
#include "cstring"
#include "queue"
#include "vector"
using namespace std;
int n,m,vis[][],dis[][],dir[][]={-,,,,,-,,},ans,dp[<<][];
char map[][];
const int inf=0x3f3f3f3f;
struct status
{
int x,y,dep;
status(int x,int y,int dep):x(x),y(y),dep(dep) {}
};
struct tunnel
{
int sx,sy,ex,ey;
}T[];
int bfs(status a,status b)
{
if(a.x==b.x&&a.y==b.y) return ; //关键一步,如果两个隧道恰好拼在一起,则dis=0
memset(vis,,sizeof(vis));
int ans=inf;
queue<status> Q;
Q.push(a);
vis[a.x][a.y]=true;
while(!Q.empty())
{
status t=Q.front();Q.pop();
for(int s=;s<;s++)
{
int X=t.x+dir[s][],Y=t.y+dir[s][];
if(X<||X>n||Y<||Y>n||vis[X][Y]||map[X][Y]!='.') continue;
vis[X][Y]=true;
if(X==b.x&&Y==b.y) {ans=min(ans,t.dep+);return ans;}
Q.push(status(X,Y,t.dep+));
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
string tt;
while(cin>>n>>m)
{
memset(dis,,sizeof(dis));
for(int i=;i<=n;i++)
{
cin>>tt;
for(int j=;j<tt.size();j++) map[i][j+]=tt[j];
}
for(int i=;i<=m;i++)
cin>>T[i].sx>>T[i].sy>>T[i].ex>>T[i].ey;
for(int i=;i<=m;i++)
for(int j=;j<=m;j++)
{
if(i==j) {dis[i][j]=;}
else dis[i][j]=bfs(status(T[i].ex,T[i].ey,),status(T[j].sx,T[j].sy,));
}
int cnt=<<m,res=inf;
for(int i=;i<cnt;i++)
{
for(int j=;j<=m;j++)
{
int s=(<<(j-));
if((i&s)==) continue;
if(i==s) dp[i][j]=;
else dp[i][j]=inf;
for(int k=;k<=m;k++)
{
int t=(<<(k-));
if((i&t)==||k==j||dis[k][j]==inf) continue;
dp[i][j]=min(dp[i][j],dp[i^s][k]+dis[k][j]);
}
if(i==cnt-) res=min(res,dp[i][j]);
}
}
if(res==inf) printf("-1\n");
else printf("%d\n",res); }
}
11887989 2014-10-16 20:17:15 Accepted 4856 171MS 2852K 2290 B C++ Physcal

HDU 4856 (状态压缩DP+TSP)的更多相关文章

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

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

  2. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

  3. HDU 3341 状态压缩DP+AC自动机

    题目大意: 调整基因的顺序,希望使得最后得到的基因包含有最多的匹配串基因,使得所能达到的智商最高 这里很明显要用状态压缩当前AC自动机上点使用了基因的情况所能达到的最优状态 我最开始对于状态的保存是, ...

  4. hdu 4284 状态压缩dp

    题意: 有N 个点的无向图,要去其中 h个地点做事,做事需要先办理护照,之后可以挣一定数量的钱,知道了一开始有的总钱数,和 一些城市之间           道路的花费,问可不可以在 指定的 h 个城 ...

  5. hdu 2167 状态压缩dp

    /* 状态转移方程:dp[i][j]=Max(dp[i][j],dp[i-1][k]+sum[i][j]); */ #include<stdio.h> #include<string ...

  6. BFS+优先队列+状态压缩DP+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=4568 Hunter Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  7. HDU 4640 状态压缩DP 未写完

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4640 解题思路: 首先用一个简单的2^n*n的dp可以求出一个人访问一个给定状态的最小花费,因为这i个 ...

  8. 2016"百度之星" - 初赛(Astar Round2A)1002 / HDU 5691 状态压缩DP

    Sitting in Line Problem Description   度度熊是他同时代中最伟大的数学家,一切数字都要听命于他.现在,又到了度度熊和他的数字仆人们玩排排坐游戏的时候了.游戏的规则十 ...

  9. hdu 4539(状态压缩dp)

    题意:曼哈顿距离是指:|x1-x2|+|y1-y2|,只要知道这个概念题意就懂了. 分析:这道题与前面做的几道题有所不同,因为当前行不仅与前一行有关,而且与前两行有关,所以我们开数组的时候还要记录前两 ...

随机推荐

  1. maven常见问题

    maven常见错误: 一..在Eclipse导入已存在工作空间的项目,出现 情况1: Scanning errors (1):1 Could not read pom.xml 就是pom.xml出现了 ...

  2. 以Python角度学习Javascript(二)之DOM

    HTML DOM 定义了访问和操作 HTML 文档的标准方法. DOM 将 HTML 文档表达为树结构. 文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文 ...

  3. How to: Set up Openswan L2TP VPN Server on CentOS 6

    Have you ever wanted to set up your own VPN server? By following the steps below, you can set up you ...

  4. MySql的like语句中的通配符:百分号、下划线和escape

      MySql的like语句中的通配符:百分号.下划线和escape   %:表示任意个或多个字符.可匹配任意类型和长度的字符. Sql代码 select * from user where user ...

  5. 【leetcode】3Sum

    3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find a ...

  6. BASH相关

    颜色 http://www.cnblogs.com/lr-ting/archive/2013/02/28/2936792.html http://segmentfault.com/q/10100000 ...

  7. 菜单栏展开和收起效果(纯js)

    2014年6月25日 15:36:29 需要关注的是: 1.用cookie保存用户当前点击的菜单项,不打扰后端代码 2.通过数学计算得到要显示和隐藏的div 3.点击事件是动态绑定到a标签上的,因此当 ...

  8. Java for LeetCode 040 Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  9. 3.前端笔记之JavaScript基础

    作者:刘耀 部分内容参考一下链接 参考: http://www.cnblogs.com/wupeiqi/articles/5369773.html http://javascript.ruanyife ...

  10. jquery给height拼接动态变量

    var sizeLength = "${list.size()}"; if(sizeLength==''){ sizeLength=0; } sizeLength=400*size ...