floyd算法 + 传递闭包 (poj3660Cow Contest)

来源:互联网 发布:数控车用什么软件编程 编辑:程序博客网 时间:2024/06/05 17:06

一、floyd算法

参考:http://blog.csdn.net/niushuai666/article/details/6772706      主要还是参考的dalao的博客理解

  floyd算法用于求最短路,即两点之间最短距离,是Warshall算法的拓展。

  基本思想:从节点A到节点B,只有两种可能:

     1.从A直接到B

     2.从A经过若干节点到B

    对于第二种情况,我们假设dis(AB)为A到B的最短路,然后检查节点C,判断dis(AC)+dis(CB)<dis(AB)是否成立,若是成立,更新dis(AB) = dis(AC)+dis(CB);在寻找AB最短的过程中,我们遍历所有的C点,对于整个图,我们遍历所有的ABC点,即三层for循环,代码如下:

for (int i = 1; i <= n; i++) {//n为节点个数    for (int j = 1; j <= n; j++) {        for (int k = 1; k <= n; k++) {            if (dis[i][k] + dis[k][j] > dis[i][j])                dis[i][j] = dis[i][k] + dis[k][j];        }    }}


但是我们要注意一个问题,即A的遍历顺序问题。上述代码中,i-j的最短路被过早的确定下来了。如下图

在寻找AB的时候,我们只能找到9,因为AD,DC,AC,BC的最短路并没有求出,主要是D点还没有遍历,导致相关路线也找不到最短路。

错误的主要原因是AB被过早的遍历了,由此,我们可以先遍历中间节点,然后遍历收尾节点,具体可以自己演示一遍,代码如下:

for (int k = 1; k <= n; k++) {//n为节点个数for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {if (dis[i][k] + dis[k][j] > dis[i][j])dis[i][j] = dis[i][k] + dis[k][j];}}}

由此可见,floyd算法在节点数多的时候不占优势,时间复杂度过高,O(n3)


二、传递闭包

上个题吧,链接:http://poj.org/problem?id=3660

Cow Contest
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11232 Accepted: 6234

Description

N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.

The contest is conducted in several head-to-head rounds, each between two cows. If cowA has a greater skill level than cow B (1 ≤ AN; 1 ≤BN; AB), then cow A will always beat cowB.

Farmer John is trying to rank the cows by skill level. Given a list the results ofM (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.

Input

* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer,A, is the winner) of a single round of competition: A and B

Output

* Line 1: A single integer representing the number of cows whose ranks can be determined
 

Sample Input

5 54 34 23 21 22 5

Sample Output

2

题意:第一行表示有n个人m场比赛,后面m行每行有A B,表示A打败的B,问最后能够确定排名的人数有多少。

思路:判断一个人的排名是否确定,只要他与其余n-1个人的关系确定即可。这里涉及到了传递,如A打败了B,B打败了C,那么A间接打败了C。

这里跟floyd算法的思想是一样的,不同的是,floyd求得是最短路,if里更新的距离,这里是判断关系,不需要距离更新。

我们创建一个数字s,s[i][j]表示i打败了j,具体代码如下:

#include<iostream>#include<stdlib.h>#include<stdio.h>#include<cmath>#include<algorithm>#include<string>#include<string.h>#include<set>#include<queue>#include<stack>#include<vector>#include<functional>#include<map>using namespace std;const int maxn = 100 + 5;typedef long long LL;int s[maxn][maxn];int n, m;void floyd() {for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {for (int k = 1; k <= n; k++) {if (s[j][i] && s[i][k])s[j][k] = 1;}}}}int main() {while (scanf("%d %d", &n, &m) != EOF) {memset(s, 0, sizeof(s));for (int i = 1; i <= m; i++) {int a, b;scanf("%d %d", &a, &b);s[a][b] = 1;}floyd();int sum = 0;int t = 0;for (int i = 1; i <= n; i++) {t = 0;for (int j = 1; j <= n; j++) {if (s[i][j] || s[j][i]) t++;}if (t == n - 1)sum++;}cout << sum << endl;}system("pause");return 0;}





0 0
原创粉丝点击