最短路(构图) 之 poj 2139 Six Degrees of Cowvin Bacon

/*
解决此题,关键在于构图。

目标:
	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\n", 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 }

 
时间: 2024-10-22 06:39:17

最短路(构图) 之 poj 2139 Six Degrees of Cowvin Bacon的相关文章

POJ 2139 Six Degrees of Cowvin Bacon

Six Degrees of Cowvin Bacon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2986   Accepted: 1390 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&quo

poj 2139 Six Degrees of Cowvin Bacon , floyd

点击打开链接 题意:给定牛的关系图,求其中一头牛与其他牛关系路程之和sum最小,然后输出 sum*100/(n-1) floyd求任意两点间的最短路程 注意: inf不能太大,因为 f[i][k] + f[k][j]  做加法时可能会溢出! #include <cstdio> #include <cstring> const int maxn = 300 + 5; const int inf = 1<<29; int n, m; int f[maxn][maxn]; i

POJ 2139 SIx Degrees of Cowvin Bacon 最短路 水題

Six Degrees of Cowvin Bacon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3602   Accepted: 1675 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&quo

任意两点间最短距离floyd-warshall ---- POJ 2139 Six Degrees of Cowvin Bacon

floyd-warshall算法 通过dp思想 求任意两点之间最短距离 重复利用数组实现方式dist[i][j] i - j的最短距离 for(int k = 1; k <= N; k++) for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]); 非常好实现 O(V^3) 这里贴一道刚好用到的题 http://poj.org

Six Degrees of Cowvin Bacon (poj 2139 最短路Floyd)

Language: Default Six Degrees of Cowvin Bacon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3288   Accepted: 1529 Description The cows have been making movies lately, so they are ready to play a variant of the famous game "Six Degrees

POJ2139 Six Degrees of Cowvin Bacon 【Floyd】

Six Degrees of Cowvin Bacon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3131   Accepted: 1455 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&quo

POJ No 2139 Six Degress of Cowvin Bacon(warshall_floyd(任意两点最短路径))

题目: http://poj.org/problem?id=2139 题解:N只牛,在一组的两只牛,分别两只之间为 “1度”,自己到自己为0度,M组牛.求,N只牛之中,两只牛之间 平均最短度数*100.模板Floyd算法,求任意两点之间最短路径. #include <iostream> #include <algorithm> #include <iomanip> using namespace std; const int maxn = 300 + 24; const

【Floyd】POJ2139 -Six Degrees of Cowvin Bacon

普通的Floyd了分分秒可以水过,结果在submit前删调试段落的时候把程序本体给删了吃了两个WA…… 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 const int INF=3000000; 5 using namespace std; 6 const int MAXN=10000; 7 int len[MAXN][MAXN]; 8 int f[MAXN][MAXN]; 9 10 int

POJ2139 Six Degrees of Cowvin Bacon [Floyd]

水题,随手敲过 一看就是最短路问题,a,b演同一场电影则他们的距离为1 默认全部两两原始距离无穷,到自身为0 输入全部数据处理后floyd 然后照它说的求平均分离度 再找最小的,×100取整输出 #include <cstdio> #include <algorithm> #include <iostream> using namespace std; int cownum,filmnum; int film[11111][333]; int g[333][333];