码迷,mamicode.com
首页 > 其他好文 > 详细

URAL 1507. Difficult Decision(矩阵快速幂)

时间:2015-03-28 10:12:14      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:ural   数学   矩阵快速幂   

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1507


Often, when a decision about investing in a new business must be taken, a New Russian has to estimate quickly whether a certain project will be a success or not. Leading economists have recently discovered a new algorithm for forecasting the success of a project.
First, one has to form an n × n matrix of risks. Let us denote this matrix by A. Then, in order to take into account the interdependencies of the parameters inside the matrix, the matrix
技术分享
must be computed. If at least one of the elements of the matrix B is zero, then there is a considerable probability that the project will fail. Otherwise, if there are no zero elements in the matrix B, the new business will grow and flourish.
Help New Russians to make use of this algorithm. Your task is to write a program that determines the probability of the success of a project given the matrix of its risks.

Input

The first line of the input contains the dimension n of the matrix A (2 ≤ n ≤ 50). Each of the next n lines contains n numbers that forms the matrix A. Each element is a whole number in the range from 0 to 100.

Output

Output "No" if there is at least one zero element in the matrix B (so it is better not to invest in the new business). Otherwise, output "Yes".

Samples

input output
2
0 7
15 30
Yes
3
100 35 40
0 22 0
10 11 0
No

PS:

正解是用bool把相乘的结果中不为零的用true代替,不然会爆!


这是队友的:

#include<stdio.h>
#include<string.h>

struct Matrix
{
    int m[51][51];
};
struct Matrix I,s;
int n,kmod;

Matrix Mul(Matrix a,Matrix b)
{
    Matrix c;
    int i,j,k;
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            c.m[i][j]=0;
            for(k=0; k<n; k++)
            {
                c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
            }
            if(c.m[i][j]>0) c.m[i][j]=1;
            else c.m[i][j]=0;
        }
    }
    return c;
}

Matrix Add(Matrix a,Matrix b)
{
    Matrix c;
    int i,j;
    for(i=0; i<n; i++)
    {
        for(j=0; j<n; j++)
        {
            c.m[i][j]=(a.m[i][j]+b.m[i][j]);
            if(c.m[i][j]>0) c.m[i][j]=1;
            else c.m[i][j]=0;
        }

    }
    return c;
}

Matrix Quickpow(Matrix a,int n)
{
    Matrix m,b;
    m=a,b=I;
    while(n)
    {
        if(n%2)
            b=Mul(b,m);
        n/=2;
        m=Mul(m,m);
    }
    return b;
}


int main()
{
    int i,j;
    int k;
    while(scanf("%d",&n)!=EOF)
    {
        Matrix ans;
        memset(I.m,0,sizeof I.m);
        memset(ans.m,0,sizeof ans.m);
        for(i=0; i<n; i++)
            I.m[i][i]=1;
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                scanf("%d",&s.m[i][j]);
                if(s.m[i][j]>0) s.m[i][j]=1;
                else s.m[i][j]=0;
            }
        }

        for(i=n*(n-1); i<=n*(n+1); i++)
            ans=Add(ans,Quickpow(s,i));
        int flag=1;
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(ans.m[i][j]==0)
                    flag=0;
            }
        }
        if(flag) puts("Yes");
        else puts("No");
    }

    return 0;
}



URAL 1507. Difficult Decision(矩阵快速幂)

标签:ural   数学   矩阵快速幂   

原文地址:http://blog.csdn.net/u012860063/article/details/44698643

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!