感觉应当挺简单的,但是弄了好久……菜死了

如果不考虑那些为$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的更多相关文章

  1. bzoj1698 / P1606 [USACO07FEB]白银莲花池Lilypad Pond

    P1606 [USACO07FEB]白银莲花池Lilypad Pond 转化为最短路求解 放置莲花的方法如果直接算会有重复情况. 于是我们可以先预处理和已有莲花之间直接互相可达的点,将它们连边(对,忽 ...

  2. P1606 [USACO07FEB]白银莲花池Lilypad Pond

    这个题其实算是个最短路计数,建图的直观思想很简单,但是很显然有一个地方没法处理,就是有的时候通过两条路走到同一个地方的话方案数会计算两次.我们发现加上原有的莲花就很难处理,会计算重复.我们要想办法避免 ...

  3. 最短路【洛谷P1606】 [USACO07FEB]荷叶塘Lilypad Pond

    P1606 [USACO07FEB]荷叶塘Lilypad Pond 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令 ...

  4. 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond 解题报告

    P1606 [USACO07FEB]荷叶塘Lilypad Pond 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enj ...

  5. P1606 [USACO07FEB]荷叶塘Lilypad Pond(最短路计数)

    P1606 [USACO07FEB]荷叶塘Lilypad Pond 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enj ...

  6. BZOJ 1632: [Usaco2007 Feb]Lilypad Pond

    题目 1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 390  Solved: 109[ ...

  7. 1632: [Usaco2007 Feb]Lilypad Pond

    1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 404  Solved: 118[Sub ...

  8. 【luogu P1606 [USACO07FEB]荷叶塘Lilypad Pond】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1606 这个题..第一问很好想,但是第二问,如果要跑最短路计数的话,零边权的花怎么办? 不如这样想,如果这个点 ...

  9. [USACO07FEB] Lilypad Pond

    https://www.luogu.org/problem/show?pid=1606 题目描述 FJ has installed a beautiful pond for his cows' aes ...

随机推荐

  1. 剑指offer-第五章优化时间和空间效率(把数组排列成最小的数)

    题目:输入一个正整数数组,将所有的数,排列起来,组成一个最小的数.

  2. egg 使用手记(一)

    1. 文件加载规则 引用官方的说法: 框架在加载文件时会进行转换,因为文件命名风格和 API 风格存在差异.我们推荐文件使用下划线,而 API 使用驼峰.比如 app/service/user_inf ...

  3. 关于bonecp和QuerRunner

    之前一直以为boneCP和QueryRunner是绑定的,但是其实不是,后者来自于commons-dbUtils,BoneCP就是负责连接池. while preparing SQL: UPSERT ...

  4. @Override注解在Eclipse中编译报错

    导入新工程后,发现默认用的jre是1.5版本的,在Eclipse中右击工程选择Build Path->点击Configure Build Path->Java Build Path-> ...

  5. leetcode 21.Merge Two Sorted Lists ,java

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  6. 容器中跨主机的网络方案-Weave

    容器中的网络是建立docker集群的重要内容. 本文将介绍如何用Weave实现容器的多节点互通. Weave是一个开源的项目,其网站为: https://www.weave.works/ 其工作原理相 ...

  7. RHEL6.2配置从零开始

    RHEL6.2最小化安装并配置,持续更新中... 1.RHEL6.2最小化安装 RHEL6.2默认安装许多用不到的软件,不仅浪费空间.增大系统开销,还会显得凌乱.所以选择最小化安装.  注意:安装步骤 ...

  8. fragment用法

    简单用法: 1.新建布局.新建fragment类 2.在activity_main.xml中添加fragment <LinearLayout...... <fragment android ...

  9. 分布式缓存系统 Memcached 主线程之main函数

    前两节中对工作线程的工作流程做了较为详细的分析,现把其主要流程总结为下图: 接下来本节主要分析主线程相关的函数设计,主函数main的基本流程如下图所示: 对于主线程中的工作线程的初始化到启动所有的工作 ...

  10. 启用不安全的HTTP方法解决方案

    启用不安全的HTTP方法解决方案 Web AppScan HTTP WebDAV 近期通过APPScan扫描程序,发现了不少安全问题,通过大量查阅和尝试最终还是解决掉了,于是整理了一下方便查阅. 1. ...