BZOJ1632: [Usaco2007 Feb]Lilypad Pond SPFA+最短路计数
Description
为了让奶牛们娱乐和锻炼,农夫约翰建造了一个美丽的池塘。这个长方形的池子被分成了M行N列个方格(1≤M,N≤30)。一些格子是坚固得令人惊讶的莲花,还有一些格子是岩石,其余的只是美丽、纯净、湛蓝的水。
贝西正在练习芭蕾舞,她站在一朵莲花上,想跳到另一朵莲花上去,她只能从一朵莲花跳到另一朵莲花上,既不能跳到水里,也不能跳到岩石上。
贝西的舞步很像象棋中的马步:每次总是先横向移动一格,再纵向移动两格,或先纵向移动两格,再横向移动一格。最多时,贝西会有八个移动方向可供选择。
约翰一直在观看贝西的芭蕾练习,发现她有时候不能跳到终点,因为中间缺了一些荷叶。于是他想要添加几朵莲花来帮助贝西完成任务。一贯节俭的约翰只想添加最少数量的莲花。当然,莲花不能放在石头上。
请帮助约翰确定必须要添加的莲花的最少数量,以及有多少种放置这些莲花的方法.
Input
第一行:两个用空格分开的整数:M和N
第二行到M+1行:第i+1行有N个用空格分开的整数,描述了池塘第i行的状态:
0为水,1为莲花,2为岩石,3为贝西所在的起点,4为贝西想去的终点。
Output
第一行:一个整数,需要增加的最少莲花数;如果无解,输出-1。
第二行:放置这些莲花的方案数量,保证这个数字不会超过一个64位的有符号整数,
如果第一行是-1,不要输出第二行。
Sample Input
4 5
1 0 0 0 0
3 0 0 0 0
0 0 2 0 0
0 0 0 4 0
Sample Output
2
3
Solution
这题挺有意思的,建图挺难想的
岩石肯定就不连边了,然后对于水,和所有能到达的点连边(水或者荷叶),对于荷叶,直接把能到达的所有荷叶压成一个点,跳过它和其他点连边
这样子新图的边就是水-水,水-荷叶这样的
新图的最短路就是答案1,新图的最短路计数就是答案2
随便跑跑就行了
代码仅供参考。(我不会说我建图写的太丑TLE了)
//该代码仅供参考
#include <bits/stdc++.h> using namespace std ; #define N 2000010
#define inf 0x3f3f3f3f
#define int long long const int dx[] = { , , - , - , , , - , - } ;
const int dy[] = { , - , , - , , - , , - } ; int n , m , s , t ;
int d[ N ] , vis[ N ] , q[ N ] , tot[ N ] , a[ ][ ] , id[ ][ ];
int head[ N ] , cnt ;
struct node {
int to , nxt , v ;
} e[ N ] ; void ins( int u , int v , int w ) {
e[ ++ cnt ].to = v ;
e[ cnt ].nxt = head[ u ] ;
e[ cnt ].v = w ;
head[ u ] = cnt ;
} void spfa() {
for( int i = ; i <= n * m ; i ++ ) d[ i ] = inf ;
d[ s ] = ;
q[ ] = s ;
vis[ s ] = ;
tot[ s ] = ;
int l = , r = ;
while( l != r ) {
int u = q[ l ++ ] ; vis[ u ] = ;
if( l == ) l = ;
for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
int v = e[ i ].to ;
if( d[ v ] > d[ u ] + e[ i ].v ) {
d[ v ] = d[ u ] + e[ i ].v ;
tot[ v ] = tot[ u ] ;
if( !vis[ v ] ) {
vis[ v ] = , q[ r ++ ] = v ;
if( r == ) r = ;
}
} else if( d[ v ] == d[ u ] + e[ i ].v ) tot[ v ] += tot[ u ] ;
}
}
if( d[ t ] == inf ) puts( "-1" ) ;
else printf( "%lld\n%lld\n" , d[ t ] - , tot[ t ] ) ;
} #define cl memset( visit , 0 , sizeof( visit ) ) bool visit[ ][ ] ; bool check( int x , int y ) {
if( x < || x > n || y < || y > m ) return ;
if( visit[ x ][ y ] ) return ;
return ;
} void col( int idx , int x , int y ) {
if( visit[ x ][ y ] ) return ;
visit[ x ][ y ] = ;
for( int i = ; i < ; i ++ ) {
int nx = x + dx[ i ] , ny = y + dy[ i ] ;
if( !check( nx , ny ) ) continue ;
if( a[ nx ][ ny ] == ) col( idx , nx , ny ) ;
if( a[ nx ][ ny ] != ) visit[ nx ][ ny ] = , ins( idx , id[ nx ][ ny ] , ) ;
}
} signed main() {
scanf( "%lld%lld" , &n , &m ) ;
for( int i = ; i <= n ; i ++ ) {
for( int j = ; j <= m ; j ++ ) {
scanf( "%lld" , &a[ i ][ j ] ) ;
id[ i ][ j ] = ( i - ) * m + j ;
if( a[ i ][ j ] == ) s = id[ i ][ j ] ;
if( a[ i ][ j ] == ) t = id[ i ][ j ] ;
}
}
for( int i = ; i <= n ; i ++ ) {
for( int j = ; j <= m ; j ++ ) {
if( !a[ i ][ j ] || a[ i ][ j ] == ) cl , col( id[ i ][ j ] , i , j ) ;
}
}
spfa() ;
}
BZOJ1632: [Usaco2007 Feb]Lilypad Pond SPFA+最短路计数的更多相关文章
- bzoj1632 [Usaco2007 Feb]Lilypad Pond
Description Farmer John 建造了一个美丽的池塘,用于让他的牛们审美和锻炼.这个长方形的池子被分割成了 M 行和 N 列( 1 ≤ M ≤ 30 ; 1 ≤ N ≤ 30 ) 正方 ...
- 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 ...
- BZOJ 1632 [Usaco2007 Feb]Lilypad Pond:spfa【同时更新:经过边的数量最小】【路径数量】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1632 题意: 有一个n*m的池塘.0代表水,1代表荷花,2代表岩石,3代表起点,4代表终点 ...
- BZOJ1698: [Usaco2007 Feb]Lilypad Pond 荷叶池塘
一傻逼题调了两天.. n<=30 * m<=30的地图,0表示可以放平台,1表示本来有平台,2表示不能走,3起点4终点,走路方式为象棋的日字,求:从起点走到终点,至少要放多少平台,以及放平 ...
- 【BZOJ】1632: [Usaco2007 Feb]Lilypad Pond(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1632 我简直是个sb... ... bfs都不会写.. 算方案还用2个bfs! 都不会整合到一个! ...
- bzoj 1632: [Usaco2007 Feb]Lilypad Pond【bfs】
直接bfs,在过程中更新方案数即可 #include<iostream> #include<cstdio> #include<queue> using namesp ...
- LuoguP1606 [USACO07FEB]荷叶塘Lilypad Pond 【最短路】By cellur925
最短路好题!] 参考资料:学长 https://blog.csdn.net/TSOI_Vergil/article/details/52975779 学长太强了!!!%%% 题目传送门 ======= ...
- [ USACO 2007 FEB ] Lilypad Pond (Silver)
\(\\\) \(Description\) 一张\(N\times M\)的网格,已知起点和终点,其中有一些地方是落脚点,有一些地方是空地,还有一些地方是坏点. 现在要从起点到终点,每次移动走日字\ ...
随机推荐
- Shell 和Python的区别。
shell 应该属于宏语言,顾名思义是系统的壳,方便与系统交互的在以下情况下,不使用shell,因为shell对此无能为力:如:跨平台,较复杂数学操作(如浮点运算,精确运算等),图形化界面 GUI,I ...
- 启动rabbitmq,提示ERROR: node with name "rabbit" already running on "localhost"
➜ ~ rabbitmq-server ERROR: node with name "rabbit" already running on "localhost" ...
- 实习培训——Java基础(1)
实习培训——Java基础(1) 1.我的第一个JAVA程序 首先好配置好JDK环境,百度上有很多.创建文件HelloWorld.java(文件名与类名相同),代码如下: public class He ...
- POJ:3083 Children of the Candy Corn(bfs+dfs)
http://poj.org/problem?id=3083 Description The cornfield maze is a popular Halloween treat. Visitors ...
- SDUT2826:名字的价值
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2806 名字的价值 Time Limit: 10 ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- JavaScrip总体
js: 简单对象: 数字 | 字符串 | 这三个像对象,有方法,但不可变 布尔值 | null undefined 对象:无类型,k-v对集合:函数.数组.REG.... JavaScript是一种l ...
- [LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- HDU 1432 Lining Up(几何)
http://acm.hdu.edu.cn/showproblem.php?pid=1432 题目大意: 2维平面上给定n个点,求一条直线能够穿过点数最多是多少. 解题思路: 因为题目给定的n(1~7 ...
- PAT乙级 1024. 科学计数法 (20)(未通过全部测试,得分18)
1024. 科学计数法 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HOU, Qiming 科学计数法是科学家用来表示很 ...