题面

好像没啥可说的,就当练码力了......

先用BFS跑出岛屿,然后跑最短路求岛屿间的距离,最后状压DP得出答案

注意细节,码码码2333

 #include<set>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=,M=,L=;
const int mov[][]={{,},{,-},{,},{-,}};
struct a
{
int xx,yy;
}que[L*L];
char mapp[L][L];
int val[N][N],dp[<<N][N];
int Mapp[L][L],vis[L][L],dis[L][L];
int n,m,f,b,nx,ny,land,shal,nde,all,ans=2e9;
deque<a> qs;
bool ok1(int x,int y)
{
return mapp[x][y]=='X'&&!Mapp[x][y]&&x>=&&x<=n&&y>=&&y<=m;
}
void BFS1(int x,int y)
{
que[f=b=]=(a){x,y},Mapp[x][y]=++land;
while(f<=b)
{
a tn=que[f++];
int tx=tn.xx,ty=tn.yy;
for(int i=;i<;i++)
if(ok1(nx=tx+mov[i][],ny=ty+mov[i][]))
Mapp[nx][ny]=land,que[++b]=(a){nx,ny};
}
}
bool ok2(int x,int y)
{
return mapp[x][y]=='S'&&!Mapp[x][y]&&x>=&&x<=n&&y>=&&y<=m;
}
void BFS2(int x,int y)
{
que[f=b=]=(a){x,y},Mapp[x][y]=--shal;
while(f<=b)
{
a tn=que[f++];
int tx=tn.xx,ty=tn.yy;
for(int i=;i<;i++)
if(ok2(nx=tx+mov[i][],ny=ty+mov[i][]))
Mapp[nx][ny]=shal,que[++b]=(a){nx,ny};
}
}
bool ok(int x,int y)
{
return Mapp[x][y]&&!vis[x][y]&&x>=&&x<=n&&y>=&&y<=m;
}
void BFS(int x,int y)
{
qs.push_back((a){x,y});
memset(vis,,sizeof vis);
memset(dis,,sizeof dis);
nde=Mapp[x][y],val[nde][nde]=,vis[x][y]=true;
while(!qs.empty())
{
a tt=qs.front(); qs.pop_front(); int tx=tt.xx,ty=tt.yy;
for(int i=;i<;i++)
if(ok(nx=tx+mov[i][],ny=ty+mov[i][]))
{
vis[nx][ny]=true;
if(Mapp[tx][ty]<) dis[nx][ny]=dis[tx][ty]+,qs.push_back((a){nx,ny});
else if(Mapp[tx][ty]>) dis[nx][ny]=dis[tx][ty],qs.push_front((a){nx,ny});
}
}
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(Mapp[i][j]>)
val[nde][Mapp[i][j]]=min(val[nde][Mapp[i][j]],dis[i][j]);
}
int ins(int nde)
{
return <<(nde-);
}
int main ()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%s",mapp[i]+);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(ok1(i,j)) BFS1(i,j);
else if(ok2(i,j)) BFS2(i,j);
memset(val,0x3f,sizeof val);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(Mapp[i][j]>) BFS(i,j);
all=(<<land)-;
memset(dp,0x3f,sizeof dp);
for(int i=;i<=land;i++) dp[ins(i)][i]=;
for(int i=;i<=all;i++)
for(int j=;j<=land;j++)
if(i&ins(j))
for(int k=;k<=land;k++)
if(!(i&ins(k))&&j!=k)
dp[i|ins(k)][k]=min(dp[i|ins(k)][k],dp[i][j]+val[j][k]);
for(int i=;i<=land;i++) ans=min(ans,dp[all][i]);
printf("%d",ans);
return ;
}

