把边当做一个状态(orz居然有这种想法),初始点也看成边,然后矩阵快速幂就可以了(感觉有点像flyod)
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(int i=l;i<=r;i++) 3 #define dec(i,l,r) for(int i=l;i>=r;i--) 4 #define link(x) for(edge *j=h[x];j;j=j->next) 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define inf 45989 7 #define ll long long 8 #define succ(x) (1<<x) 9 #define NM 300 10 using namespace std; 11 int read(){ 12 int x=0,f=1;char ch=getchar(); 13 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 14 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 15 return x*f; 16 } 17 struct mat{ 18 int a[NM][NM]; 19 }c,_t; 20 int n,m,S,T,a[NM],b[NM],ans; 21 mat operator*(const mat&x,const mat&y){ 22 mat s;mem(s.a); 23 inc(i,1,m) 24 inc(j,1,m) 25 inc(k,1,m) 26 (s.a[i][j]+=x.a[i][k]*y.a[k][j]%inf)%=inf; 27 return s; 28 } 29 ll k; 30 int main(){ 31 freopen("data.in","r",stdin); 32 n=read();m=read();k=read();S=read()+1;T=read()+1; 33 inc(i,1,m){ 34 a[i*2-1]=b[i*2]=read()+1;b[i*2-1]=a[i*2]=read()+1; 35 } 36 m<<=1; 37 inc(i,1,m)c.a[i][i]=1; 38 inc(i,1,m) 39 inc(j,1,m) 40 if((i-1)/2!=(j-1)/2&&b[i]==a[j])_t.a[i][j]=1; 41 /*inc(i,1,m){ 42 inc(j,1,m)printf("%d ",_t.a[i][j]);printf("\n");}*/ 43 for(k--;k;k>>=1,_t=_t*_t) 44 if(k&1)c=c*_t; 45 /* inc(i,1,m){ 46 inc(j,1,m)printf("%d ",c.a[i][j]);printf("\n");}*/ 47 inc(i,1,m)if(a[i]==S) 48 inc(j,1,m)if(b[j]==T) 49 (ans+=c.a[i][j]%inf)%=inf; 50 printf("%d\n",ans); 51 return 0; 52 }