Sicily1211(矩阵相乘)

来源:互联网 发布:淘宝降价通知如何设置? 编辑:程序博客网 时间:2024/05/21 09:27

邻接矩阵相乘n次后得到矩阵A,A[i][j]表示从i出发恰好走过n条边到达j的情况数。

#include <cstdio>#include <iostream>using namespace std;int main(){    int n,m,L;    scanf("%d%d%d",&n,&m,&L);    int i,j;    int A[101][101]={{0}};    int a,b;    for (i=1;i<=m;i++)    {        scanf("%d%d",&a,&b);        A[a][b]=1;    }    int ans[101][101];    for (i=1;i<=n;i++)        for (j=1;j<=n;j++)            ans[i][j]=A[i][j];    L--;    int anstemp[101][101];    while (L--)    {        int x,y;        int temp=0;        for (x=1;x<=n;x++)            for (y=1;y<=n;y++)            {                for (i=1;i<=n;i++)                    temp+=ans[x][i]*A[i][y];                anstemp[x][y]=temp;                temp=0;            }        for (x=1;x<=n;x++)            for (y=1;y<=n;y++)                ans[x][y]=anstemp[x][y];    }    int q;    int s,e;    scanf("%d",&q);    for (i=1;i<=q;i++)    {      scanf("%d%d",&s,&e);      printf("%d\n",ans[s][e]);    }    return 0;}