题面

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

先用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. Centos7下使用RDO方式安装openstack-r版

    一.前言 OpenStack是一个开源的云计算管理平台项目,OpenStack支持几乎所有类型的云环境,项目目标是提供实施简单.可大规模扩展.丰富.标准统一的云计算管理平台.OpenStack通过各种 ...

  2. IDEA 2018 最新激活码 License server

    IDEA 2018 最新激活码 License server 总会有一个属于适合你的!嘻嘻 http://hb5.s.osidea.cc:1017 http://idea.youbbs.org htt ...

  3. Yii2 输出图片相关

    http://www.yiichina.com/doc/api/2.0/yii-web-response#$format-detail https://segmentfault.com/q/10100 ...

  4. SpringMvc跨域支持

    SpringMvc跨域支持 在controller层加上注解@CrossOrigin可以实现跨域 该注解有两个参数 1,origins  : 允许可访问的域列表 2,maxAge:飞行前响应的缓存持续 ...

  5. CS小分队第二阶段冲刺站立会议(6月3日)

    昨日成果:完成了主界面按钮移动交换位置 遇到问题:最后的时候发现仅交换了按钮在数据库中的信息,对于按钮的链接忘记交换了 今日计划:解决这个问题,对这个冲刺阶段的成果进行整理

  6. The user survey(用户调查)

    在周末,我们找了一些人来进行了一个调查,鉴于选择困难,我们只找到了几个真正的小学生,没有找到家长,其余那些都是找大学生来做调查的,我们和他们说,让他们把自己的立场看成是小学生或家长.下面是我们整理出来 ...

  7. js 代码几种方式

    var nameSpace={ //public } (function(){ //private })(); var module=(function(){ //private return { / ...

  8. Gulp插件笔记

    初次接触Gulp是出于网页模块化的需要,用过之后发现这个任务管理工具有很多实用的插件,意外地好用,于是打算写下这篇笔记把用到的Gulp插件记录一下.至于想了解Gulp基本用法的同学可以去Gulp官网查 ...

  9. 利用css制作带边框的小三角

    标签(空格分隔):css 在项目中会使用到的小实例,目前知道的有两种方法来实现 设置元素的宽和高,利用rotate实现,比较简单的一种 div{ width: 10px; height: 10px; ...

  10. testng对执行失败的用例,再次执行

    前段时间在网络上看到通过重写TestNG的接口,可以再次执行失败的测试用例,于是学习了,我之前的做法是当自己的脚本中碰到异常,就自动调用方法本身来达到再次执行用例的目的,这个过程中有设定重试的次数 对 ...