题目描述

Farmer John has taken the cows to a vacation out on the ocean! The cows are living on N (1 <= N <= 15) islands, which are located on an R x C grid (1 <= R, C <= 50). An island is a maximal connected group of squares on the grid that are marked as 'X', where two 'X's are connected if they share a side. (Thus, two 'X's sharing a corner are not necessarily connected.)

Bessie, however, is arriving late, so she is coming in with FJ by helicopter. Thus, she can first land on any of the islands she chooses. She wants to visit all the cows at least once, so she will travel between islands until she has visited all N of the islands at least once.

FJ's helicopter doesn't have much fuel left, so he doesn't want to use it until the cows decide to go home. Fortunately, some of the squares in the grid are shallow water, which is denoted by 'S'. Bessie can swim through these squares in the four cardinal directions (north, east, south, west) in order to travel between the islands. She can also travel (in the four cardinal directions) between an island and shallow water, and vice versa.

Find the minimum distance Bessie will have to swim in order to visit all of the islands. (The distance Bessie will have to swim is the number of distinct times she is on a square marked 'S'.) After looking at a map of the area, Bessie knows this will be possible.

给你一张r*c的地图,有’S’,’X’,’.’三种地形,所有判定相邻与行走都是四连通的。我们设’X’为陆地,一个’X’连通块为一个岛屿,’S’为浅水,’.’为深水。刚开始你可以降落在任一一块陆地上,在陆地上可以行走,在浅水里可以游泳。并且陆地和浅水之间可以相互通行。但无论如何都不能走到深水。你现在要求通过行走和游泳使得你把所有的岛屿都经过一边。Q:你最少要经过几个浅水区?保证有解。

输入输出格式

输入格式:

* Line 1: Two space-separated integers: R and C.

* Lines 2..R+1: Line i+1 contains C characters giving row i of the
grid. Deep water squares are marked as '.', island squares are marked as
'X', and shallow water squares are marked as 'S'.

输出格式:

* Line 1: A single integer representing the minimum distance Bessie has to swim to visit all islands.

输入输出样例

输入样例#1:

5 4
XX.S
.S..
SXSS
S.SX
..SX
输出样例#1:

3

说明

There are three islands with shallow water paths connecting some of them.

Bessie can travel from the island in the top left to the one in the middle, swimming 1 unit, and then travel from the middle island to the one in the bottom right, swimming 2 units, for a total of 3 units.


注意到岛屿数量只有15个,所以考虑状压。

于是我们先跑dfs,把所有岛屿找出来,然后对于每个岛屿跑spfa求出经过的浅滩数。

然后设f[i][j]表示在i岛,经过岛屿的状态为j的最少经过的浅滩。

然后就是普通的DP。

话说把三个不难的知识块放在一起考仍然增加不了它的难度。

一开始是不想写spfa,直接暴力求出任意两个坐标的距离,因为复杂度在允许范围之内,写起来又方便...

于是TLE三个点,不得不说数据一点都不弱(-_-||

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
inline int read(){
int res=;char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch)){res=(res<<)+(res<<)+(ch^);ch=getchar();}
return res;
} int dx[]={, , -, , }, dy[]={, , , , -};
int n, m;
char mp[][];
bool vis[][];
int belong[][], tot;
int f[][<<], bin[]; void dfs(int x, int y)
{
belong[x][y] = tot;
for (int i = ; i <= ; i ++)
{
int tx = x + dx[i], ty = y + dy[i];
if (tx <= or tx > n or ty <= or ty > m) continue;
if (mp[tx][ty] != 'X') continue;
if (belong[tx][ty]) continue;
dfs(tx, ty);
}
} int dis[][], w[][][][];
struct date{
int x, y, stp;
}; inline void bfs(int X, int Y)
{
memset(vis, , sizeof vis);
queue <date> q;
q.push((date){X, Y, });
vis[X][Y] = ;
w[X][Y][X][Y] = ;
while(!q.empty())
{
int x = q.front().x, y = q.front().y, tp = q.front().stp;
q.pop();
vis[x][y] = ;
w[X][Y][x][y] = min(w[X][Y][x][y], tp);
for (int i = ; i <= ; i ++)
{
int tx = x + dx[i], ty = y + dy[i];
if (tx <= or tx > n or ty <= or ty > m) continue;
if (vis[tx][ty]) continue;
if (mp[tx][ty] == '.') continue;
q.push((date){tx, ty, tp + (mp[tx][ty]=='S')});
}
}
} int main()
{
n = read(), m = read();
for (int i = ; i <= n ; i ++)
for (int j = ; j <= m ; j ++)
cin >> mp[i][j]; for (int i = ; i <= n ; i ++)
{
for (int j = ; j <= m ; j ++)
{
if (!belong[i][j] and mp[i][j] == 'X')
{
tot++;
dfs(i, j);
}
}
}
memset(w, 0x3f, sizeof w);
for (int i = ; i <= n ; i ++)
{
for (int j = ; j <= m ; j ++)
{
bfs(i, j);
}
}
memset(dis, 0x3f, sizeof dis);
for (int i = ; i <= n ; i ++)
for (int j = ; j <= m ; j ++)
for (int x = ; x <= n ; x ++)
for (int y = ; y <= m ; y ++)
dis[belong[i][j]][belong[x][y]] = min(dis[belong[i][j]][belong[x][y]], w[i][j][x][y]);
memset(f, 0x3f, sizeof f);
bin[] = ;for(int i=;i<=;i++) bin[i]=bin[i-]<<;
for (int i = ; i <= tot ; i ++) f[i][bin[i]] = ;
for (int j = ; j <= (<<tot)- ; j ++)
{
for (int i = ; i <= tot ; i ++)
{
if ((j & bin[i]) == ) continue;
for (int k = ; k <= tot ; k ++)
{
if (k == i) continue;
if (j & bin[k]) continue;
f[k][j+bin[k]] = min(f[k][j+bin[k]], f[i][j] + dis[i][k]);
}
}
}
int ans = 1e9;
for (int i = ; i <= tot ; i ++)
ans = min(ans, f[i][(<<tot)-]);
printf("%d\n", ans);
return ;
}

