[SCOI2009]迷路

[SCOI2009]迷路

问题简化 \(:\)

给定一张 \(n\) 个点的带权有向图,求以 \(0\) 号节点为起点且以 \(n-1\) 号节点为终点的长度为 \(T\) 的路径有多少条.

考虑,如果不带权,即所有边权都为 \(1\) , 那么就是一个矩阵加速 \(Floyd\) 的裸题.

那么带权怎么做呢?和以前一样重定义矩阵的方法是否可行呢 \(?\)

我们发现,由于边权的不统一,矩阵的状态表示变得扑朔迷离,就是说你现在不知道矩阵里的状态到底是什么了.(当然这可能只是因为我比较屑...)

那么能不能把整张图通过某种方式变成一张 \(01\) 图呢 \(?\)

你发现每个点的点权不超过 \(9\) , 这点值得考虑.

如果我们把每个点拆点为 \(9\) 个点,每个拆出来的点都向与它相邻的点连一条边权为 \(1\) 的边,那么我们就能完整地表达所有的边权.

而且,显然,这不会使得答案路径增多或减少.

那么令 \(idx_{i,j}\) 表示 \(i\) 号点拆出来的第 \(j\) 个点的编号,其中 \(idx_{i,0}\) 为原点.

为了方便我们采用这样的编号方式 \(: idx_{i,j}=i+j\times n\).

那么初始时我们要对于 \(\forall j\)\(idx_{i,j}\)\(idx_{i,j-1}\) 连一条边权为 \(1\) 的边.

假设我们现在有一条这样的边 \(:(u,v,w)\) 表示从 \(u\)\(v\) 的边权为 \(w\) 的边.

应该怎么连边呢 \(?\)

\(idx_{u,0}\)\(idx_{v,w-1}\) 连边,边权呢 \(?\) 显然为 \(1\).

这样整张图就变成了一个 点数 \(\times \: 9\)\(01\) 图.

直接矩阵加速 \(Floyd\) 即可.

最后的答案呢 \(?\) 根据虚点的意义,我们可以知道最后的答案就是 \(e_{1,n}\)

\(Code:\)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define per(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define pii pair < int , int >
#define one first
#define two second
#define rint read<int>
#define int long long
#define pb push_back
#define db double
#define ull unsigned long long
#define lowbit(x) ( x & ( - x ) )

using std::queue ;
using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;

template < class T >
    inline T read () {
        T x = 0 , f = 1 ; char ch = getchar () ;
        while ( ch < '0' || ch > '9' ) {
            if ( ch == '-' ) f = - 1 ;
            ch = getchar () ;
        }
       while ( ch >= '0' && ch <= '9' ) {
            x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
            ch = getchar () ;
       }
       return f * x ;
    }

const int mod = 2009 ;

struct Matrix {
    int M[135][135] , line , row ;

    inline void clear () { MEM ( M , 0 ) ; line = row = 0 ; return ; }

    inline void init () { MEM ( M , 0 ) ; rep ( i , 1 , line ) M[i][i] = 1 ; return ; }

    friend Matrix operator * (Matrix a , Matrix b) {
        Matrix res ; res.clear () ; res.line = a.line ; res.row = b.row ;
        rep ( k , 1 , a.row ) rep ( i , 1 , a.line ) rep ( j , 1 , b.row )
            res.M[i][j] = ( res.M[i][j] + a.M[i][k] * b.M[k][j] % mod ) % mod ;
        return res ;
    }

    friend Matrix operator ^ (Matrix a , int p) {
        Matrix res = a ;
        for ( -- p ; p ; a = a * a , p >>= 1)
            if ( p & 1 ) res = res * a ;
        return res ;
    }

} e ;

int n , m , s , t , k ;
int idx[15][15] , cnt ;
char st[15][15] ;

inline void create_void () {
    rep ( i , 1 , n ) rep ( j , 0 , 8 ) idx[i][j] = i + j * n ;
    rep ( i , 1 , n ) per ( j , 8 , 1 ) e.M[idx[i][j]][idx[i][j-1]] = 1 ;
    return ;
}

inline void link (int u , int v , char c) {
    if ( c == '0' ) return ; int w = c - '0' ;
    e.M[idx[u][0]][idx[v][w-1]] = 1 ; return ;
}

signed main (int argc , char * argv[]) {
    n = rint () ; k = rint () ;
    rep ( i , 1 , n ) scanf ("%s" , st[i] + 1 ) ;
    e.clear () ; e.line = e.row = n * 9 ; create_void () ;
    rep ( i , 1 , n ) rep ( j , 1 , n ) link ( i , j , st[i][j] ) ;
    e = e ^ k ; printf ("%lld\n" , e.M[1][n] ) ;
    system ("pause") ; return 0 ;
}
posted @ 2019-10-25 10:35  Phecda  阅读(121)  评论(0编辑  收藏  举报

Contact with me