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;}
- 2017武汉大学校赛网络预选赛e题
- 2017武汉大学校赛网络预选赛b题
- 2017武汉大学校赛网络预选赛g题
- 2017武汉大学校赛网络预选赛d题
- 2017武汉大学校赛 网络预选赛 简单题题解
- 2017武汉大学校赛网络预选赛c题
- 武汉大学校赛总结 + E题+F题
- 武汉大学校赛(个人总结+补题)
- 2017浙江理工大学校赛E题
- 2017华中区邀请赛暨武汉大学校赛网络赛小结 + WOJ 642 Lost in WHU
- 2016年武汉科技大学邀请赛网络赛 E题
- 2017华中地区邀请赛暨武汉大学校赛现场赛小结
- 2014牡丹江网络预选赛E题(线段树)zoj3813
- 积木积水(广东工业大学校赛决赛2016E题)
- 校赛网络预选赛小结
- 2017ccpc网络预选赛1005
- 长安大学校赛-2017
- 2017武大校赛网络预选赛C题 Divide by Six
- bzoj 3275: Number 最小割
- 数据结构与算法(c++)--prim算法
- LeetCode 148. Sort List
- golang学习笔记之yaml文件处理
- 感觉非常棒的ssm框架搭建
- 2017武汉大学校赛网络预选赛e题
- 在Eclipse中使用Mybatis
- Oracle的trunc函数
- Android常用的15个框架
- 12 学习阶段总结
- 登录界面及记住密码的实现
- CSS"盒子模型"
- Spring配置文件报Referenced file contains errors :beans/factory/xml/spring-beans-4.3.xsd
- [C++](转载)虚函数表解析