解题: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: ...
随机推荐
- IntelliJ 10.0.1设置系统的JDK
IntelliJ 10.0.1设置JDK: File-->Project Structure: project SDK--New a jsdk.
- SpringMvc跨域支持
SpringMvc跨域支持 在controller层加上注解@CrossOrigin可以实现跨域 该注解有两个参数 1,origins : 允许可访问的域列表 2,maxAge:飞行前响应的缓存持续 ...
- Daily Scrum 11.19 部分测试报告
下面是我们的部分测试报告: 功能测试部分: 1Exception in thread "Thread-11" java.lang.IllegalArgumentException: ...
- 校友聊NABCD需求分析
校友聊 NABCD需求分析 N:内网用户流量不够使用 A:基于局域网进行通讯 B:通讯不花费外网流量 C:目前学校还没有使用 D:将软件放在校园网,可以下载使用
- Hibernate笔记②--hibernate类生成表、id生成策略、级联设置、继承映射
一.多表的一个关联关系 老师和学生是一对多的关系 student:tid属性 外键约束 对应teacher表中的id属性 teacher:id 在myeclipse的db窗口中选中两个表来生成类. ...
- 软工实践-Beta 冲刺 (1/7)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 1.界面的修改与完善 展示GitHub当日代码/文档签入记 ...
- 【Coursera】应用机器学习的建议
偏差方差权衡 使用较小的神经网络,类似于参数较少的情况,容易导致高偏差和欠拟合,但计算代价较小使用较大的神经网络,类似于参数较多的情况,容易导致高方差和过拟合,虽然计算代价比较大,但是可以通过归一化手 ...
- Week4-作业1
第四章 这一章主要讲了代码规范.复审和团队合作这两项内容. 在关于代码设计规范方面,书中讲到了关于goto的使用: “函数最好有单一的出口,为了达到这一目的, ...
- 对WEB url 发送POST请求
package com.excellence.spark; import java.util.List; import com.excellence.spark.test.test; import c ...
- Head First Java & static