• [BZOJ2963][JLOI2011]飞行路线 分层图+spfa


    Description

    Alice和Bob现在要乘飞机旅行,他们选择了一家相对便宜的航空公司。该航空公司一共在n个城市设有业务,设这些城市分别标记为0到n-1,一共有m种航线,每种航线连接两个城市,并且航线有一定的价格。Alice和Bob现在要从一个城市沿着航线到达另一个城市,途中可以进行转机。航空公司对他们这次旅行也推出优惠,他们可以免费在最多k种航线上搭乘飞机。那么Alice和Bob这次出行最少花费多少?

    Input

    数据的第一行有三个整数,n,m,k,分别表示城市数,航线数和免费乘坐次数。
    第二行有两个整数,s,t,分别表示他们出行的起点城市编号和终点城市编号。(0<=s,t<n)
    接下来有m行,每行三个整数,a,b,c,表示存在一种航线,能从城市a到达城市b,或从城市b到达城市a,价格为c。(0<=a,b<n,a与b不相等,0<=c<=1000)
     

    Output

     
    只有一行,包含一个整数,为最少花费。

    Sample Input

    5 6 1
    0 4
    0 1 5
    1 2 5
    2 3 5
    3 4 5
    2 3 3
    0 2 100

    Sample Output

    8

    HINT

     对于30%的数据,2<=n<=50,1<=m<=300,k=0;
    对于50%的数据,2<=n<=600,1<=m<=6000,0<=k<=1;
    对于100%的数据,2<=n<=10000,1<=m<=50000,0<=k<=10. 

    Solution

    做法:分层图+$spfa$

    大概是叫分层图吧...感觉和$dp$一个样子

    就是给$dis$数组多一个属性:用了多少次免费

    更新的时候多$for$一遍更新整个$dis$数组就行了

    两种转移:

    $dis[v][k]=min(dis[v][k],d[u][k]+e[i].v)$

    $dis[v][k]=min(dis[v][k],dis[u][k-1])$

    $k$表示目前一共用了$k$次免费次数

    然后直接跑一遍$spfa$就行了,$k<=10$,$spfa$的常数大了十倍。不过能过就是了

    还有就是注意队列用下循环队列,不然空间爆炸

    #include <bits/stdc++.h>
    
    using namespace std ;
    
    #define N 1000010
    #define inf 0x3f3f3f3f
    
    int n , m , k , s , t ;
    int cnt , head[ N ] ;
    int q[ N ] , d[ N ][ 11 ] ;
    bool vis[ N ] ;
    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() {
        memset( d , 0x3f , sizeof( d ) ) ;
        for( int i = 0 ; i <= k ; i ++ ) d[ s ][ i ] = 0 ;
        q[ 1 ] = s ;vis[ s ] = 1 ;
        int l = 1 , r = 2 ;
        while( l != r ) {
            int u = q[ l ++ ] ;
            vis[ u ] = 0 ;
            if( l == 500000 ) l = 1 ;
            for( int i = head[ u ] ; i ; i = e[ i ].nxt ) {
                int v = e[ i ].to ,now = 0 ;
                for( int j = 0 ; j <= k ; j ++ ) {
                    if( !j ) now = d[ u ][ j ] + e[ i ].v ;
                    else now = min( d[ u ][ j - 1 ] , d[ u ][ j ] + e[ i ].v ) ;
                    if( d[ v ][ j ] > now ) {
                        d[ v ][ j ] = now ;
                        if( !vis[ v ] ) vis[ v ] = 1 , q[ r ++ ] = v ;
                        if( r == 500000 ) r = 1 ;
                    }
                }
            }
        }
    }
    
    int main() {
        scanf( "%d%d%d%d%d" , &n , &m , &k , &s , &t ) ;
        s ++ ; t ++ ;
        for( int i = 1 ; i <= m ; i ++ ) {
            int u , v , w ;
            scanf( "%d%d%d" , &u , &v , &w ) ;
            u ++ , v ++ ;
            ins( u , v , w ) ;
            ins( v , u , w ) ;
        }
        spfa() ;
        int ans = inf ;
        for( int i = 0 ; i <= k ; i ++ ) {
            ans = min( ans , d[ t ][ i ] ) ;
        }
        printf( "%d
    " , ans ) ;
        return 0 ;
    }
  • 相关阅读:
    程序Dog的大梦想
    图的存储结构
    c语言:函数的递归调用
    c语言:自增自减运算符的操作详解
    Shell 传递参数
    Shell 概述、截取字符操作等
    Python Falling back to the 'python' engine because the 'c' engine does not support regex separators
    Python IOError: [Errno 22] invalid mode ('r') 解决方法
    正则表达式 基础语法
    Hive 常用函数
  • 原文地址:https://www.cnblogs.com/henry-1202/p/BZOJ2963.html
Copyright © 2020-2023  润新知