Skip to content

Instantly share code, notes, and snippets.

@amoshyc
Created August 1, 2015 08:44
Show Gist options
  • Save amoshyc/0325e4f4a49ead8710ef to your computer and use it in GitHub Desktop.
Save amoshyc/0325e4f4a49ead8710ef to your computer and use it in GitHub Desktop.
Poj 2139: Six Degrees of Cowvin Bacon

Poj 2139: Six Degrees of Cowvin Bacon

分析

裸 Floyd Warshall

AC Code

#include <iostream>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cstring>
#include <cstdlib>

using namespace std;

int N, M;
const int INF = 0x3f3f3f3f;

int dp[300][300];
int input[300];

int solve() {
    // floyd warshall
    for (int k = 0; k < N; k++)
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                if (dp[i][k] + dp[k][j] < dp[i][j])
                    dp[i][j] = dp[i][k] + dp[k][j];

    int ans = INF;
    for (int i = 0; i < N; i++) {
        int sum = accumulate(dp[i], dp[i] + N, 0);
        if (sum < ans)
            ans = sum;
    }
    return 100 * ans / (N-1);
}

int main() {
    scanf("%d %d", &N, &M);

    memset(dp, INF, sizeof(dp));
    for (int i = 0; i < N; i++)
        dp[i][i] = 0;

    for (int i = 0; i < M; i++) {
        int n; scanf("%d", &n);

        for (int j = 0; j < n; j++)
            scanf("%d", &input[j]);

        for (int j = 0; j < n - 1; j++) {
            for (int k = j + 1; k < n; k++) {
                dp[input[j] - 1][input[k] - 1] = 1;
                dp[input[k] - 1][input[j] - 1] = 1;
            }
        }
    }

    printf("%d\n", solve());

    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment