ACM学习感悟——POJ2139(Floyd)
2015-04-08 21:07
323 查看
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
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 .]
题意其实挺难理解的,真的。意思就是每部电影会有几个牛合作演出,那么这几个牛之间的距离就是1,这个题的第一个难点就是建图,说难,其实就是要想办法把几个点存下来,反正都是题意惹的祸= =。最后求得就是每一个牛的所有最短路的平均值的100倍,注意是除以n-1个,而不是n个的平均值。最后要记得班结果准换成整数。
AC代码:
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 .]
题意其实挺难理解的,真的。意思就是每部电影会有几个牛合作演出,那么这几个牛之间的距离就是1,这个题的第一个难点就是建图,说难,其实就是要想办法把几个点存下来,反正都是题意惹的祸= =。最后求得就是每一个牛的所有最短路的平均值的100倍,注意是除以n-1个,而不是n个的平均值。最后要记得班结果准换成整数。
AC代码:
///////////////////////////////////////////////////////// // // // // Created by Team 3 // // Copyright (c) 2015年 Team 3. All rights reserved. // ///////////////////////////////////////////////////////// #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <cctype> #include <stack> #include <queue> #include <map> #include <string> #include <set> #include <vector> #define INF 0x3f3f3f3f #define cir(i,a,b) for (int i=a;i<=b;i++) #define CIR(j,a,b) for (int j=a;j>=b;j--) #define CLR(x) memset(x,0,sizeof(x)) typedef long long ll; using namespace std; int N,M,m=0; #define maxn 400 int d[maxn][maxn]; int a[maxn]; void floyd() { for (int k=1;k<=N;k++) for (int i=1;i<=N;i++) for (int j=1;j<=N;j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } int main() { while (cin >> N >> M) { for (int i=1;i<=maxn;i++) for (int j=1;j<=maxn;j++) d[i][j]=i==j?0:INF; for (int i=1;i<=M;i++) { int n; cin >> n; for (int i=1;i<=n;i++) cin >>a[i]; for (int k=1;k<=n;k++) for (int l=1;l<=n;l++) { if(a[k]==a[l]) d[a[k]][a[l]]=0; else d[a[k]][a[l]]=1; } } floyd(); int ans=INF; for (int i=1;i<=N;i++) { int temp=0; for (int j=1;j<=N;j++) temp+=d[i][j]; ans=min(ans,temp); } // cout << ans << endl; double tp=ans*1.0/(N-1); ans=tp*100; cout << ans << endl; } return 0; }
相关文章推荐
- ACM学习感悟——HDU257 HOW TO TYPE
- ACM 学习感悟——HDU2845
- ACM学习感悟——ACdream字符串专场F(manacher)
- ACM学习感悟
- ACM学习感悟——HDU5092(dp)
- ACM学习感悟——暴力专场B(dfs)
- ACM学习感悟——HDU1505 最大0矩阵
- ACM学习感悟——POJ3169(差分约束)
- ACM学习感悟——POJ1932(图论)
- ACM学习感悟——HUD5090(我也不知道我用的啥= =)
- ACM学习感悟——摸底赛(并查集)
- ACM学习感悟——HDU2844 多重背包
- ACM学习感悟——POF3255 (用SPFA求次短路)
- ACM学习感悟——POJ1258(kruskal)
- ACM学习感悟——暴力专场F(dp)
- ACM学习感悟——FZU2192(并查集)
- ACM学习感悟——weekly training
- ACM学习感悟——Aizu2224 (kruskal 最大生成树)
- ACM学习感悟——HDU-命运
- ACM学习感悟——HDU1204