Time Limit: 20 sec memory limit: 64 MB
Submit: 2333 solved: 1204
[Submit] [Status] [discuss]
Description
HH has a habit of having to remain unchanged and enjoys taking a hundred steps after meals. The so-called "Walking" means walking, that is, walking through a certain distance within a certain period of time. However
At the same time, HH is a change-loving person, so he will not immediately go back along the road he just walked. Because hh is a person who likes to change
Tian's path is not exactly the same. He wants to know how many ways he can walk. Map for your school now (assuming the length of each road is
1). The length of the question is T. How many qualified paths are there from the given location a to the given location B?
Input
The first row: Five integers n, m, T, A, and B.
N indicates the number of intersections in the school.
M indicates the number of routes in the school.
T indicates the distance HH wants to walk
A Indicates the starting point of walking.
B indicates the end of the walk.
Next m rows
Each line contains a group of AI. BI indicates that there is a path from intersection AI to intersection bi.
Data assurance AI! = Bi, but there is no guarantee that there is at most one link between any two intersections.
The intersection number ranges from 0 to n-1.
All data in the same row is separated by a space. There is no extra space at the end of the first line. No extra blank lines.
The answer model is 45989.
N ≤ 20, m ≤ 60, T ≤ 2 ^ 30, 0 ≤ a, B
Output
One line indicates the answer.
Sample Input
4 5 3 0 0
0 1
0 2
0 3
2 1
3 2
Sample output
4
You cannot go back on the road you just walked.
Then it's annoying.
Solution: Split two-way edges into two one-way edges and then create a matrix based on the edges ~ (Too many rows )/~
#include<iostream>#include<cstdio>#define M 45989#define LL long long using namespace std;int i,m,n,j,k,a,b,t, x,y,s[1000][2],ans;struct vv{ int g[200][200];} ch,f,cs,l;vv cheng(vv a,vv b){ vv d=ch; for(int i=1;i<=m+m;i++) for(int j=1;j<=m+m;j++) for(int k=1;k<=m+m;k++) d.g[i][j]=(int)(d.g[i][j]+(LL)a.g[i][k]*b.g[k][j])%M; return d;}vv ksm(vv a,int x){ vv c=cs; for(x;x>1;x>>=1) { if(x&1) c=cheng(c,a); vv v=cheng(a,a); a=v; } return cheng(a,c);}int main(){ scanf("%d%d%d%d%d",&n,&m,&t,&a,&b); for(i=1;i<=m+m;i++) cs.g[i][i]=1; for(i=1;i<=m;i++) scanf("%d%d",&s[i][0],&s[i][1]), s[i+m][0]=s[i][1], s[i+m][1]=s[i][0]; for(i=1;i<=m+m;i++) for(j=1;j<=m+m;j++) if(s[i][1]==s[j][0] && i-j!=m && j-i!=m) f.g[i][j]=1; vv d=ksm(f,t-1); for(i=1;i<=m+m;i++) if(s[i][0]==a) l.g[1][i]=1; l=cheng(l,d); for(i=1;i<=m+m;i++) if(s[i][1]==b) ans=(ans+l.g[1][i])%M; printf("%d",ans);}
1875: [sdoi2009] HH for a walk