How many ways??
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3219 Accepted Submission(s): 1227
接下来的T行, 每行有三个整数 A, B, k, 表示问你从A 点到 B点恰好经过k个点的方案数 (k < 20), 可以走重复边。如果不存在这样的走法, 则输出0
当n, m都为0的时候输入结束
#include<bits/stdc++.h>
using namespace std;
int N;
struct matrix
{
int a[35][35];
matrix(){memset(a,0,sizeof(a));}
};
matrix mul(matrix x,matrix y)
{
matrix ans;
memset(ans.a,0,sizeof(ans.a));
for(int i=1;i<=N;++i)
for(int k=1;k<=N;++k)
for(int j=1;j<=N;++j)
ans.a[i][j]=(ans.a[i][j]+(x.a[i][k]*y.a[k][j])%1000)%1000;
return ans;
}
matrix qpow(matrix x,int n)
{
matrix ans;
for(int i=1;i<=N;++i) ans.a[i][i]=1;
for(;n;n>>=1,x=mul(x,x)){
if(n&1) ans=mul(ans,x);
}
return ans;
}
int main()
{
int n,m,i,j,k;
int u,v,w;
matrix x;
while(cin>>n>>m){ memset(x.a,0,sizeof(x.a));N=n;
if(m==0&&n==0) break;
for(i=1;i<=m;++i){
cin>>u>>v;
x.a[1+u][1+v]=1;
}
int t;
cin>>t;
while(t--){ matrix temp;
cin>>u>>v>>k;
temp=qpow(x,k);
printf("%d
",temp.a[u+1][v+1]);
}
}
return 0;
}
sb题目,n|m有一个为零也行,md检查半天ccccc
DP三维的死活WA不管了