<poj - 2139> Six Degrees of Cowvin Bacon 最短路径问题 the cow have been making movies
本题链接:http://poj.org/problem?id=2139
Description:
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:
* 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:
Sample Input:
4 2
3 1 2 3
2 3 4
Sample Output:
100
Hint:
题意:有一群牛在拍电影(牛们的生活真丰富),如果两头牛在同一部电影中出现过,那么这两头牛的度就为1,如果a与b之间有n头媒介牛,那么a,b的度为n+1。 给出m部电影,每一部给出牛的个数和编号。问哪一头到其他每头牛的度数平均值最小,输出 最小 平均值 乘100。
解题思路:只要把任意两个牛之间的最小度求出来然后就处理就行了,可以考虑用Floyd算法(三个for)。
参考代码:
#include <cstring>
#include <iostream>
#define INF 9999999
#define maxn 300
using namespace std; int cost[maxn][maxn];
int p[maxn];
int V; void fl () {
for (int k = ; k <= V; ++k) {
for (int i = ; i <= V; ++i) {
for (int j = ; j <= V; ++j) {
cost[i][j] = min (cost[i][j], cost[i][k] + cost[k][j]);
}
}
}
} int main () {
int x, y;
int a, b;
int n; cin >> V >> n; //-----------------------------------------初始化
memset (p, , sizeof(p));
for (int i = ;i <= V; ++i)
for (int j = ; j <= V; ++j)
cost[i][j] = INF;
for (int i = ; i <= V; ++i)
cost[i][i] = ;
//-----------------------------------------整理输入数据 while (n--) {
cin >> y;
for (int i = ; i <= y; ++i) {
cin >> p[i];
}
for (int i = ;i <= y; ++i) {
for (int j = i + ; j <= y; ++j) {
a = p[i];
b = p[j];
cost[b][a] = cost[a][b] = ;///a和b之间的距离
}
}
} fl ();//--------------调用函数 //-------------------------------------------------------求最小值输出
int sum, minsum = INF;
int i, j; for (i = ; i <= V; ++i) {
sum = ;
for (j = ; j <= V; ++j) {
sum += cost[i][j];
}
if (sum < minsum)//求最小值
minsum = sum;
} cout << (minsum * ) / (V - ) << endl; //输出 最小 平均值 return ;
}
欢迎码友评论,一起成长。
<poj - 2139> Six Degrees of Cowvin Bacon 最短路径问题 the cow have been making movies的更多相关文章
- 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 ...
- 任意两点间最短距离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
水题,Floyd. #include<cstdio> #include<cstring> #include<algorithm> using namespace s ...
- POJ 2139 Six Degrees of Cowvin Bacon (弗洛伊德最短路)
题意:奶牛拍电影,如果2个奶牛在同一场电影里演出,她们的合作度是1,如果ab合作,bc合作,ac的合作度为2,问哪一头牛到其他牛的合作度平均值最小再乘100 思路:floyd模板题 #include& ...
- POJ:2139-Six Degrees of Cowvin Bacon
传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...
- 【POJ - 2139】Six Degrees of Cowvin Bacon (Floyd算法求最短路)
Six Degrees of Cowvin Bacon Descriptions 数学课上,WNJXYK忽然发现人缘也是可以被量化的,我们用一个人到其他所有人的平均距离来量化计算. 在这里定义人与人的 ...
- POJ2139--Six Degrees of Cowvin Bacon(最简单Floyd)
The cows have been making movies lately, so they are ready to play a variant of the famous game &quo ...
随机推荐
- “String.h” 源代码总结
<String.h> 总结: 常用的函数: 一.memchr: 说明:当第一次遇到字符ch时停止查找.如果成功,返回指向字符ch的指针:否则返回NULL. 代码: #include ...
- Java学习笔记——JDBC之PreparedStatement类中“预编译”的综合应用
预编译 SQL 语句被预编译并存储在 PreparedStatement 对象中.然后可以使用此对象多次高效地执行该语句. 预编译的优点 1.PreparedStatement是预编译的,对于批量处理 ...
- UML和模式应用学习笔记-1(面向对象分析和设计)
UML和模式应用学习笔记-1(面向对象分析和设计) 而只是对情节的记录:此处的用例场景为:游戏者请求掷骰子.系统展示结果:如果骰子的总点数是7,则游戏者赢得游戏,否则为输 (2)定义领域模型:在领域模 ...
- IceMx.Mvc 我的js MVC 框架五、完善植物大战僵尸(雏形版增加动画)
有图有真相 说明 实在找不到僵尸的素材,从网上扒了一个山寨的僵尸,只有4张的一个连续图片,所以动作有点僵硬,植物的图片是自己处理的,非专业所以……咳咳!. 背景是自己抠下来2块儿拼接的,看在这么辛苦的 ...
- C语言栈与调用惯例
C语言栈与调用惯例 1.前言 最近在再看<程序员的自我修养>这本书,对程序的链接.装载与库有了更深入的认识.关于这本书的评价可以去豆瓣看看http://book.douban.com/su ...
- MVC3+EF5.0 code first+Flexigrid+ajax请求+jquery dialog 增删改查
MVC3+EF5.0 code first+Flexigrid+ajax请求+jquery dialog 增删改查 本文的目的: 1.MVC3项目简单配置EF code first生成并初始化数据 ...
- WPF制作的小型笔记本
WPF制作的小型笔记本-仿有道云笔记 楼主所在的公司不允许下载外部资源, 不允许私自安装应用程序, 平时记录东西都是用记事本,时间久了很难找到以前记的东西. 平时在家都用有道笔记, 因此就模仿着做了一 ...
- 使用Excel背单词-高效-简单
背单词是一个很纠结的事,想必那些走在留学路上的很多人都被英语这一关卡住了,这里,笔者就聊聊,不讲背单词的方法,只提供使用vb开发的产品和使用方法,有问题欢迎讨论. 简介:使用excel背单词,有一些人 ...
- nginx+apache+mysql+php+memcache+squid搭建集群web环境
服务器的大用户量的承载方案 一.前言 二.编译安装 三. 安装MySQL.memcache 四. 安装Apache.PHP.eAccelerator.php-memcache 五. 安装Squid 六 ...
- 简化日常工作之三:自己写一个CI脚手架
程序员是诗人,应该写一些有思想意义的code,而不是每天重复造轮子,写一些低成本的业务逻辑. ---------------------------------一个脚本仔的心声 由于目前公司使用CI框 ...