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

来源:互联网 发布:有数据分析软件吗 编辑:程序博客网 时间:2024/05/09 01:54

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
原创粉丝点击