复杂度允许的暴力

然后无奈只好写spfa...

于是调了好久好久...

最后发现好像个给每个联通快记大小的时候,少了初始点。

然后加上了91分,实在不想改了一个特判走人。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
inline int read(){
int res=;char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch)){res=(res<<)+(res<<)+(ch^);ch=getchar();}
return res;
} int dx[]={, , -, , }, dy[]={, , , , -};
int n, m;
char mp[][];
bool vis[][];
int belong[][], tot;
int f[][<<], bin[]; int siz[];
struct date{
int x, y;
}block[][]; void dfs(int x, int y)
{
belong[x][y] = tot;
for (register int i = ; i <= ; i ++)
{
int tx = x + dx[i], ty = y + dy[i];
if (tx <= or tx > n or ty <= or ty > m) continue;
if (mp[tx][ty] != 'X') continue;
if (belong[tx][ty]) continue;
block[tot][++siz[tot]] = (date){tx, ty};
dfs(tx, ty);
}
} int dis[][], w[][]; inline void bfs(int bl)
{
memset(vis, , sizeof vis);
memset(w, 0x3f, sizeof w);
queue <date> q;
for (register int i = ; i <= siz[bl] ; i ++)
{
q.push(block[bl][i]);
w[block[bl][i].x][block[bl][i].y] = ;
vis[block[bl][i].x][block[bl][i].y] = ;
}
while(!q.empty())
{
int x = q.front().x, y = q.front().y;
q.pop();
vis[x][y] = ;
for (register int i = ; i <= ; i ++)
{
int tx = x + dx[i], ty = y + dy[i];
if (tx <= or tx > n or ty <= or ty > m) continue;
if (mp[tx][ty] == '.') continue;
if (mp[tx][ty] == 'X')
{
if (w[tx][ty] > w[x][y])
{
w[tx][ty] = w[x][y];
if (!vis[tx][ty]) vis[tx][ty] = , q.push((date){tx, ty});
}
dis[bl][belong[tx][ty]] = min(dis[bl][belong[tx][ty]], w[tx][ty]);
dis[belong[tx][ty]][bl] = min(dis[belong[tx][ty]][bl], w[tx][ty]);
}
else
{
if (w[tx][ty] > w[x][y] + )
{
w[tx][ty] = w[x][y] + ;
if (!vis[tx][ty]) vis[tx][ty] = , q.push((date){tx, ty});
}
}
}
}
} int main()
{
n = read(), m = read();
for (register int i = ; i <= n ; i ++)
for (register int j = ; j <= m ; j ++)
cin >> mp[i][j]; for (register int i = ; i <= n ; i ++)
{
for (register int j = ; j <= m ; j ++)
{
if (!belong[i][j] and mp[i][j] == 'X')
{
tot++;
block[tot][++siz[tot]] = (date){i, j} ;
dfs(i, j);
}
}
}
memset(dis, 0x3f, sizeof dis);
for (register int i = ; i <= tot ; i ++)
{
dis[i][i] = ;
bfs(i);
}
memset(f, 0x3f, sizeof f);
for (register int i = ; i <= tot ; i ++) f[i][<<(i-)] = ;
for (register int j = ; j <= (<<tot)- ; j ++)
{
for (register int i = ; i <= tot ; i ++)
{
if ((j & (<<(i-))) == ) continue;
for (register int k = ; k <= tot ; k ++)
{
if (k == i) continue;
if (j & (<<(k-))) continue;
f[k][j|(<<(k-))] = min(f[k][j|(<<(k-))], f[i][j] + dis[i][k]);
}
}
}
int ans = 1e9;
for (register int i = ; i <= tot ; i ++)
ans = min(ans, f[i][(<<tot)-]);
if (ans == ) ans = ;
printf("%d\n", ans);
return ;
}

