题目链接: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】 题解的更多相关文章

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

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

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

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

  3. [洛谷P1606] [USACO07FEB] 荷叶塘Lilypad Pond

    Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是 ...

  4. 洛谷 P1606 [USACO07FEB]荷叶塘Lilypad Pond【spfa】

    和bzoj同名题不一样! 起点和水点向花费一个荷花能到的第一个点连一条边权为1的有向边,然后跑计数spfa即可 #include<iostream> #include<cstdio& ...

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

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

  6. Dfs+Spfa【p1606】[USACO07FEB]荷叶塘Lilypad Pond

    Description 为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘.这个长方形的池子被分成了M行N列个方格(1≤M,N≤30).一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是 ...

  7. LuoguP1606 [USACO07FEB]荷叶塘Lilypad Pond 【最短路】By cellur925

    最短路好题!] 参考资料:学长 https://blog.csdn.net/TSOI_Vergil/article/details/52975779 学长太强了!!!%%% 题目传送门 ======= ...

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

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

  9. BZOJ 1632: [Usaco2007 Feb]Lilypad Pond

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

随机推荐

  1. grpc的数据包监控

    CommView是一个专门为网络管理员,安全专家,网络程序员,以及任何想要全面了解一台个人电脑或一个网段中的网络通信量的用户设计的强大的网络监控器和分析器,不过它支持Win系统. 我这里用的 Comm ...

  2. 线程协作--wait,notify:经典消费者生产者

    JDK 中关于wait,notify这两个方法的介绍: 1.wait:线程进入阻塞. synchronized (obj) { while (<condition does not hold&g ...

  3. Delegate背后的秘密

    表面上看来使用delegate是一件很简单的事. 用delegate关键字定义,使用老套的new创建一个instance ,使用熟悉的方法调用写法调用,只不过不在是方法名,而是委托名. 但是在这背后C ...

  4. Java数据库操作(JDBC)

    JDBC Java数据库连接(Java DataBase Connectivity,JDBC)用于在Java程序中实现数据库操作功能,它提供了执行SQL语句.访问各种数据库的方法,并为各种不同的数据库 ...

  5. Tomcat配置连接c3p0连接池

    一.Tomcat配置JNDI资源 JNDI(Java Naming and Directory Interface),Java 命名和目录接口. JNDI的作用就是:在服务器上配置资源,然后通过统一的 ...

  6. Thymeleaf学习记录(2)--自动编译设置

    了方便每次修改HTML文件都能实时刷新,做一下更改. 在application.properties文件加入以下命令: #thymeleaf start spring.thymeleaf.mode=H ...

  7. 微信小程序-scroll-view组件

    <view class="section"> <view class="section__title">vertical scroll& ...

  8. 微服务架构之spring boot admin

    Spring boot admin是可视化的监控组件,依赖spring boot actuator收集各个服务的运行信息,通过spring boot actuator可以非常方便的查看每个微服务的He ...

  9. Linux时间同步+国内常用的NTP服务器地址

    当Linux服务需要时间戳的时候,时间同步就显得十分重要.这里介绍下,最近我使用的一个同步命令. # ntpdate s1a.time.edu.cn 国内常用的NTP地址 210.72.145.44 ...

  10. Example of assigning attributes directly to an object name

    Student类 package com.itheima_05; /* * 学生类 * * 通过对象直接访问成员变量,会存在数据安全问题 * 这个时候,我们就想能不能不让外界的对象直接访问成员变量呢? ...