解题:USACO13JAN Island Travels
好像没啥可说的,就当练码力了......
先用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的更多相关文章
- 洛谷P3070 [USACO13JAN]岛游记Island Travels
P3070 [USACO13JAN]岛游记Island Travels 题目描述 Farmer John has taken the cows to a vacation out on the oce ...
- 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 ...
- [Luogu3070][USACO13JAN]岛游记Island Travels
题目描述 Farmer John has taken the cows to a vacation out on the ocean! The cows are living on N (1 < ...
- [Usaco2013 Jan]Island Travels
Description Farmer John has taken the cows to a vacation out on the ocean! The cows are living on N ...
- 【BZOJ 3049】【USACO2013 Jan】Island Travels BFS+状压DP
这是今天下午的互测题,只得了60多分 分析一下错因: $dis[i][j]$只记录了相邻的两个岛屿之间的距离,我一开始以为可以,后来$charge$提醒我有可能会出现来回走的情况,而状压转移就一次,无 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- Usaco2012-2013 金组 题解 (暂缺Hill walk以及Figue eight)
https://files.cnblogs.com/files/Winniechen/usaco2012-2013.pdf 做的不是很好,还请见谅! 如果有什么疑问,可以QQ上找我. QQ号:1967 ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】463. Island Perimeter 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 减去相交部分 参考资料 日期 题目地址:https: ...
随机推荐
- golang--性能测试和分析
前言 测试分为:压力测试.负载测试.性能测试,功能测试等等,其中在开发过程中开发人员经常要写一些test case unit 自己的模块进行功能测试测和性能.在分析出模块的性能瓶颈后开发人员就需要针对 ...
- 学习笔记 | Set
目录 Set Set 前言 不会数据结构选手 当几乎没写过什么数据结构的菜鸡遇上了毒瘤的splay和treap 时间正一点一点地被续走TAT 听说set有时候可以替代treap和splay 那么菜鸡L ...
- [转] Unicode字符编码区间表
firebug 打UTF8 字符: var res = ""; for(var i=0x80;i< 0xff ;i++){ res += i.toString(16) + & ...
- 查看jdk使用的是什么垃圾收集器
一.方法一 打印虚拟机所有参数 [root@localhost ~]# java -XX:+PrintFlagsFinal -version | grep : uintx InitialHeap ...
- 【Oracle】存储过程在字符串单引号'内拼接单引号'
http://blog.csdn.net/u011704894/article/details/44976557 一般变量里面接3个单引号 eg: 'DELETE FROM RDM_SUPP_DATA ...
- MySql 赋值操作符"="与":="
MySql小点心—1.赋值操作符"="与":=" 对于刚接触到mysql的程序员来说,会对这两个符号有疑问,因为会发现有的代码里用这个有的用另一个. 当然他们是 ...
- C++:友元
前言:友元对于我来说一直是一个难点,最近看了一些有关友元的课程与博客,故在此将自己的学习收获做一个简单的总结 一.什么是友元 在C++的自定义类中,一个常规的成员函数声明往往意味着: • 该成员函数能 ...
- 【数据预处理】TIMIT语料库WAV文件转换
1 问题描述 这两天复现代码.先构造数据集,纯净语音.不同噪声.不同SNR的混合语音.其中纯净语音由两部分组成,IEEE corpus和TIMIT. 一开始我用MATLAB中的audioread读取音 ...
- spring冲刺计划
会议召开时间表 日期 时间 内容 05/09 21:00-22:00 讨论题目(未果) 05/10 21:00-21:30 确定题目(网络助手) 05/13 21:00-21:45 讨论软件页面设计 ...
- StringBuffer 与 StringBuilder类的使用
/*如果需要频繁修改字符串 的内容,建议使用字符串缓冲 类(StringBuffer). StringBuffer 其实就是一个存储字符 的容器. 笔试题目:使用Stringbuffer无 参的构造函 ...