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-第五章优化时间和空间效率(在字符串中第一次出现切只出现一次的字符)
题目:在字符串中第一次出现切只出现一次的字符 思路:用HashMap来存放对应的char值和该char出现的次数.做一次变量就可以得到第一个只出现一次的字符. Java代码: import java. ...
- VS 生成 dll、exe 版本号与SVN版本号一致
1.VS 可自动生成版本号 注释掉以下两行代码 [assembly: AssemblyVersion("1.0.0.0")][assembly: AssemblyFileVersi ...
- 读论文系列:Object Detection ICCV2015 Fast RCNN
Fast RCNN是对RCNN的性能优化版本,在VGG16上,Fast R-CNN训练速度是RCNN的9倍, 测试速度是RCNN213倍:训练速度是SPP-net的3倍,测试速度是SPP-net的3倍 ...
- 【java规则引擎】规则引擎RuleBase中利用观察者模式
(1)当RuleBase中有规则添加或删除,利用观察者模式实现,一旦有变动,规则引擎其他组件也做出相应的改变.(2)学习思想:当一个应用中涉及多个组件,为了实现易扩展,解耦思想.可以利用观察者模式实现 ...
- LG3391 【模板】文艺平衡树(Splay)
题意 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 \(n,m ...
- vuecli3修改项目启动端口
工作中可能存在启动多个项目的时候,默认端口号会被占,导致启动错误,这种情况下只要把要启动的项目的端口号换掉启动未用的端口就可以了,具体实现如下: vuecli3中的端口文件存放目录为:node_mod ...
- 手把手教你在Eclipse中使用CVS Branch功能
Brach 的作用: 开发新版本的人员就基于 main trunk 工作,而 fix bug 的人员就基于 branch 工作. 一旦在 branch上将 Release_1_0的 bug修复了,我们 ...
- The type javax.xml.rpc.ServiceException cannot be resolved.It is indirectly
The type javax.xml.rpc.ServiceException cannot be resolved.It is indirectly 博客分类: 解决方案_Java 问题描述:T ...
- (转)C# Socket简单例子(服务器与客户端通信)
本文转载自:http://blog.csdn.net/andrew_wx/article/details/6629721 这个例子只是简单实现了如何使用 Socket 类实现面向连接的通信. 注意:此 ...
- 2012_p2 寻宝 (treasure.cpp/c/pas)
2012_p2 寻宝 (treasure.cpp/c/pas) 时间限制: 1 Sec 内存限制: 128 MB提交: 23 解决: 9[提交][状态][讨论版][命题人:外部导入] 题目描述 2 ...