POJ 2139 Six Degrees of Cowvin Bacon

floyd问题,输入的时候有一个注意点。

#include 
#include 
using namespace std;
const int INF = 1000000000;
const int MAXN = 310;
int n, m, k, dis[MAXN][MAXN], a[MAXN], rec[MAXN] = {}, Min = INF;
void Floyd()
{
    for(int k = 1; k <= n; k++){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
            }
        }
    }
}
int main()
{
    fill(dis[0], dis[0] + MAXN * MAXN, INF);
    for(int i = 0; i < MAXN; i++){
        dis[i][i] = 0;
    }
    scanf("%d%d", &n, &m);
    while(m--){
        scanf("%d", &k);
        for(int i = 0; i < k; i++){
            scanf("%d", &a[i]);
            for(int j = 0; j < i; j++){
                dis[a[i]][a[j]] = dis[a[j]][a[i]] = 1;
            }
        }
    }
    Floyd();
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            rec[i] += dis[i][j];
        }
        if(rec[i] < Min) Min = rec[i];
    }
    printf("%d\n", Min * 100 / (n - 1));
    return 0;
}

你可能感兴趣的:(#,第2章)