POJ:2139-Six Degrees of Cowvin Bacon
传送门:http://poj.org/problem?id=2139
Six Degrees of Cowvin Bacon
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6709 Accepted: 3122
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
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 .]
中文题意:
数学课上,WNJXYK忽然发现人缘也是可以被量化的,我们用一个人到其他所有人的平均距离来量化计算。
在这里定义人与人的距离:
1.自己与自己的距离为0
2.如果A和B属于同一个小团体,那么他们之间的距离为1
3.如果A与B属于一个小团体,B与C属于一个小团体,且A与C不同属于任何一个小团体,那么A与C的距离为2(A联系C,经过B、C两个人)
4.以此类推
班里有N个人 (2 <= N <= 300),共有M对小团体关系(1 <= M <= 10000)。现在,给你所有班级中小团体的信息,问你班里人缘最好的人到其他人的平均距离。(你需要输出平均距离的100倍)
Input
第一行输入两个证书N,M 接下来的M+1行。
每行开头一个整数K表示本小团体大小,然后接下来K个整数表示小团体内学生编号。
Output
输出一行:最小的平均距离*100(程序中请不要使用Float和Double计算,可能会判Wa)
Sample Input
4 2
3 1 2 3
2 3 4
Sample Output
100
解题心得:
- 其实就是一个建图的问题,直接将同一个团体里面的人都添加一条边,距离是100,不建立其他的边,枚举每个点跑最短路就行了。
#include <stdio.h>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 310;
int maps[maxn][maxn],Time[maxn],Min = 0x7f7f7f7f;
bool vis[maxn];
int n,m;
void init() {
int num[10010];
memset(maps,0,sizeof(maps));
for(int z=0;z<m;z++) {
int cnt;
scanf("%d",&cnt);
for(int i=0;i<cnt;i++) {
scanf("%d",&num[i]);
}
for(int i=0;i<cnt;i++)
for(int j=0;j<i;j++) {
maps[num[i]][num[j]] = maps[num[j]][num[i]] = 100;
}
}
}
void spfa(int s) {
memset(Time,0x7f,sizeof(Time));
queue <int> qu;
qu.push(s);
Time[s] = 0;
while(!qu.empty()) {
int now = qu.front(); qu.pop();
vis[now] = false;
for(int i=1;i<=n;i++) {
if(maps[now][i]) {
if(Time[i] > Time[now] + maps[now][i]) {
Time[i] = Time[now] + maps[now][i];
if(!vis[i]) {
qu.push(i);
vis[i] = true;
}
}
}
}
}
}
void get_sum_path() {
int sum = 0;
for(int i=1;i<=n;i++) {
sum += Time[i];
}
if(sum < Min)
Min = sum;
}
int main() {
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=n;i++) {
spfa(i);
get_sum_path();
}
printf("%d",Min/(n-1));
return 0;
}
POJ:2139-Six Degrees of Cowvin Bacon的更多相关文章
- AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...
- POJ 2139 Six Degrees of Cowvin Bacon (floyd)
Six Degrees of Cowvin Bacon Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Ja ...
- <poj - 2139> Six Degrees of Cowvin Bacon 最短路径问题 the cow have been making movies
本题链接:http://poj.org/problem?id=2139 Description: The cows have been making movies lately, so the ...
- warshall-floyd算法:POJ No 2139 Six Degress of Cowvin Bacon(任意两点最短路径))
题目: http://poj.org/problem?id=2139 题解:N只牛,在一组的两只牛,分别两只之间为 “1度”,自己到自己为0度,M组牛.求,N只牛之中,两只牛之间 平均最短度数*100 ...
- 任意两点间最短距离floyd-warshall ---- POJ 2139 Six Degrees of Cowvin Bacon
floyd-warshall算法 通过dp思想 求任意两点之间最短距离 重复利用数组实现方式dist[i][j] i - j的最短距离 for(int k = 1; k <= N; k++) f ...
- POJ 2139 Six Degrees of Cowvin Bacon (Floyd)
题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...
- POJ 2139 Six Degrees of Cowvin Bacon (弗洛伊德最短路)
题意:奶牛拍电影,如果2个奶牛在同一场电影里演出,她们的合作度是1,如果ab合作,bc合作,ac的合作度为2,问哪一头牛到其他牛的合作度平均值最小再乘100 思路:floyd模板题 #include& ...
- POJ 2139 Six Degrees of Cowvin Bacon
水题,Floyd. #include<cstdio> #include<cstring> #include<algorithm> using namespace s ...
- 【POJ - 2139】Six Degrees of Cowvin Bacon (Floyd算法求最短路)
Six Degrees of Cowvin Bacon Descriptions 数学课上,WNJXYK忽然发现人缘也是可以被量化的,我们用一个人到其他所有人的平均距离来量化计算. 在这里定义人与人的 ...
随机推荐
- Django Rest Framework进阶二
一.版本 版本控制:当程序越来越大,后期需要再加入一些功能或者进行二次开发时就需要加上版本号了. 之前我们在没有接触rest_framework之前一般是以下这种方式来实现的 class UserVi ...
- Struts2_默认Action
配置Struts2默认跳转的Action <package name="default" namespace="/" extends="stru ...
- ansible使用2-命令
并发与shell # bruce用户身份,-m指定模块名称,默认模块名command,all所有目标主机,也可以指定组名或者主机名 ansible all -m ping -u bruce # bru ...
- python3绘图示例5(基于matplotlib:正弦图等)
#!/usr/bin/env python# -*- coding:utf-8 -*- import numpy as npimport pylab as pyimport matplotlib as ...
- mybatis由JDBC的演化过程分析
我们知道,mybatis是对JDBC的封装,那么他是如何演变过来的呢? 摘自传智传媒Java培训资料 关于mybatis的演化原理,我们先看看我们最熟悉也是最基础的通过JDBC查询数据库数据,一般需要 ...
- nginx配置优化-生产环境应用版
user www www; worker_processes auto; worker_cpu_affinity auto; error_log /usr/local/nginx/logs/error ...
- 初识Python(四)
一.数字数据类型 Python的数字数据类型用于存储数值,它是不可变的数据类型,这意味着改变数字数据类型,则需要一个新分配的对象: Python支持四种不同的数值类型: 整型(Int):通常被称为是整 ...
- Tensorflow ValueError: Protocol message RewriterConfig has no "layout_optimizer" field
I changed models/research/object_detection/exporter.py line 71/72 from: rewrite_options = rewriter_c ...
- HTC vive VR设备软硬件安装+运行unity开发的VR程序
总结在HTC vive VR开发过程中的HTC vive的安装调试 1.首先确保电脑的配置满足要求: 进入官网,测试电脑是否满足要求 链接:https://www.vive.com/us/produc ...
- C#自定义规则对比两个集合的对象是否相等
IList<获取的类> ret = 类的结果集; return ret.Except(另一个相同类型的对象列表集, new AClassComPare()): public class A ...