传送门: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

解题心得:

  1. 其实就是一个建图的问题,直接将同一个团体里面的人都添加一条边,距离是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的更多相关文章

  1. AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...

  2. POJ 2139 Six Degrees of Cowvin Bacon (floyd)

    Six Degrees of Cowvin Bacon Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Ja ...

  3. <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 ...

  4. warshall-floyd算法:POJ No 2139 Six Degress of Cowvin Bacon(任意两点最短路径))

    题目: http://poj.org/problem?id=2139 题解:N只牛,在一组的两只牛,分别两只之间为 “1度”,自己到自己为0度,M组牛.求,N只牛之中,两只牛之间 平均最短度数*100 ...

  5. 任意两点间最短距离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 ...

  6. POJ 2139 Six Degrees of Cowvin Bacon (Floyd)

    题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...

  7. POJ 2139 Six Degrees of Cowvin Bacon (弗洛伊德最短路)

    题意:奶牛拍电影,如果2个奶牛在同一场电影里演出,她们的合作度是1,如果ab合作,bc合作,ac的合作度为2,问哪一头牛到其他牛的合作度平均值最小再乘100 思路:floyd模板题 #include& ...

  8. POJ 2139 Six Degrees of Cowvin Bacon

    水题,Floyd. #include<cstdio> #include<cstring> #include<algorithm> using namespace s ...

  9. 【POJ - 2139】Six Degrees of Cowvin Bacon (Floyd算法求最短路)

    Six Degrees of Cowvin Bacon Descriptions 数学课上,WNJXYK忽然发现人缘也是可以被量化的,我们用一个人到其他所有人的平均距离来量化计算. 在这里定义人与人的 ...

随机推荐

  1. 高仿饿了么mock本地数据

    未使用router新版webpack.dev.conf.js配置本地数据访问:// 引入express 模块 const express = require('express') // 创建expre ...

  2. (五)JavaScript之[类型转换]

    /** * 类型转换 * * JavaScript 数据类型 * 1.不同的数据类型 * string * number * object * boolean * function * * 2.对象类 ...

  3. 什么是TOPO学

    拓扑,一个跟门萨同样古怪的“科技Word”.其定义,对绝大多数读者而言,不一定需要理解,但无妨知道———拓扑学,数学的一门分科,研究几何图形在一对一的双方连续变换下不变的性质.不少门萨题,来自拓扑学, ...

  4. NPOI Excel表格处理

    //创建一个Excel文件 HSSFWorkbook work = new HSSFWorkbook(); //新建一个工作表 ISheet sheet1 = work.CreateSheet(&qu ...

  5. PCB仿真软件与电磁场求解器的算法

    1. 简介 目前商业化的PCB仿真软件主要有: Cadence公司的Sigrity.Ansys公司的SIwave/HFSS.CST公司的CST.Mentor公司的HyperLynx.Polor公司的S ...

  6. 西门子触摸屏利用VBScript脚本创建csv文件

    功能描述:利用VBScript脚本创建csv/txt文件 有时需要将PLC或运动控制器Simotion中的数据写到SD卡或U盘上.一种实现方法是,如果使用的是精致面板(comfort panel),可 ...

  7. Cholesky分解(Cholesky decomposition / Cholesky )

    Cholesky decomposition In linear algebra, the Cholesky decomposition or Cholesky is a decomposition ...

  8. MySQL数据库实验五:数据更新

    实验五   数据更新 一.实验目的 掌握数据更新操作的用法. 二.实验环境 三.实验示例 1.往基本表SC中插入元组. ①    INSERT INTO S(S#,SNAME,AGE,SEX) VA ...

  9. Javascript作业—数组去重(要求:原型链上添加函数)

    数组去重(要求:原型链上添加函数) <script> //数组去重,要求:在原型链上添加函数 //存储不重复的--仅循环一次 if(!Array.prototype.unique1){ A ...

  10. "提取位于北坡的各类用地面积信息"的程序设计与实现

    "提取位于北坡的各类用地面积信息"的程序设计与实现  程序员:左正康         发表时间:2013/12/20 14:24       代号:黑眼圈的日子 第一步:导入dem ...