2017 Wuhan University Programming Contest --Lost in WHU

As one of the most beautiful campus in China, Wuhan University is around several hills, so the road is complex and visitors always lose themselves. Give a undirected graph of WHU of N points and a deadline, then you should count the number of plan to reach the destination by deadline (the starting point is 1 and the ending point is N).​
Output
First line contains the number of points
N (N≤100) and the number of edges
M (M≤N(N−1)/2).
The i-th line of next M lines contains two numbers
ui and vi which respents an edge (ui,vi)The last
line contains the deadline T(T≤10^9).mples
Input 1
4 5
1 3
2 3
3 4
1 2
1 4
8
Output 1
170
send
tput 1
170

套路,矩阵快速幂!
题意:N座小山,要求你在给定的M秒内从1到N,求总共有多少条路线。
首先我们如果深搜的话,铁定完蛋,T<=10^9啊!!!
再看一下山的个数最多才100所以用关系矩阵表示其路线数mat[i][j];
mat[i][j]的n次幂表示在n条路径之内从i到j有多少条路径!
以下证明:
cij = sigma(aik*bkj) k from 1 to N 由其容易看出结论正确。

#include <iostream>
#include <string.h>
using namespace std;
long long MOD = 1000000007;
int N,M; 
struct matrix{
    long long mat[101][101];
//数组声明为long long 否则会WA
};
matrix operator *(matrix &a,matrix &b){
    matrix c;
    for(int i=1;i<=N;i++){
        for(int j=1;j<=N;j++){
            c.mat[i][j] = 0;
            for(int k=1;k<=N;k++){
                c.mat[i][j]+=(a.mat[i][k]*b.mat[k][j])%MOD;
                c.mat[i][j]%=MOD;
            }
        }
    }
    return c;
}
matrix pow(matrix a,long long b){
    matrix ans;
    for(int i=1;i<=N;i++){
        ans.mat[i][i] = 1;//单位矩阵 相当于1
    }
    while(b){
        if(b&1){
            ans = a*ans;
        }
        a = a*a;
        b/=2;
    }
    return ans;
}
int main()
{
    cin>>N>>M;
    matrix a;
    for(int i=0;i<M;i++){
        int u,v;cin>>u>>v;
        if(u!=N)a.mat[u][v] = 1;//将所有从N到其他路径的路线断掉等价于到N就停下
        if(v!=N)a.mat[v][u] = 1;
    }
    long long T;cin>>T;
    a.mat[N][N] = 1; 
//使其为1等价于每次加上自己
    cout<<pow(a,T).mat[1][N]<<endl;
    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值