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

如果不考虑那些为$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 = 1005;
const int M = 2e5 + 5;
const int L = 35;
const int dx[] = {-1, -2, -2, -1, 1, 2, 2, 1};
const int dy[] = {-2, -1, 1, 2, 2, 1, -1, -2};
const ll inf = 0x3f3f3f3f3f3f3f3f;int n, m, a[L][L], tot = 0, 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 = 0; char ch = 0; T op = 1;for(; ch > '9' || ch < '0'; ch = getchar())if(ch == '-') op = -1;for(; ch >= '0' && ch <= '9'; ch = getchar())X = (X << 3) + (X << 1) + ch - 48;X *= op;
}inline int id(int x, int y) {return (x - 1) * m + y;
}void dfs(int nowId, int x, int y) {ins[x][y] = 1;for(int i = 0; i < 8; i++) {int tox = x + dx[i], toy = y + dy[i];if(tox < 1 || tox > n || toy < 1 || toy > m) continue;if(ins[tox][toy]) continue;if(a[tox][toy] == 1) dfs(nowId, tox, toy);else ins[tox][toy] = 1, 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(0, st));for(; !Q.empty(); ) {int x = Q.top().second; Q.pop();if(vis[x]) continue;vis[x] = 1;for(int i = head[x]; i; i = e[i].nxt) {int y = e[i].to;if(dis[y] > dis[x] + 1) {dis[y] = dis[x] + 1;cnt[y] = cnt[x];Q.push(pin(-dis[y], y));} else if(dis[y] == dis[x] + 1) {cnt[y] += cnt[x];}}}
}int main() {
//    freopen("testdata.in", "r", stdin);
    read(n), read(m);int st, ed;for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++) {read(a[i][j]);if(a[i][j] == 3) st = id(i, j);if(a[i][j] == 4) ed = id(i, j);}for(int i = 1; i <= n; i++)for(int j = 1; j <= m; j++)if(a[i][j] == 0 || a[i][j] == 3) {memset(ins, 0, sizeof(ins));dfs(id(i, j), i, j);} dij(st);if(dis[ed] == inf) puts("-1");else printf("%lld\n%lld\n", dis[ed] - 1, cnt[ed]);return 0;
}

View Code

转载于:https://www.cnblogs.com/CzxingcHen/p/9856811.html

Luogu 1606 [USACO07FEB]白银莲花池Lilypad Pond相关推荐

  1. 【USACO】青铜莲花池[2]

    前言 搜索到这篇文章的朋友,那么很巧了,我们多半是一个学校的,为什么呢?因为这道题叫白银莲花池.. 题目 [问题描述] FJ建造了一个美丽的池塘,用于让奶牛们锻炼.这个长方形的池子被分割成了 M 行和 ...

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

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

  3. bzoj 1632: [Usaco2007 Feb]Lilypad Pond(BFS)

    1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 688  Solved: 230 [S ...

  4. bzoj 1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘(BFS)

    1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 550  Solved: 1 ...

  5. Problem : [usaco2007 Feb]Lilypad Pond

    Problem : [usaco2007 Feb]Lilypad Pond Description Farmer John 建造了一个美丽的池塘,用于让他的牛们审美和锻炼. 这个长方形的池子被分割成了 ...

  6. 故事公园-—昆明莲花池

    每个公园都有自己的地理特征,奇风异俗,各存其韵.昆明莲花池公园则因几处遗址以及涉及的故事,让人们留连往返,奕奕不舍,他(她)们的故事让世人感叹! 这里的遗址其中有南明末代皇帝的墓地,陈圆圆的梳妆楼,现 ...

  7. [USACO07FEB] Lilypad Pond

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

  8. 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond(spfa+最短路计数) 题解

    题目来源: https://www.luogu.org/problemnew/show/P1606 题目描述: 题目描述 FJ has installed a beautiful pond for h ...

  9. [USACO07FEB]Lilypad Pond

    Link 考场上把我送走的一道题 考试时,晃眼一看,这不是裸的最短路计数吗?!赶忙写好了代码,自信地关闭了文件.然后,0分-- 那么回过头来,到底错在哪里?只有新加的荷叶才会被统计,而原来就有的荷叶不 ...

  10. 洛谷P1606 Lilypad Pond G

    传送门 题目描述 FJ has installed a beautiful pond for his cows' aesthetic enjoyment and exercise. The recta ...

最新文章

  1. 最小二乘法的本质是什么?
  2. 人人都能成为安全防范的高手 ——《黑客新型攻击防范:深入剖析犯罪软件》
  3. linux自动运行python脚本,执行脚本如何在linux环境下自启动
  4. 缓存中常见的概念及解决方案
  5. 同步现象 心理学_非心理专业背景的人,如何成为心理学家或心理咨询师?
  6. 使用JMH做Java微基准测试
  7. 弹出对话框拖拽JavaScript实现
  8. 用得最多的冒泡排序是不是少了个关键点?
  9. ArcGIS 10.2数字化线状要素时自动拼接成一条线
  10. PTA18、图的字典表示 (10 分)
  11. 阿里云技术天团空降 CSDN 独家在线峰会,揭秘核心竞争力
  12. 电脑连接校园网不自动跳转到登录界面
  13. 国产手机下乡难以撼动山寨手机农村市场
  14. 数字人事系统 java_市国税局“数字人事”信息系统正式上线
  15. 用PS将图片或表格中的英文变成中文
  16. 1.25 Cubemx_STM32H743II—QSPI
  17. 锂电池表面缺陷检测设备
  18. Threejs系列--9游戏开发--沙漠赛车游戏【基础场景渲染】
  19. 【Linux】宝塔面板 SSL 证书安装部署
  20. 百度ToB垂类账号权限平台的设计与实践

热门文章

  1. 初学者须知 常见Web前端开发工具有哪些
  2. Firefox中about:config配置大全
  3. Android WiFi Direct文件传输
  4. FGFA训练自己的数据集docker
  5. 剑指Offer_46 把数字翻译成字符串
  6. SAP MM 采购信息记录中价格单位转换因子的修改
  7. Nginx实现反向代理(同一服务器下不同端口服务映射到80端口)
  8. gbd调试器及静态库/动态库的制作
  9. java订单超时取消设计_订单超时30分钟自动取消
  10. 鸿蒙系统屏幕录制,全屏幕录制可以隐藏选项窗口么