2017武汉大学校赛网络预选赛e题

Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 512 mebibytes
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 NN 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 NN).

Input
First line contains the number of points NN (N\le 100N≤100) and the number of edges MM (M\le N(N-1)/2M≤N(N−1)/2).

The ii-th line of next MM lines contains two numbers u_iu
​i
​​ and v_iv
​i
​​ , which respents an edge (u_i, v_i)(u
​i
​​ ,v
​i
​​ ).

The last line contains the deadline TT(T\le 10^9T≤10
​9
​​ ).

Output
The number of plan to reach the destination by deadline module 10^9+710
​9
​​ +7.

Examples
Input 1
4 5
1 3
2 3
3 4
1 2
1 4
8
Output 1
170

题意:给你一个图,然你找出能在T步之内从1到n的路径数目。

解题思路:有一个结论,就是在一个图中,顶点i到j长度(不是实际长度,走一次算长度加一,可以往返)为K的路径数等于这个图的邻接矩阵乘k次,之后的v[i][j],注意这个路径可以往返。这题的坑点是,初始时v[N][N] = 1,因为可以提前到达N点,v[N][i]对于所有的i,v[N][i] = 0,因为到了N点之后就不能到达其他点。最后T有1e9,所以需要矩阵快速幂弄一下。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
int N,M,T;
struct Matrix{
    int len;
    ll v[102][102];
};
Matrix Matrix_mul(Matrix m1,Matrix m2)
{
    Matrix ans;
    ans.len = m1.len;
    for(int i = 1; i <= m1.len; i++)
    {
        for(int j = 1; j <= m1.len; j++)
        {
            ans.v[i][j] = 0;
            for(int k = 1; k <= m1.len; k++)
            {
                ans.v[i][j] += (m1.v[i][k]*m2.v[k][j])%mod;
            }
            ans.v[i][j] %= mod;
        }
    }
    return ans;
}
Matrix Matrix_pow(int x,Matrix m)
{
    int len = m.len;
    Matrix ans;
    ans.len = len;
    for(int i = 1; i <= len; i++)
    {
        for(int j = 1; j <= len; j++)
        {
            if(i == j) ans.v[i][j] = 1;
            else ans.v[i][j] = 0;
        }
    }
    while(x)
    {
        if(x&1) ans = Matrix_mul(ans,m);
        x >>= 1;
        m = Matrix_mul(m,m);
    }
    return ans;
}
int main()
{
    scanf("%d%d",&N,&M);
    Matrix res;
    res.len = N;
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            res.v[i][j] = 0;
        }
    }
    int from,to;
    for(int i = 1; i <= M; i++)
    {
        scanf("%d%d",&from,&to);
        res.v[from][to] = 1;
        res.v[to][from] = 1;
    }
    for(int i = 1; i < N; i++)
    {
        res.v[N][i] = 0;
    }
    res.v[N][N] = 1;
    scanf("%d",&T);
    Matrix result = Matrix_pow(T,res);
    printf("%lld\n",result.v[1][N]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值