题面

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

先用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. DP使用GUI推送WIN客户端是报110:1022错误的解决办法

    在使用GUI推送WIN客户端时,输入用户名和密码后报错: [Critical 110::1022]  Cannot connect to the SCM (Service Control Manage ...

  2. java web 3.1-web.xml文件配置

    <?xml version="1.0" encoding="UTF-8" ?> <web-app xmlns:xsi="http:/ ...

  3. 编译Android VNC Server

    1,在如下地址checkout源代码,我checkout的版本为0.9.7http://code.google.com/p/android-vnc-server/source/checkout 2,在 ...

  4. 2017-2018-20172311 暑期编程作业:APP

    2017-2018-20172311 暑期编程作业:实现一个简单倒计时APP 写在前面:暑假的时候就单纯的想要设计一个倒计时软件,然后就通过查阅资料等学了一些,包括实现倒计时功能及显示:背景音乐的添加 ...

  5. 团队计划backlog---DayTwo

    任务索引卡(Two): 1.  季方:实现界面跳转,数据库相关数据的显示 的测试: 2.  司宇航:添加部分团队博客,并测试: 3.  王金萱.马佳慧:学习爬虫的相关内容,为将来统计博客部分做准备: ...

  6. 【搜索】POJ-3187 枚举全排列

    一.题目 Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to ...

  7. 11_Java面向对象_第11天(接口、多态)_讲义

    今日内容介绍 1.接口 2.多态 01接口的概念 * A:接口的概念 接口是功能的集合,同样可看做是一种数据类型,是比抽象类更为抽象的"类". 接口只描述所应该具备的方法,并没有具 ...

  8. angularJS1笔记-(15)-自定义指令(accordion伸缩菜单原始实现)

    index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  9. 使用node-webkit包装浏览器

    node-webkit简称nwjs:开源地址 https://github.com/nwjs/nw.js 参考博客 https://www.cnblogs.com/soaringEveryday/p/ ...

  10. Beta阶段——2

    一.提供当天站立式会议照片一张: 二. 每个人的工作 (有work item 的ID) (1) 昨天已完成的工作: 昨天主要是对beta阶段的任务做了总结 (2) 今天计划完成的工作: 今天主要是对管 ...