poj--2139
Description
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
* 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 题意:奶牛们要拍电影,如果两个奶牛同时拍一部那么他们的距离为1,如果两个奶牛同时和第三头奶牛拍一部电影,那么他们的距离为2,求他们最短距离乘上100的最小平均值
这题是相当于求最短路径,forld最小环从自身出发,最后到达自身的最短路径,
Flord 最小环问题相当于一个动态规划的算法,如果a不能到达b那么可以考虑引入一个k,经过k到达b并更新一下a,b的距离
所以这个算法的核心代码是
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
map[i][j]=min(map[i][j],map[i][k]+map[k][j])
最近做题一直忘记预处理,导致程序不对。
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int Max_n=;
const int INF=;
int map[Max_n][Max_n];
int node[Max_n];
int n,m;
void prepare()
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if(i!=j) map[i][j]=map[j][i]=INF;
}
}
int main()
{
while(cin>>n>>m)
{
prepare();
int x;
while(m--)
{
cin>>x;
for(int i=;i<=x;i++)
{
cin>>node[i];
}
for(int i=;i<=x;i++)
for(int j=i+;j<=x;j++)
map[node[i]][node[j]]=map[node[j]][node[i]]=;
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
}
int ans=INF;
for(int i=;i<=n;i++)
{
int maxn=;
for(int j=;j<=n;j++)
{
maxn+=map[i][j];
}
ans=min(ans,maxn*/(n-));
}
printf("%d\n",ans);
}
return ;
}
poj--2139的更多相关文章
- 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 最短路径问题 the cow have been making movies
本题链接:http://poj.org/problem?id=2139 Description: The cows have been making movies lately, so the ...
- 任意两点间最短距离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 ...
- ShortestPath:Six Degrees of Cowvin Bacon(POJ 2139)
牛与电影 题目大意:就是一群牛,又在玩游戏了(怎么你们经常玩游戏),这个游戏规则如下,把牛拆分成一个一个组,并且定义一个“度”,规定在一个组的牛与他自己的度为0,与其他牛的度为1,不同组的牛不存在度, ...
- 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)
题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有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 (弗洛伊德最短路)
题意:奶牛拍电影,如果2个奶牛在同一场电影里演出,她们的合作度是1,如果ab合作,bc合作,ac的合作度为2,问哪一头牛到其他牛的合作度平均值最小再乘100 思路:floyd模板题 #include& ...
- poj 2139 奶牛拍电影问题 floyd算法
题意:奶牛拍一系列电影,n头牛拍m部电影,同一部电影种的搭档们距离为1,求最小距离? 思路:Floyd 图 最短路径 存图: 初始化图 for (int i = 0; i <= n; i++) ...
- poj 2139 flord水题
读懂题意就简单了 #include<stdio.h> #define inf 999999999 #define N 310 int f[N]; int map[N][N]; int ma ...
随机推荐
- Jasper_table_resolve multiple copies of table in detail band issue
resolve method: (1) put table component into the Title band / Page Header band / Summary band, not i ...
- 关于ssh的介绍
最近看到一篇关于介绍ssh讲得很清晰的文章,这里来记录一下加深一下印象: 基本原理: SSH(Secure Shell)是一套协议标准,可以用来实现两台机器之间的安全登录以及安全的数据传送,其保证数据 ...
- python Windows和Linux路径表示问题
Windows下路径是用‘\\’表示也可以使用'/',但是Linux下路径都是‘/’表示. 因为python是跨平台的,有时候程序迁移会出现错误. 解决办法1 可全部使用‘/’表示 解决办法2 我们可 ...
- ubuntu 文件解压命令
[解压.zip文件] unzip ./FileName.zip //前提是安装了unzip 安装unzip命令:sudo apt-get install unzip 卸载unzip软件 命令:sudo ...
- phpmyadmin消除无法保存最近表的提示
运行 sudo dpkg-reconfigure phpmyadmin 重新配置phpmyadmin ip选择127.0.0.1,端口3306,"MySQL username for php ...
- XML文件的解析和序列化
序列化: private void createXml() { XmlSerializer serializer = Xml.newSerializer();// xml文件生成器 File file ...
- Android - CollapsingToolbarLayout 完全解析
CollapsingToolbarLayout 是 google 在其推出的design libiary 中给出的一个新型控件.其可以实现的效果类似于: toolbar是透明的,有一个背景图片以及大标 ...
- 【排序】插入排序:最稳定:时间复杂度O(n^2)
想象着自己在玩扑克的时候抓牌,每抓到一张牌,按照从小到大的顺序排序. 如果第二张的点数小于第一张,就交换这两张牌,默认每次抓牌之前,前面的已经排好序了. 再来一张牌,与第二张比较,如果小于第二张,交换 ...
- 企业CIO、CTO必读的34个经典故事
一. 用人之道 去过庙的人都知道,一进庙门,首先是弥陀佛,笑脸迎客,而在他的北面,则是黑口黑脸的韦陀.但相传在很久以前,他们并不在同一个庙里,而是分别掌管不同的庙.弥乐佛热情快乐,所以来的人非常多,但 ...
- LR中排序脚本
/* * LoadRunner Java script. (Build: 670) * * Script Description: * */ import lrapi.lr; public class ...