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

随便跑跑就行了

#include <bits/stdc++.h>

using namespace std ;

#define N 2000010
#define inf 0x3f3f3f3f
#define int long long

const int dx[] = { 1 , 1 , -1 , -1 , 2 , 2 , -2 , -2 } ;
const int dy[] = { 2 , -2 , 2 , -2 , 1 , -1 , 1 , -1 } ;

int n , m , s , t ;
int d[ N ] , vis[ N ] , q[ N ] , tot[ N ] , a[ 1000 ][ 1000 ] , id[ 1000 ][ 1000 ];
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 = 1 ; i <= n * m ; i ++ ) d[ i ] = inf ;
    d[ s ] = 0 ;
    q[ 1 ] = s ; 
    vis[ s ] = 1 ;
    tot[ s ] = 1 ;
    int l = 1 , r = 2 ;
    while( l != r ) {
        int u = q[ l ++ ] ; vis[ u ] = 0 ;
        if( l == 2000000 ) l = 1 ;
        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 ] = 1 , q[ r ++ ] = v ;
                    if( r == 2000000 ) r = 1 ;
                }
            } 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 ] - 1 , tot[ t ] ) ;
}

#define cl memset( visit , 0 , sizeof( visit ) ) 

bool visit[ 1000 ][ 1000 ] ;

bool check( int x , int y ) {
    if( x < 1 || x > n || y < 1 || y > m ) return 0 ;
    if( visit[ x ][ y ] ) return 0 ;
    return 1 ;
}

void col( int idx , int x , int y ) {
    if( visit[ x ][ y ] ) return ;
    visit[ x ][ y ] = 1 ;
    for( int i = 0 ; i < 8 ; i ++ ) {
        int nx = x + dx[ i ] , ny = y + dy[ i ] ;
        if( !check( nx , ny ) ) continue ;
        if( a[ nx ][ ny ] == 1 ) col( idx , nx , ny ) ;
        if( a[ nx ][ ny ] != 2 ) visit[ nx ][ ny ] = 1 , ins( idx , id[ nx ][ ny ] , 1 ) ;
    }
}

signed main() {
    scanf( "%lld%lld" , &n , &m ) ;
    for( int i = 1 ; i <= n ; i ++ ) {
        for( int j = 1 ; j <= m ; j ++ ) {
            scanf( "%lld" , &a[ i ][ j ] ) ;
            id[ i ][ j ] = ( i - 1 ) * m + j ;
            if( a[ i ][ j ] == 3 ) s = id[ i ][ j ] ;
            if( a[ i ][ j ] == 4 ) t = id[ i ][ j ] ;
        }
    }
    for( int i = 1 ; i <= n ; i ++ ) {
        for( int j = 1 ; j <= m ; j ++ ) {
            if( !a[ i ][ j ] || a[ i ][ j ] == 3 ) cl , col( id[ i ][ j ] , i , j ) ;
        }
    }
    spfa() ;
}

猜你喜欢

转载自www.cnblogs.com/henry-1202/p/BZOJ1698.html
今日推荐