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 ...
随机推荐
- 剑指offer-第四章解决面试题的思路(栈的压入和弹出序列)
题目:输入两个整数序列,第一个序列表示栈的压入序列,请判断第二个序列是否为弹出序列. 思路:定义两个指针sPush和sPop分别指向两个整数序列的开头,借助一个辅助的栈,将第一个序列的数据依次压入栈中 ...
- 【转】C# Socket编程(2)识别网络主机
[转自:https://www.cnblogs.com/IPrograming/archive/2012/10/11/CSharp_Socket_2.html] 一个客户端想要发起一次通信,先决条件就 ...
- w3cschool在线教程
做网页开发的,没有不知道w3cschool的,如果你还不知道,那么就应该早点看下面推荐的文章,菜鸟可以帮你提升你的技能,老鸟可以温故而知新. 第一个是:http://www.w3school.com. ...
- java代码=--数组复制
总结:arraycopy注意数组定义的长度.不足会补0 package clientFrame; //数组的复制arraycopy() public class Xiang { public stat ...
- Playbooks 中的错误处理
Topics Playbooks 中的错误处理 忽略错误的命令 控制对失败的定义 覆写更改结果 Ansible 通常默认会确保检测模块和命令的返回码并且会快速失败 – 专注于一个错误除非你另作打算. ...
- Java面向对象-方法的重载
Java面向对象-方法的重载 所谓方法的重载, 类里面有两个或者多个重名的方法,但是方法的参数个数.类型.顺序至少有一个不一样,这时候局构成方法重载: 上代码: package com.java123 ...
- Linux进阶路线
初级:熟练使用命令.熟悉Shell编程.能配置简单的服务,清楚各类服务相关的配置文件的位置, 能看懂并可修改系统提供的配置脚本(/etc/*.*)把/etc目录下面常用的配置你都搞懂,把 /bin / ...
- Deep Learning(深度学习)学习笔记整理系列
http://blog.csdn.net/zouxy09/article/details/8775360 http://blog.csdn.net/zouxy09/article/details/87 ...
- OCX RegSvr32 error
[Window Title]RegSvr32 [Content]The module "tt.ocx" failed to load. Make sure the binary i ...
- linux下FTP使用
如何在linux下开启FTP服务 1. 首先服务器要安装ftp软件,查看是否已经安装ftp软件下: #which vsftpd 如果看到有vsftpd的目录说明服务器已经安装了ftp软件 ...