【luogu P1606 [USACO07FEB]荷叶塘Lilypad Pond】 题解
题目链接:https://www.luogu.org/problemnew/show/P1606
这个题。。第一问很好想,但是第二问,如果要跑最短路计数的话,零边权的花怎么办?
不如这样想,如果这个点能到花的话,那把他和从花能到的一个点边权连成一,好比两条路径共为1:一条为1一条为0的路径
但在实际操作的时候,一朵花是可以到另一朵花的!
电风扇好啊
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define int long long
using namespace std;
const int maxn = 2000000;
ll a[50][50], n, m, s, t, ans[maxn], dis[maxn];
bool vis[maxn], used[maxn];
ll dx[9] = {0,-2,-2,-1,+1,+2,+2,+1,-1}, dy[9] = {0,-1,+1,+2,+2,+1,-1,-2,-2};
queue<ll> q;
struct edge{
ll to, next, len;
}e[maxn<<2];
ll head[maxn], cnt;
void add(ll u, ll v, ll w)
{
e[++cnt].len = w; e[cnt].to = v; e[cnt].next = head[u]; head[u] = cnt;
}
void SPFA()
{
for(ll i = 1; i <= n*m; i++) dis[i] = 9223372036854775806;
q.push(s);
vis[s] = 1;
dis[s] = 0;
ans[s] = 1;
while(!q.empty())
{
ll now = q.front(); q.pop();
vis[now] = 0;
for(ll i = head[now]; i != -1; i = e[i].next)
{
ll v = e[i].to;
if(dis[v] > dis[now] + e[i].len)
{
ans[v] = ans[now];
dis[v] = dis[now] + e[i].len;
if(!vis[v])
{
q.push(v);
vis[v] = 1;
}
}
else if(dis[v] == dis[now] + e[i].len) ans[v] += ans[now];
}
}
}
void dfs(ll fx, ll fy, ll tx, ll ty)
{
used[(tx-1)*m+ty] = 1;
for(ll i = 1; i <= 8; i++)
{
ll ex = tx+dx[i], ey = ty+dy[i];
if(ex < 1 || ex > n || ey < 1 || ey > m || used[(ex-1)*m+ey]) continue;
if(a[ex][ey] == 2) continue;
if(a[ex][ey] == 1) dfs(fx, fy, ex, ey);
else used[(ex-1)*m+ey] = 1, add((fx-1)*m+fy, (ex-1)*m+ey, 1);
}
}
void init()
{
memset(head, -1, sizeof(head));
scanf("%lld%lld",&n,&m);
for(ll i = 1; i <= n; i++)
for(ll j = 1; j <= m; j++)
{
scanf("%lld",&a[i][j]);
if(a[i][j] == 3)
s = (i-1)*m+j;
if(a[i][j] == 4)
t = (i-1)*m+j;
}
for(ll i = 1; i <= n; i++)
for(ll j = 1; j <= m; j++)
{
if(a[i][j] == 2 || a[i][j] == 4 || a[i][j] == 1) continue;
memset(used, 0, sizeof(used));
dfs(i, j, i, j);
}
}
#undef ll
int main()
#define ll long long
{
freopen("testdata.in","r",stdin);
init();
SPFA();
if(dis[t] == 9223372036854775806) puts("-1");
else printf("%lld\n%lld",dis[t]-1, ans[t]);
return 0;
}
【luogu P1606 [USACO07FEB]荷叶塘Lilypad Pond】 题解的更多相关文章
- 洛谷 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 ...
- [洛谷P1606] [USACO07FEB] 荷叶塘Lilypad Pond
Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是 ...
- 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond【spfa】
和bzoj同名题不一样! 起点和水点向花费一个荷花能到的第一个点连一条边权为1的有向边,然后跑计数spfa即可 #include<iostream> #include<cstdio& ...
- 最短路【洛谷P1606】 [USACO07FEB]荷叶塘Lilypad Pond
P1606 [USACO07FEB]荷叶塘Lilypad Pond 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令 ...
- Dfs+Spfa【p1606】[USACO07FEB]荷叶塘Lilypad Pond
Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是 ...
- LuoguP1606 [USACO07FEB]荷叶塘Lilypad Pond 【最短路】By cellur925
最短路好题!] 参考资料:学长 https://blog.csdn.net/TSOI_Vergil/article/details/52975779 学长太强了!!!%%% 题目传送门 ======= ...
- bzoj1698 / P1606 [USACO07FEB]白银莲花池Lilypad Pond
P1606 [USACO07FEB]白银莲花池Lilypad Pond 转化为最短路求解 放置莲花的方法如果直接算会有重复情况. 于是我们可以先预处理和已有莲花之间直接互相可达的点,将它们连边(对,忽 ...
- BZOJ 1632: [Usaco2007 Feb]Lilypad Pond
题目 1632: [Usaco2007 Feb]Lilypad Pond Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 390 Solved: 109[ ...
随机推荐
- BZOJ5465: [APIO 2018] 选圆圈(K-D Tree)
题意 题目链接 Sol 下面是错误做法,正解请看这里 考虑直接用K-D tree模拟.. 刚开始想的是维护矩形最大最小值,以及子树中最大圆的位置,然后... 实际上最大圆的位置是不用维护的,直接把原序 ...
- PHP开发支付宝之电脑网站支付--流程简介
前言 前端时间自己开发了一个drupal的支付宝模块,现在整理一下过程,因为支付宝官方网站提供的接口及文档都是新接口的,而且使用新接口的过程比较麻烦一点,所以整理一下 1.支付宝的账号必须经过企业资格 ...
- vue2.0路由写法
// 0. 如果使用模块化机制编程,導入Vue和VueRouter,要调用 Vue.use(VueRouter) // 1. 定义(路由)组件. // 可以从其他文件 import 进来 var Fo ...
- chrome:插件、跨域、调试....
chrome 调试小技巧 ctrl+shift+c 打开chrome的控制台选中一个元素,然后在控制台输入$0即可获取选中的元素,就可以对其进行操作了. $0.addEventListener(... ...
- vsftpd for Anonymous Downloads on Ubuntu 16.04
Introduction FTP, short for File Transfer Protocol, is a network protocol that was once widely used ...
- Python爬虫教程-05-python爬虫实现百度翻译
使用python爬虫实现百度翻译功能 python爬虫实现百度翻译: python解释器[模拟浏览器],发送[post请求],传入待[翻译的内容]作为参数,获取[百度翻译的结果] 通过开发者工具,获取 ...
- maven(13)-安装nexus私服
环境 nexus最新3.x版需要java1.8,2.x版需要1.7以上.我之前一直用2.x,现在偿试在centos7和window10上分别安装nexus3.x,首先确保系统中已经配好了JDK1 ...
- Oralce 序列
序列: 是oacle提供的用于产生一系列唯一数字的数据库对象. l 自动提供唯一的数值 l 共享对象 l 主要用于提供主键值 l 将序列值装入内存可以提高访问效率 创建序列: 1. 要有创建 ...
- 查询login什么时候过期
-- Show all logins where the password is over 60 days old --查看60天没改密码的login SELECT name, LOGINPROPER ...
- java正则表达式校验移动电话、固话、邮编的校验
package com.tmall.epp.web.module.util; import java.util.regex.Pattern; /** * 移动电话.固话.邮编的校验 * @since ...