CSU-1849 Comparing answers(矩阵)

1849: Comparing answers

            Time Limit: 10 Sec       Memory Limit: 128 Mb       Submitted: 11       Solved: 6    

Description

In a place in Southwestern Europe, the name of which I do not wish to recall, not long ago there were n cities connected by unidirectional roads, with possibly more than one road connecting a city to another one, or even to itself. As a homework assignment for your geography class, you need to calculate the number of paths of length exactly two that were between each pair of cities. However, you've been too busy celebrating the Spanish victory in the World Cup, so now you are copying the answers from your friend. You would like to make sure his answers are correct before handing in your homework.

Input

  The input consists of several test cases, separated by single blank lines. Each test case begins with a line containing the integer n (  1n1000 1≤n≤1000 ). The following n lines contain n elements each, with element j of line i being the number of roads from city i to city j (a number between 0 and 10, inclusive). After that, there will be n lines. Each will contain n elements, with element j of line i being the answer from your friend for the number of length-2 paths from city i to city j; it will be an integer between 0 and 100 000 inclusive.

  The test cases will finish with a line containing only the number zero (also preceded by a blank line).

  Note: Large input file , use fast I/O routines.

Output

  For each case, your program should output a line. The content of this line should be YES if your classmate's solution to the assignment is right, and NO otherwise.

Sample Input

3
2 0 1
1 0 3
1 1 0
5 1 2
5 3 1
3 0 4

3
2 0 1
1 0 3
1 1 0
5 1 2
5 3 2
3 0 4

0

Sample Output

YES
NO

题意:有n个城市,编号为0~n-1,现在给出n*n的矩阵A,如果城市u与城市v之间有w条直接相连的路,那么A[u][v]=w,再给出n*n的矩阵B,
B[u][v]表示从u到v有B[u][v]条不同的路径,问题是:B所描述的路径数是否正确

题解:根据离散数学的定义,令G=A*A,那么G[u][v]表示的就是从u到v的路径总数
因为n<=1000,O(n^3)矩阵乘法不能用;考虑到如果矩阵A*B=C,那么对于任意非0矩阵X,总是有X*A*B=X*C
于是我们可以开一个1*n的矩阵X,比较X*A*A是否等于X*B即可,这样复杂度可以降到O(n^2)

#include<cstdio>
#include<algorithm>
#include<string.h>
#include<time.h>
using namespace std;
typedef unsigned long long ULL;
const int MX = 1005;
int n;
ULL A[MX][MX],B[MX][MX],X[MX],Y[MX];
void init(){
    for(int i=0;i<n;i++) {
        X[i]=rand()*rand()+1;
        Y[i]=X[i];
    }
}
ULL tmp[MX];
void MatCross(ULL X[],ULL A[MX][MX]){
    memset(tmp,0,sizeof(tmp));
    for(int k=0;k<n;k++){
        for(int i=0;i<1;i++){
            for(int j=0;j<n;j++){
                tmp[j]+=X[k]*A[k][j];
            }
        }
    }
    for(int i=0;i<n;i++) X[i]=tmp[i];
}
bool check(ULL A[MX],ULL B[MX]){
    for(int i=0;i<n;i++) {
        if(A[i]!=B[i]) return 0;
    }
    return 1;
}
int main(){
    //freopen("in.txt","r",stdin);
    srand((unsigned)time(NULL));
    while(scanf("%d",&n),n){
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%llu",&A[i][j]);
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%llu",&B[i][j]);
        //对于矩阵A,B,C,如果A*B=C,那么X*A*B=X*C
        init();
        MatCross(X,A);
        MatCross(X,A);
        MatCross(Y,B);
        printf("%s\n",check(X,Y)?"YES":"NO");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值