求关系的传递闭包
来源:互联网 发布:java实用教程第4版答案 编辑:程序博客网 时间:2024/05/18 01:38
1.题目描述:
成绩10开启时间2017年04月26日 星期三 14:55折扣0.8折扣时间2017年05月15日 星期一 14:55允许迟交否关闭时间2017年06月1日 星期四 14:55
输入
一次输入一个关系矩阵,每一行两个相邻元素之间用一个空格隔开,输入元素的行与列分别对应关系矩阵的行与列。关系的基数小于12。
输出
输出该关系的传递闭包所对应的关系矩阵。
友情提示:可以使用while (scanf("%d",&a)!=EOF)
- 0 1 0 0↵
- 1 0 1 0↵
- 0 0 0 1↵
- 0 1 0 0↵
- 1 1 1 1↵
- 1 1 1 1↵
- 1 1 1 1↵
- 1 1 1 1↵
书上伪代码已经写的够清楚了。之前学过最短路的floyd发现和这个好像啊。然后用floyd算法敲了一遍。还有个点在于矩阵阶数,这个可以考虑先读完所有矩阵,假设总数为n,则阶数肯定是sqrt(n),然后再构造一个矩阵数组进行floyd就行。
4.AC代码:
#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <functional>#include <cmath>#include <vector>#include <queue>#include <deque>#include <stack>#include <map>#include <set>#include <ctime>#define INF 0x3f3f3f3f#define maxn 100100#define lson root << 1#define rson root << 1 | 1#define lent (t[root].r - t[root].l + 1)#define lenl (t[lson].r - t[lson].l + 1)#define lenr (t[rson].r - t[rson].l + 1)#define N 130#define eps 1e-6#define pi acos(-1.0)#define e exp(1.0)using namespace std;const int mod = 1e9 + 7;typedef long long ll;int a[N], mp[13][13];void floyd(int n){for (int k = 1; k <= n; k++)for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)mp[i][j] = max(mp[i][j], mp[i][k] && mp[k][j] ? 1 : 0);}int main(){#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);long _begin_time = clock();#endifint cnt = 1;while (~scanf("%d", &a[cnt++]));int n = sqrt(cnt), count = 0;for (int i = 1; i <= n; i++)for (int j = 1; j <= n; j++)mp[i][j] = a[++count];floyd(n);for (int i = 1; i <= n; i++){for (int j = 1; j <= n; j++)if (j == 1)printf("%d", mp[i][j]);elseprintf(" %d", mp[i][j]);puts("");}#ifndef ONLINE_JUDGElong _end_time = clock();printf("time = %ld ms.", _end_time - _begin_time);#endifreturn 0;}
0 0
- 求关系的传递闭包
- Floyd求传递闭包
- floyd求传递闭包
- floyd求传递闭包
- floyd 求传递闭包
- 离散数学实践:关系的性质判断&关系的幂&关系的传递闭包
- Floyd算法求图的传递闭包
- 一种离散数学中求传递闭包的算法实现
- 关于3种求传递闭包的方法
- Floyd-Warshall算法求矩阵的传递闭包
- 数据库原理之求关系的闭包
- Warshall算法求传递闭包
- Floyed求传递闭包 poj 3660
- poj 3660(Floyd求传递闭包)
- 离散题目18--求传递闭包
- POJ 3660 Floyd求传递闭包
- bitset优化Floyd求传递闭包
- 【离散数学实验】关系R的幂运算及其传递闭包的计算
- 雷神课程
- 总结redis的连接方式
- Codeforces Round #411(Div. 2)C. Find Amir【思维】
- mysql常用函数整理
- 查询每个年龄的顾客消费金额数的SQL语句
- 求关系的传递闭包
- 蓝桥杯练习系统 合并石子
- StarCitizen星际公民添加好友一起玩教程
- ceph format2格式image
- 提示框的一些需要注意的小细节
- Docker 停机扩容
- ios开发中SVN的使用
- 蓝桥杯练习系统 拿糖果
- JS跨域:No 'Access-Control-Allow-Origin' header is present on the requested resource