$\sum_{age=16}^{18} hardworking = success$

[Luogu3070][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. 解题:USACO13JAN Island Travels

    题面 好像没啥可说的,就当练码力了...... 先用BFS跑出岛屿,然后跑最短路求岛屿间的距离,最后状压DP得出答案 注意细节,码码码2333 #include<set> #include ...

  4. 菲律宾薄荷岛游记 & 攻略

    2019年的4月跑去薄荷岛玩了!其实是一个比较小众的海岛,感觉那边还是比较穷的,但是景色真的好好啊!而且我们两个人,最后包括前期买水母服.浮潜面罩.防晒霜什么的,总共花费才人均5000+,非常划算了! ...

  5. [Usaco2013 Jan]Island Travels

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

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

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

  7. bzoj AC倒序

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

  8. SFC游戏列表(维基百科)

    SFC游戏列表 日文名 中文译名 英文版名 发行日期 发行商 スーパーマリオワールド 超级马里奥世界 Super Mario World 1990年11月21日 任天堂 エフゼロ F-Zero F-Z ...

  9. 关于VR技术和未来发展---转

    原文地址:http://mp.weixin.qq.com/s?__biz=MzA4MTIwNTczMQ==&mid=2651345594&idx=3&sn=2741ab7321 ...

随机推荐

  1. Vue-学习笔记0-独立项目搭建

    前言 搭建Vue+Webpack项目,使用vue-cli搭建项目. 准备 vue独立项目依赖node的npm包管理器,所以需要先安装node. 相关的npm常用命令文章: Npm-常用命令,点击访问 ...

  2. Chrome 调试AJAX请求返回的JS脚本

    有时候会使用AJAX请求加载局部的Html页面,这个时候如果想调试局部页面中的js就比较麻烦,现在暂时发现了两种方法.第一种是在js代码中想要断点的地方加debugger,这样代码执行到此处会进入断点 ...

  3. 基于API和SQL的基本操作【DataFrame】

    写在前面: 当得到一个DataFrame对象之后,可以使用对象提供的各种API方法进行直接调用,进行数据的处理. // =====基于dataframe的API=======之后的就都是DataFra ...

  4. 【Python笔记】Python变量类型

    Python 变量类型 变量存储在内存中的值.这就意味着在创建变量时会在内存中开辟一个空间. 基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中. 因此,变量可以指定不同的数据 ...

  5. DeleteFile

    import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.apac ...

  6. .NET生成漂亮桌面背景

    .NET生成漂亮桌面背景 一天,我朋友指着某某付费软件对我说,这个东西不错,每天生成一张桌面背景,还能学英语(放置名人名言和翻译)!我说,这东西搞不好我也能做,然后朋友说,"如果你搞出来了, ...

  7. 死磕 java同步系列之zookeeper分布式锁

    问题 (1)zookeeper如何实现分布式锁? (2)zookeeper分布式锁有哪些优点? (3)zookeeper分布式锁有哪些缺点? 简介 zooKeeper是一个分布式的,开放源码的分布式应 ...

  8. System.exit退出程序

    方法源码: /** * Terminates the currently running Java Virtual Machine. The * argument serves as a status ...

  9. vs code编码设置

    在使用vs code(版本1.35.0)打开文件时,出现乱码问题,可通过如下方式设置: 1.针对单个文件 点击右下角的编码按钮(图中为UTF-8),然后选择操作,通过编码重新打开(Reopen wit ...

  10. 【ADO.NET--MVC】初学MVC(MVC入门)(1)

    最近一直在学MVC,本来今天想开始做项目了,但是一下手才发现还有好多好多都不懂,虽然想照搬别人的模板,但是还是觉得很虚,这也不懂哪也不懂.看来学习一门技术断不是那么简单,只要随便套套模板,看看别人代码 ...