How many ways??
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3151 Accepted Submission(s): 1202
接下来的T行, 每行有三个整数 A, B, k, 表示问你从A 点到 B点恰好经过k个点的方案数 (k < 20), 可以走重复边。如果不存在这样的走法, 则输出0
当n, m都为0的时候输入结束
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 1 3
把给定的图转为邻接矩阵,即A(i,j)=1当且仅当存在一条边i->j。令C=A*A,那么C(i,j)=ΣA(i,k)*A(k,j),实际上就等于从点i到点j恰好经过2条边的路径数(枚举k为中转点)。类似地,C*A的第i行第j列就表示从i到j经过3条边的路径数。同理,如果要求经过k步的路径数,我们只需要二分求出A^k即可。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int > P;
typedef long long LL;
const int maxn=100;
#define N 25
#define mod 1000
struct Matrix
{
LL mat[N][N];
Matrix()
{
memset(mat,0,sizeof(mat));
}
};
Matrix mul(Matrix a,Matrix b)
{
Matrix res;
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
{
for(int k=0; k<N; k++)
{
res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
res.mat[i][j]%=mod;
}
}
return res;
}
Matrix pow_matrix(Matrix a,LL n)//矩阵快速幂;
{
Matrix res;
for(int i=0; i<N; i++) //初始化为单位矩阵;
res.mat[i][i]=1;
while(n!=0)
{
if(n&1)
res=mul(res,a);
a=mul(a,a);
n>>=1;
}
return res;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
Matrix A;
if(n==0&&m==0)
break;
int x,y;
for(int i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
A.mat[x][y]=1;
}
int T;
scanf("%d",&T);
while(T--)
{
Matrix B;
int s,t;
LL k;
scanf("%d%d%lld",&s,&t,&k);
B=pow_matrix(A,k);
printf("%lld\n",B.mat[s][t]%mod);
}
}
return 0;
}
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 512 mebibytes
As one of the most beautiful campus in China, Wuhan University is around several hills, so the road is complex and visitors always lose themselves. Give a undirected graph of WHU of N points and a deadline, then you should count the number of plan to reach the destination by deadline (the starting point is 1 and the ending point is N).
Input
First line contains the number of points N (N≤100) and the number of edges M (M≤N(N−1)/2).
The i-th line of next M lines contains two numbers ui and vi, which respents an edge (ui,vi).
The last line contains the deadline T(T≤109).
Output
The number of plan to reach the destination by deadline module 109+7.
Examples
Input 1
4 5
1 3
2 3
3 4
1 2
1 4
8
Output 1
170
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
typedef pair<int,int > P;
typedef long long LL;
const int maxn=100;
#define N 105
#define mod 1000000007
struct Matrix
{
LL mat[N][N];
Matrix()
{
memset(mat,0,sizeof(mat));
}
};
Matrix mul(Matrix a,Matrix b)
{
Matrix res;
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
{
for(int k=0; k<N; k++)
{
res.mat[i][j]+=a.mat[i][k]*b.mat[k][j];
res.mat[i][j]%=mod;
}
}
return res;
}
Matrix pow_matrix(Matrix a,LL n)//矩阵快速幂;
{
Matrix res;
for(int i=0; i<N; i++) //初始化为单位矩阵;
res.mat[i][i]=1;
while(n!=0)
{
if(n&1)
res=mul(res,a);
a=mul(a,a);
n>>=1;
}
return res;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
Matrix A;
int x,y;
for(int i=0; i<m; i++)
{
scanf("%d%d",&x,&y);
if(x!=n) //?
A.mat[x-1][y-1]=1;
if(y!=n)
A.mat[y-1][x-1]=1;
}
A.mat[n-1][n-1]=1; //?
LL T;
scanf("%lld",&T);
Matrix B=pow_matrix(A,T);
printf("%lld\n",B.mat[0][n-1]%mod);
return 0;
}