POJ-3660 Cow Contest (floyd+传递闭包)

来源:互联网 发布:傅红雪捏脸数据 编辑:程序博客网 时间:2024/05/19 14:16

题目大意:

题目大概就是说,有n头牛,对比m头牛后,能确定具体排名的牛的数量是多少

算法分析:

一开始我想着是不是用dijkstra算法做(已中毒),后来发现死活做不了,那我就去找博客看解析。 最后才发现只要用传递闭包做就行了,我稍微了解了一下传递闭包后,就跑去自己实现了,返现效果不是很理想。 于是又开始找传递闭包的实现,最后终于把传递闭包实现了出来= =。。。

代码实现:

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;int n, m;int edge[110][110];int floyd(){    int count_ = 0;    for (int i = 1; i <= n; i++) {        for (int j = 1; j <= n; j++) {            for (int k = 1; k <= n; k++) {                if (edge[j][i] && edge[i][k]) {                    edge[j][k] = 1;                }            }        }    }    for (int i = 1; i <= n; i++) {        int tmp = 0;        for (int j = 1; j <= n; j++) {            if (edge[i][j])                tmp++;            if (edge[j][i])                tmp++;        }        if (tmp == n-1)            count_++;    }    return count_;}int main(){    int x, y;    while (scanf("%d%d", &n, &m) != EOF) {        for (int i = 1; i <= m; i++) {            scanf("%d%d", &x, &y);            edge[x][y] = 1;        }        printf("%d\n", floyd());    }    return 0;}


0 0
原创粉丝点击