POJ 2139 Six Degrees of Cowvin Bacon (Floyd)
2015-03-23 21:39
387 查看
Six Degrees of Cowvin Bacon
Description
The cows have been making movies lately, so they are ready to play a variant of the famous game "Six Degrees of Kevin Bacon".
The game works like this: each cow is considered to be zero degrees of separation (degrees) away from herself. If two distinct cows have been in a movie together, each is considered to be one 'degree' away from the other. If a two cows have never worked together
but have both worked with a third cow, they are considered to be two 'degrees' away from each other (counted as: one degree to the cow they've worked with and one more to the other cow). This scales to the general case.
The N (2 <= N <= 300) cows are interested in figuring out which cow has the smallest average degree of separation from all the other cows. excluding herself of course. The cows have made M (1 <= M <= 10000) movies and it is guaranteed that some relationship
path exists between every pair of cows.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each input line contains a set of two or more space-separated integers that describes the cows appearing in a single movie. The first integer is the number of cows participating in the described movie, (e.g., Mi); the subsequent Mi integers
tell which cows were.
Output
* Line 1: A single integer that is 100 times the shortest mean degree of separation of any of the cows.
Sample Input
Sample Output
预处理 所有参加过同一电影的互相之间度为1 然后300个点
Floyd水题 - -
AC代码如下:
//
// POJ 2139 Six Degrees of Cowvin Bacon
//
// Created by TaoSama on 2015-03-20
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n, m, a[305], dp[305][305];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
scanf("%d%d", &n, &m);
memset(dp, 0x3f, sizeof dp);
for(int i = 1; i <= m; ++i) {
int t; scanf("%d", &t);
for(int j = 1; j <= t; ++j) scanf("%d", a + j);
for(int j = 1; j <= t; ++j)
for(int k = j + 1; k <= t; ++k)
dp[a[j]][a[k]] = dp[a[k]][a[j]] = 1;
}
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
int ans = INF;
for(int i = 1; i <= n; ++i) {
int t = 0;
for(int j = 1; j <= n; ++j) {
if(i == j) continue;
t += dp[i][j];
}
ans = min(ans, t);
}
printf("%d\n", ans * 100 / (n - 1));
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3289 | Accepted: 1530 |
The cows have been making movies lately, so they are ready to play a variant of the famous game "Six Degrees of Kevin Bacon".
The game works like this: each cow is considered to be zero degrees of separation (degrees) away from herself. If two distinct cows have been in a movie together, each is considered to be one 'degree' away from the other. If a two cows have never worked together
but have both worked with a third cow, they are considered to be two 'degrees' away from each other (counted as: one degree to the cow they've worked with and one more to the other cow). This scales to the general case.
The N (2 <= N <= 300) cows are interested in figuring out which cow has the smallest average degree of separation from all the other cows. excluding herself of course. The cows have made M (1 <= M <= 10000) movies and it is guaranteed that some relationship
path exists between every pair of cows.
Input
* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each input line contains a set of two or more space-separated integers that describes the cows appearing in a single movie. The first integer is the number of cows participating in the described movie, (e.g., Mi); the subsequent Mi integers
tell which cows were.
Output
* Line 1: A single integer that is 100 times the shortest mean degree of separation of any of the cows.
Sample Input
4 2 3 1 2 3 2 3 4
Sample Output
100
预处理 所有参加过同一电影的互相之间度为1 然后300个点
Floyd水题 - -
AC代码如下:
//
// POJ 2139 Six Degrees of Cowvin Bacon
//
// Created by TaoSama on 2015-03-20
// Copyright (c) 2015 TaoSama. All rights reserved.
//
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
#define CLR(x,y) memset(x, y, sizeof(x))
using namespace std;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int N = 1e5 + 10;
int n, m, a[305], dp[305][305];
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
scanf("%d%d", &n, &m);
memset(dp, 0x3f, sizeof dp);
for(int i = 1; i <= m; ++i) {
int t; scanf("%d", &t);
for(int j = 1; j <= t; ++j) scanf("%d", a + j);
for(int j = 1; j <= t; ++j)
for(int k = j + 1; k <= t; ++k)
dp[a[j]][a[k]] = dp[a[k]][a[j]] = 1;
}
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
int ans = INF;
for(int i = 1; i <= n; ++i) {
int t = 0;
for(int j = 1; j <= n; ++j) {
if(i == j) continue;
t += dp[i][j];
}
ans = min(ans, t);
}
printf("%d\n", ans * 100 / (n - 1));
return 0;
}
相关文章推荐
- Six Degrees of Cowvin Bacon POJ - 2139 (floyd求最短路)
- POJ2139 Six Degrees of Cowvin Bacon (Floyd 最短路径)
- POJ 2139 - Six Degrees of Cowvin Bacon(floyd)
- POJ 2139 Six Degrees of Cowvin Bacon(floyd)
- poj 2139 Six Degrees of Cowvin Bacon (Floyd 算法)
- POJ2139 Six Degrees of Cowvin Bacon [Floyd]
- POJ 2139-Six Degrees of Cowvin Bacon(最短路Floyd)
- POJ 2139 Six Degrees of Cowvin Bacon(floyd两点间最短距离)
- POJ 2139 Six Degrees of Cowvin Bacon (Floyd)
- 【图论floyd】 poj 2139 Six Degrees of Cowvin Bacon
- POJ - 2139 Six Degrees of Cowvin Bacon(任意两点最短路,Floyd)
- 任意两点间最短距离floyd-warshall ---- POJ 2139 Six Degrees of Cowvin Bacon
- POJ 2139 Six Degrees of Cowvin Bacon (floyd)
- [POJ](2139)Six Degrees of Cowvin Bacon ---- 最短路径算法(Floyd)
- POJ 2139 Six Degrees of Cowvin Bacon【floyd】
- POJ_2139_Six Degrees of Cowvin Bacon【Floyd】
- POJ 2139 Six Degrees of Cowvin Bacon(任意两点最短路,Floyd)
- A - Six Degrees of Cowvin Bacon POJ - 2139 最短路Floyd
- poj 2139 Six Degrees of Cowvin Bacon 图论 Floyd最短路
- Poj 2139 Six Degrees of Cowvin Bacon【floyd】