Luogu 1606 [USACO07FEB]白银莲花池Lilypad Pond
感觉应当挺简单的,但是弄了好久……菜死了
如果不考虑那些为$1$的点,直接跑个最短路计数就好了,但是我们现在有一些边可以不用付出代价,那么只要在连边的时候先预处理搜一下就好了。
原来的想法是拆点,但是这样子不好连边,所以直接把点权转化到边权上来。
注意到起点其实不用付出代价,那么最后的答案就是$dis_ed - 1$。
时间复杂度上界是$O(n^4 + n^2log(n^2))$。
Code:
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair <ll, int> pin; const int N = ;
const int M = 2e5 + ;
const int L = ;
const int dx[] = {-, -, -, -, , , , };
const int dy[] = {-, -, , , , , -, -};
const ll inf = 0x3f3f3f3f3f3f3f3f; int n, m, a[L][L], tot = , head[N];
ll dis[N], cnt[N];
bool vis[N], ins[L][L]; struct Edge {
int to, nxt;
} e[M]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int id(int x, int y) {
return (x - ) * m + y;
} void dfs(int nowId, int x, int y) {
ins[x][y] = ;
for(int i = ; i < ; i++) {
int tox = x + dx[i], toy = y + dy[i];
if(tox < || tox > n || toy < || toy > m) continue;
if(ins[tox][toy]) continue;
if(a[tox][toy] == ) dfs(nowId, tox, toy);
else ins[tox][toy] = , add(nowId, id(tox, toy));
}
} priority_queue <pin> Q;
void dij(int st) {
memset(dis, 0x3f, sizeof(dis));
cnt[st] = 1LL, dis[st] = 0LL;
Q.push(pin(, st));
for(; !Q.empty(); ) {
int x = Q.top().second; Q.pop();
if(vis[x]) continue;
vis[x] = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(dis[y] > dis[x] + ) {
dis[y] = dis[x] + ;
cnt[y] = cnt[x];
Q.push(pin(-dis[y], y));
} else if(dis[y] == dis[x] + ) {
cnt[y] += cnt[x];
}
}
}
} int main() {
// freopen("testdata.in", "r", stdin); read(n), read(m);
int st, ed;
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++) {
read(a[i][j]);
if(a[i][j] == ) st = id(i, j);
if(a[i][j] == ) ed = id(i, j);
} for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
if(a[i][j] == || a[i][j] == ) {
memset(ins, , sizeof(ins));
dfs(id(i, j), i, j);
} dij(st); if(dis[ed] == inf) puts("-1");
else printf("%lld\n%lld\n", dis[ed] - , cnt[ed]); return ;
}
Luogu 1606 [USACO07FEB]白银莲花池Lilypad Pond的更多相关文章
- bzoj1698 / P1606 [USACO07FEB]白银莲花池Lilypad Pond
P1606 [USACO07FEB]白银莲花池Lilypad Pond 转化为最短路求解 放置莲花的方法如果直接算会有重复情况. 于是我们可以先预处理和已有莲花之间直接互相可达的点,将它们连边(对,忽 ...
- P1606 [USACO07FEB]白银莲花池Lilypad Pond
这个题其实算是个最短路计数,建图的直观思想很简单,但是很显然有一个地方没法处理,就是有的时候通过两条路走到同一个地方的话方案数会计算两次.我们发现加上原有的莲花就很难处理,会计算重复.我们要想办法避免 ...
- 最短路【洛谷P1606】 [USACO07FEB]荷叶塘Lilypad Pond
P1606 [USACO07FEB]荷叶塘Lilypad Pond 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令 ...
- 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond 解题报告
P1606 [USACO07FEB]荷叶塘Lilypad Pond 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enj ...
- P1606 [USACO07FEB]荷叶塘Lilypad Pond(最短路计数)
P1606 [USACO07FEB]荷叶塘Lilypad Pond 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enj ...
- BZOJ 1632: [Usaco2007 Feb]Lilypad Pond
题目 1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 390 Solved: 109[ ...
- 1632: [Usaco2007 Feb]Lilypad Pond
1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 404 Solved: 118[Sub ...
- 【luogu P1606 [USACO07FEB]荷叶塘Lilypad Pond】 题解
题目链接:https://www.luogu.org/problemnew/show/P1606 这个题..第一问很好想,但是第二问,如果要跑最短路计数的话,零边权的花怎么办? 不如这样想,如果这个点 ...
- [USACO07FEB] Lilypad Pond
https://www.luogu.org/problem/show?pid=1606 题目描述 FJ has installed a beautiful pond for his cows' aes ...
随机推荐
- 理解SQL【转http://blog.jobbole.com/55086/】
很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完全不同于我们所熟知的命令行语言.面向对象的程序语言.甚至是函数语言(尽管有些人认为 SQL 语言也是一种函数式语言) ...
- CSS 属性:touch-action
CSS 属性 touch-action 用于指定某个给定的区域是否允许用户操作,以及如何响应用户操作(比如浏览器自带的划动.缩放等). /* Keyword values */ touch-actio ...
- Pycharm简单使用教程
转自https://blog.csdn.net/qq_40130759/article/details/79421242 1.下载pycharm pycharm是一种Python IDE,能够帮助我们 ...
- 从如何优化SQL入手,提高数据仓库的ETL效率
1 引言数据仓库建设中的ETL(Extract, Transform, Load)是数据抽取.转换和装载到模型的过程,整个过程基本是通过控制用SQL语句编写的存储过程和函数的方式来实现对 ...
- Oracle恢复drop误删除的表和建用户操作
一.表的恢复 对误删的表,只要没有使用PURGE永久删除选项,那么从flash back区恢复回来希望是挺大的.一般步骤有: 1.从flash back里查询被删除的表 select * from r ...
- laravel中有条件使用where
在项目开发的过程中;有时候会有多个参数 去用在where查询中;那么这里的where语句是可能有也可能没有的 1.用原生的mysql语句来实现 private function getData($ty ...
- MongoDB-WriteConcern
WriteConcern safe=false 非安全模式 很快
- RHEL6 64位ASM方式安装oracle 11gR2(二)
本文转载自:http://vnimos.blog.51cto.com/2014866/1221377 三.安装数据库软件 1 2 3 4 5 6 7 8 # unzip -d /stage/ linu ...
- iPhone之IOS5内存管理(ARC技术概述)
ARC(Automatic Reference Counting )技术概述 此文章由Tom翻译,首发于csdn的blog,任何人都可以转发,但是请保留原始链接和翻译者得名字.多谢! Automati ...
- PyYAML和configparser模块讲解
Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation ymal主要用于配置文件. Co ...