Description
HH has a constant habit, like hundred steps after a meal. The so-called hundred step Walk, is a walk, is in a certain time, through a certain distance. But at the same time HH is a person who likes to change, so he will not immediately follow the road just walked back. And because HH is a person who likes to change, so he walks the path every day is not exactly the same, he wants to know how many ways he has to walk. Now give your school map (assuming that each road is the same length is 1), ask the length of T, from a given location A to a given location B total number of eligible paths
Input
First line: five integer n,m,t,a,b. where n indicates the number of intersections in the school, m indicates the number of roads in the school, T indicates the distance that HH wants to walk, a is the starting point for a walk, and B represents the end of the walk. The next m line, a set of Ai,bi per row, represents a road from the intersection AI to the intersection Bi. The data guarantees AI = Bi, but there is no guarantee that there is at most one route connected between any two intersections. Intersection number from 0 to n? 1. All data in the same row is separated by a space, and there is no extra space at the end of the line. There are no extra empty lines. Answer modulus 45989.
Output
A line that represents the answer.
Sample Input
4 5 3) 0 0
0 1
0 2
0 3
2 1
3 2
Sample Output
4
HINT
For 30% of data, n≤4,m≤10,t≤10.
For 100% of data, n≤20,m≤60,t≤230,0≤a,b
Source
Day1
Moment by DP.
Construct a matrix based on the relationship of the edges and then do the fast power.
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#define MAXN#define P 45989using namespace Std;intNm, T,a,b;inttop=1, size;intAns;struct edge{intFrom,to,num; Edge*next;} e[maxn<<1],*prev[Maxn];void Insert (intUintV) {E[++top].to=v;e[top].Next=prev[u];p rev[u]=&e[top];e[top].num=top;e[top].from=u;} struct matrix{intA[MAXN][MAXN]; Matrix () {memset (A,0, sizeof (a)); } Friend matrix operator*(Matrix A,matrix B) {matrix ret; for(intI=1; i<=size;i++) for(intj=1; j<=size;j++) for(intk=1; k<=size;k++) ret.a[i][j]= (Ret.a[i][j]+a.a[i][k]*b. A[k][j])%P;returnRet } Friend matrix operator ^ (matrixx,intk) {matrix ret; for(intI=1; i<=size;i++) ret.a[i][i]=1; for(inti=k;i;i>>=1,x=x*x)if(i&1) Ret=ret*x;returnRet }}st,tmp;intMain () {scanf ("%d %d%d%d%d",&n,&m, &t,&a,&b); for(intI=1; i<=m; i++) {intU,v; scanf"%d%d", &u,&v); Insert (u,v); insert (v,u); } size=top; for(Edge*i=prev[a];i;i=i->Next) st.a[1][i->num]++; for(intI=2; i<=top;i++) for(intj=2; j<=top;j++)if(E[i].to==e[j].from&&i!= (j^1)) tmp.a[i][j]++; St=st*(tmp^ (t1)); for(Edge*i=prev[b];i;i=i->Next) ans+=st.a[1][i->num^1]; Ans%=Pprintf("%d\ n", ans);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"SDOI2009" "BZOJ1875" hh go for a walk