解题:USACO13JAN Island Travels的更多相关文章

  1. 洛谷P3070 [USACO13JAN]岛游记Island Travels

    P3070 [USACO13JAN]岛游记Island Travels 题目描述 Farmer John has taken the cows to a vacation out on the oce ...

  2. BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS

    BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS Description Farmer John has taken the cows to a va ...

  3. [Luogu3070][USACO13JAN]岛游记Island Travels

    题目描述 Farmer John has taken the cows to a vacation out on the ocean! The cows are living on N (1 < ...

  4. [Usaco2013 Jan]Island Travels

    Description Farmer John has taken the cows to a vacation out on the ocean! The cows are living on N ...

  5. 【BZOJ 3049】【USACO2013 Jan】Island Travels BFS+状压DP

    这是今天下午的互测题,只得了60多分 分析一下错因: $dis[i][j]$只记录了相邻的两个岛屿之间的距离,我一开始以为可以,后来$charge$提醒我有可能会出现来回走的情况,而状压转移就一次,无 ...

  6. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  7. Usaco2012-2013 金组 题解 (暂缺Hill walk以及Figue eight)

    https://files.cnblogs.com/files/Winniechen/usaco2012-2013.pdf 做的不是很好,还请见谅! 如果有什么疑问,可以QQ上找我. QQ号:1967 ...

  8. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  9. 【LeetCode】463. Island Perimeter 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...

随机推荐

  1. ubuntu 相关软件设置

    软件篇 1. 网易云音乐软件 首先去官网下载网易云音乐客户端linux版,网址:http://music.163.com/#/download,选择linux版本,然后选择ubuntu 16.04(6 ...

  2. aircrack-ng无线破解实验

    查看无线网卡 airmon-ng 开启网卡监听模式 airmon-ng start wlan0 扫描附近的wifi airodump-ng wlan0mon 停止扫描: ctrl c 使用airodu ...

  3. 解读Python编程中的命名空间与作用域

    变量是拥有匹配对象的名字(标识符).命名空间是一个包含了变量名称们(键)和它们各自相应的对象们(值)的字典.一个Python表达式可以访问局部命名空间和全局命名空间里的变量.如果一个局部变量和一个全局 ...

  4. linux安装nginx并配置负载均衡

    linux上安装nginx比较简单: 前提是需要有gcc或者g++ 1.yum需要的依赖  yum -y install openssl openssl-devel 2.解压pcre库.zlib库   ...

  5. java中重要的多线程工具类

    前言 之前学多线程的时候没有学习线程的同步工具类(辅助类).ps:当时觉得暂时用不上,认为是挺高深的知识点就没去管了.. 在前几天,朋友发了一篇比较好的Semaphore文章过来,然后在浏览博客的时候 ...

  6. -lPods-NewsPushClientSDK is not an object file (not allowed in a library)

    今天在给客户定制SDK的过程中,出现了下面的一个问题,具有代表性,现在记录下来 问题: Showing All Errors Only : /Applications/Xcode.app/Conten ...

  7. TeamWork#3,Week5,Bing Input Method vs Sogou Input Method

    现在电脑上用五笔的用户越来越少了,好的拼音输入法也是难求.必应输入法的前身英库拼音输入法来自微软亚洲研究院的多项基础研究成果.最新的必应输入法不仅保留了英库拼音输入法的各项优势,还结合了必应的搜索体验 ...

  8. spring冲刺第三天

    昨天完成了环境配置和初步的地图设想. 今天从网上找了有关这方面的例子,运行试验了一番.编写的地图画面在程序上运行了一下,有些错误,还需要很多方面的改进. 这些例子有很多地方都不太懂,但还是看完了.我认 ...

  9. (小组)第六次作业:NABCD模型分析。产品Backlog。

    NABCD模型分析: NABCD模型分析 1.N——need需求 随着时代的进步,人们生活水平的提高,现在手机的普及率已经非常高了,而且现在的家长很多时候会忙于工作,很少会花时间出来给自己读小学的孩子 ...

  10. GC 年轻代 老年代 持久代

    转载自:http://www.cnblogs.com/yaoyuan23/p/5587548.html 虚拟机中的共划分为三个代:年轻代(Young Generation).老年代(Old Gener ...