让邻接矩阵做乘法,对于不能走的,因为t只有234,所以构造12个矩阵然后分k/12和k%12做就行
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 10000 7 #define ll long long 8 #define succ(x) (1<<x) 9 #define lowbit(x) (x&(-x)) 10 #define NM 100 11 using namespace std; 12 int read(){ 13 int x=0,f=1;char ch=getchar(); 14 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 15 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 16 return x*f; 17 } 18 struct mat{ 19 int a[NM][NM]; 20 }a[13],ans,null; 21 int n,m,S,T,k,p,_x,_y,_t; 22 mat operator*(const mat&x,const mat&y){ 23 mat s; 24 mem(s.a); 25 inc(i,1,n) 26 inc(j,1,n) 27 inc(k,1,n)(s.a[i][j]+=x.a[i][k]*y.a[k][j])%=inf; 28 return s; 29 } 30 mat mul(int k){ 31 mat s=null,t=a[0]; 32 for(;k;k>>=1,t=t*t)if(k&1)s=s*t; 33 return s; 34 } 35 void out(mat t){ 36 inc(i,1,n){ 37 inc(j,1,n)printf("%d ",t.a[i][j]);putchar('\n'); 38 }putchar('\n'); 39 } 40 int main(){ 41 freopen("data.in","r",stdin); 42 n=read();m=read();S=read()+1;T=read()+1;k=read(); 43 mem(null.a); 44 inc(i,1,n)null.a[i][i]=1; 45 inc(i,1,m){ 46 _x=read()+1;_y=read()+1; 47 a[1].a[_x][_y]++;a[1].a[_y][_x]++; 48 } 49 inc(i,2,12)a[i]=a[1]; 50 p=read(); 51 while(p--){ 52 _t=read(); 53 inc(i,0,_t-1){ 54 _x=read()+1; 55 for(int j=i;j<=12;j+=_t)if(j) 56 inc(k,1,n)a[j].a[k][_x]=0; 57 } 58 } 59 a[0]=a[1];inc(i,2,12)a[0]=a[0]*a[i]; 60 ans=mul(k/12); 61 inc(i,1,k%12)ans=ans*a[i]; 62 printf("%d\n",ans.a[S][T]); 63 return 0; 64 }