4 4 0 1 0 2 1 3 2 3 2 0 3 2 0 3 3 3 6 0 1 1 0 0 2 2 0 1 2 2 1 2 1 2 1 0 1 3 0 0
2 0 13
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; #define MOD 1000 struct matrix { int mat[31][31]; matrix(){memset(mat,0,sizeof(mat));} }; int n; matrix mul(matrix A,matrix B) { int i,j,k; matrix C; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { for(k=1;k<=n;k++) { C.mat[i][j]=(C.mat[i][j]+A.mat[i][k]*B.mat[k][j])%MOD; } } } return C; } matrix powmul(matrix A,int k) { matrix B; for(int i=1;i<=n;i++)B.mat[i][i]=1; while(k) { if(k&1)B=mul(B,A); A=mul(A,A); k>>=1; } return B; } int main() { int s,t,m,T,a,b,k; while(~scanf("%d%d",&n,&m)&&(n||m)) { matrix A,B; while(m--) { scanf("%d%d",&s,&t); A.mat[s+1][t+1]=1; } scanf("%d",&T); while(T--) { scanf("%d%d%d",&a,&b,&k); B=A; B=powmul(A,k); cout<<B.mat[a+1][b+1]<<endl; } } return 0; }