/*
解决此题,关键在于构图。
目标:
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.
即:求解过任意两点间的最短路后,sumMin = min{ sum{dp[1][1->n]}, sum{dp[2][1->n]} ... , sum{dp[n][1->n]} }
ans = sumMin*100/(n-1)
注意:一定要先乘以100,再除以(n-1),因为这个wa了好多次。
构图的基础:
The cows have made M (1 <= M <= 10000) movies and it is guaranteed that some relationship
path exists between every pair of cows.
即: dp[cows[Mi]][cows[Mj]] = dp[cows[Mj]][cows[Mi]] = 1 (Mi != Mj)
dp[cows[Mi]][cows[Mj]] = 0 (Mi == Mj)
*/
1 #include <iostream>
2 #include <cstdlib>
3 #include <cstdio>
4 #include <cstddef>
5 #include <iterator>
6 #include <algorithm>
7 #include <string>
8 #include <locale>
9 #include <cmath>
10 #include <vector>
11 #include <cstring>
12 #include <map>
13 #include <utility>
14 #include <queue>
15 #include <stack>
16 #include <set>
17 #include <functional>
18 using namespace std;
19 const int INF = 0x3f3f3f3f;
20 const int MaxN = 305;
21 const int MaxM = 10010;
22 const int modPrime = 3046721;
23
24 int n, m;
25 int dp[MaxN][MaxN];
26 int cows[MaxM];
27
28 void Solve()
29 {
30 for (int k = 1; k <= n; ++k)
31 {
32 for (int i = 1; i <= n; ++i)
33 {
34 for (int j = 1; j <= n; ++j)
35 {
36 dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
37 }
38 }
39 }
40 int sumMin = INF;
41 for (int i = 1; i <= n; ++i)
42 {
43 int sum = 0;
44 for (int j = 1; j <= n; ++j)
45 {
46 sum += dp[i][j];
47 }
48 sumMin = min(sumMin, sum);
49 }
50 printf("%d
", sumMin*100/(n-1));
51 }
52
53 int main()
54 {
55 #ifdef HOME
56 freopen("in", "r", stdin);
57 //freopen("out", "w", stdout);
58 #endif
59
60 scanf("%d %d", &n, &m);
61 for (int i = 1; i <= n; ++i)
62 {
63 for (int j = 1; j <= n; ++j)
64 {
65 if (i != j)
66 {
67 dp[i][j] = INF;
68 }
69 else
70 {
71 dp[i][j] = 0;
72 }
73 }
74 }
75 for (int i = 0; i < m; ++i)
76 {
77 int numOfCows;
78 scanf("%d", &numOfCows);
79 for (int j = 0; j < numOfCows; ++j)
80 {
81 scanf("%d", &cows[j]);
82 }
83 for (int j = 0; j < numOfCows; ++j)
84 {
85 for (int k = j + 1; k < numOfCows; ++k)
86 {
87 dp[cows[j]][cows[k]] = dp[cows[k]][cows[j]] = 1;
88 }
89 }
90 }
91 Solve();
92
93 #ifdef HOME
94 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl;
95 _CrtDumpMemoryLeaks();
96 #endif
97 return 0;
98 }