zoukankan      html  css  js  c++  java
  • 【离散数学】Warshall算法实现 传递闭包对应矩阵

    测试样例,数据拿离散书上Page 124页测的:

    /*

    7
    1 1 0 0 0 0 0 
    0 0 0 1 0 0 0
    0 0 0 0 1 0 0
    0 1 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0
    0 0 0 0 0 0 0

    */


    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <vector>
    using namespace std;
    
    const int len = 1000 + 100;
    int M[len][len];   //原方阵
    int A[len][len];   //置新后方阵
    int n, i, j;       //n表示方阵行数
    
    void Wareshall()
    {
        for(i = 1; i <= n; i++)
        {
            for(j = 1; j <= n; j++)
            {
                if(A[j][i] == 1)
                {
                    for(int k = 1; k <= n; k++)
                    {
                        A[j][k] = A[j][k] + A[i][k];
                        if(A[j][k] >= 1)
                        {
                            A[j][k] =  1;
                        }
                    }
                }
            }
        }
    }
    
    int main()
    {
        freopen("datain.txt","r",stdin);
        cout << "输入原方阵行数:" << endl;
        cin >> n;
        cout << endl;
        cout << "输入原关系矩阵(方阵):";
        for(i = 1; i <= n; i++)
        {
            for(j = 1; j <= n; j++)
            {
                cin >> M[i][j];
                A[i][j] = M[i][j];
            }
        }
        Wareshall();
        cout << "输出传递闭包对应关系矩阵:" << endl;
        for(i = 1; i <= n; i++)
        {
            for(j = 1; j <= n; j++)
            {
                cout << A[i][j] <<"  ";
            }
            cout << endl;
        }
    
        return 0;
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Oracle FGA审计记录的清理步骤
    UVa11488-Hyper Prefix Sets(trie树)
    配置Log4j(非常具体)
    poj1190生日蛋糕
    zju1610Count the Colors
    【例9.3】求最长不下降序列
    P1364 医院设置
    P1629 邮递员送信
    P1476 休息中的小呆
    P1330 封锁阳光大学
  • 原文地址:https://www.cnblogs.com/Tobyuyu/p/4965344.html
Copyright © 2011-2022 走看看