Six Degrees of Cowvin Bacon
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
Hint
[Cow 3 has worked with all the other cows and thus has degrees of separation: 1, 1, and 1 -- a mean of 1.00 .]
Source
题目类型:最短路(Dij)
算法分析:由于点数不是很大,所以可以直接枚举所有点求最短路,然后更新答案
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
/************************************************* Author :supermaker Created Time :2016/1/31 14:50:07 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 300 + 66; int n, m, dis[maxn]; vector <int> in[maxn]; vector <PII> g[maxn]; void Dij (int s) { fill (dis, dis + maxn, INF); dis[s] = 0; priority_queue <PII, vector <PII>, greater <PII> > qu; qu.push (PII (0, s)); while (!qu.empty ()) { PII tt = qu.top (); qu.pop (); int ta = tt.second, tb = tt.first; if (dis[ta] < tb) continue; for (int i = 0; i < g[ta].size (); i++) { PII v = g[ta][i]; if (v.first != s && dis[v.first] > v.second + dis[ta]) { dis[v.first] = v.second + dis[ta]; qu.push (PII (dis[v.first], v.first)); } } } } int main() { //CFF; //CPPFF; while (scanf ("%d%d", &n, &m) != EOF) { for (int i = 0; i < maxn; i++) in[i].clear (), g[i].clear (); for (int i = 1; i <= m; i++) { int num; scanf ("%d", &num); for (int j = 1; j <= num; j++) { int u; scanf ("%d", &u); in[i].push_back (u); } } for (int i = 1; i <= m; i++) { int len = in[i].size (); for (int j = 0; j < len; j++) for (int k = j + 1; k < len; k++) { if (j == k) continue; g[in[i][j]].push_back (PII (in[i][k], 1)); g[in[i][k]].push_back (PII (in[i][j], 1)); } } int minval = INF; for (int i = 1; i <= n; i++) { Dij (i); int cnt = 0; for (int j = 1; j <= n; j++) cnt += dis[j]; minval = min (minval, cnt); } printf ("%d\n", minval * 100 / (n - 1)); } return 0; } |
- « 上一篇:poj2449
- poj1459:下一篇 »