2024. Cow Contest

煤渣2333

多源最短路径

思路

  • 两头牛之间的强弱关系不仅可以由输入输出获得,还可以通过A > C, C > B知道A > B,于是就能想到floyd算法了,将AB之间的最短路问题转化成AB之间的胜负问题
  • 如果一头牛和其他n-1头牛之间的胜负问题确定了,那它的排名就确定了2333,就是这么简单。。。

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>

using namespace std;

const int maxn = 105;
int g[maxn][maxn];
int n, m;

void floy() {
    for (int k = 1; k <= n; k++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n ; j++) {
                if (g[i][k] == 1 && g[k][j] == 1) {       /* A > C, C > B -> A > B */
                    g[i][j] = 1;
                    g[j][i] = -1;
                }
                else if (g[i][k] == -1 && g[k][j] == -1) { /* A < C, C < B -> A < B */
                    g[i][j] = -1;
                    g[j][i] = 1;
                }
            }
        }
    }
}

int main() {
    cin >> n >> m;
    while (m--) {
        int a, b;
        scanf("%d %d", &a, &b);
        g[a][b] = 1;
        g[b][a] = -1;
    }
    floy();
    int ans = 0;
    for (int i = 1; i <= n; i++) {
        int cnt = 0;
        for (int j = 1; j <= n; j++)
            if (g[i][j])
                cnt++;
        if (cnt == n - 1) ans++;
    }
    cout << ans;
    return 0;
}
你当前正在回复 博客/题目
存在问题!