求关系的传递闭包

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)

  测试输入关于“测试输入”的帮助 期待的输出关于“期待的输出”的帮助 时间限制关于“时间限制”的帮助 内存限制关于“内存限制”的帮助 额外进程关于“{$a} 个额外进程”的帮助
测试用例 1 以文本方式显示
  1. 0 1 0 0↵
  2. 1 0 1 0↵
  3. 0 0 0 1↵
  4. 0 1 0 0↵
以文本方式显示
  1. 1 1 1 1↵
  2. 1 1 1 1↵
  3. 1 1 1 1↵
  4. 1 1 1 1↵
1秒 64M 0
3.解题思路:

书上伪代码已经写的够清楚了。之前学过最短路的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_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	int 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]);
			else
				printf(" %d", mp[i][j]);
		puts("");
	}
#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值