美文网首页信息学竞赛题解(IO题解)
BZOJ-2662: [BeiJing wc2012]冻结(SP

BZOJ-2662: [BeiJing wc2012]冻结(SP

作者: AmadeusChan | 来源:发表于2018-11-13 12:19 被阅读0次

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2662

    裸SPFA。。。没了。。。

    代码:

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <deque>
     
    using namespace std ;
     
    #define AddEdge( s , t , d ) Add( s , t , d ) , Add( t , s , d )
    #define pf push_front
    #define pb push_back
     
    const int inf = 0x7fffffff ;
    const int maxn = 50 + 5 ;
    const int maxk = 50 + 5 ;
     
    struct edge {
        edge *next ;
        int t , d ;
    } *head[ maxn ] ;
     
    void Add( int s , int t , int d ) {
        edge *p = new( edge ) ;
        p -> t = t , p -> d = d , p -> next = head[ s ] ;
        head[ s ] = p ;
    }
     
    int dist[ maxn ][ maxk ] , n , m , k ;
     
    struct node {
        int x , y ;
        node( int _x , int _y ) : x( _x ) , y( _y ) {
        }
    };
    deque < node > q ;
     
    bool f[ maxn ][ maxk ] ;
     
    int main(  ) {
        memset( head , 0 , sizeof( head ) ) ;
        scanf( "%d%d%d" , &n , &m , &k ) ;
        while ( m -- ) {
            int s , t , d ; scanf( "%d%d%d" , &s , &t , &d ) ;
            AddEdge( s , t , d ) ;
        }
        q.clear(  ) ;
        for ( int i = 0 ; i ++ < n ; ) {
            for ( int j = 0 ; j <= k ; ++ j ) {
                dist[ i ][ j ] = inf ; 
            }
        }
        memset( f , false , sizeof( f ) ) ;
        dist[ 1 ][ 0 ] = 0 , q.pf( node( 1 , 0 ) ) , f[ 1 ][ 0 ] = true ;
        while ( ! q.empty(  ) ) {
            node v = q.front(  ) ; q.pop_front(  ) , f[ v.x ][ v.y ] = false ;
            for ( edge *p = head[ v.x ] ; p ; p = p -> next ) {
                if ( dist[ p -> t ][ v.y ] > dist[ v.x ][ v.y ] + p -> d ) {
                    dist[ p -> t ][ v.y ] = dist[ v.x ][ v.y ] + p -> d ;
                    if ( ! f[ p -> t ][ v.y ] ) {
                        f[ p -> t ][ v.y ] = true ;
                        if ( ! q.empty(  ) && dist[ p -> t ][ v.y ] < dist[ q.front(  ).x ][ q.front(  ).y ] ) {
                            q.pf( node( p -> t , v.y ) ) ;
                        } else q.pb( node( p -> t , v.y ) ) ;
                    }
                }
                if ( v.y < k && dist[ p -> t ][ v.y + 1 ] > dist[ v.x ][ v.y ] + p -> d / 2 ) {
                    dist[ p -> t ][ v.y + 1 ] = dist[ v.x ][ v.y ] + p -> d / 2 ;
                    if ( ! f[ p -> t ][ v.y + 1 ] ) {
                        f[ p -> t ][ v.y ] = true ;
                        if ( ! q.empty(  ) && dist[ q.front(  ).x ][ q.front(  ).y ] > dist[ p -> t ][ v.y + 1 ] ) {
                            q.pf( node( p -> t , v.y + 1 ) ) ;
                        } else q.pb( node( p -> t , v.y + 1 ) ) ;
                    }
                }
            }
        }
        int ans = inf ;
        for ( int i = 0 ; i <= k ; ++ i ) ans = min( ans , dist[ n ][ i ] ) ;
        printf( "%d\n" , ans ) ;
        return 0 ;
    }
    

    相关文章

      网友评论

        本文标题:BZOJ-2662: [BeiJing wc2012]冻结(SP

        本文链接:https://www.haomeiwen.com/subject/kowkfqtx.html