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

题意:奶牛们要拍电影,如果两个奶牛同时拍一部那么他们的距离为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的更多相关文章

  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 最短路径问题 the cow have been making movies

    本题链接:http://poj.org/problem?id=2139 Description:     The cows have been making movies lately, so the ...

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

  4. ShortestPath:Six Degrees of Cowvin Bacon(POJ 2139)

    牛与电影 题目大意:就是一群牛,又在玩游戏了(怎么你们经常玩游戏),这个游戏规则如下,把牛拆分成一个一个组,并且定义一个“度”,规定在一个组的牛与他自己的度为0,与其他牛的度为1,不同组的牛不存在度, ...

  5. POJ 2139 Six Degrees of Cowvin Bacon

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

  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 (floyd)

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

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

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

  9. poj 2139 奶牛拍电影问题 floyd算法

    题意:奶牛拍一系列电影,n头牛拍m部电影,同一部电影种的搭档们距离为1,求最小距离? 思路:Floyd 图 最短路径 存图: 初始化图 for (int i = 0; i <= n; i++) ...

  10. poj 2139 flord水题

    读懂题意就简单了 #include<stdio.h> #define inf 999999999 #define N 310 int f[N]; int map[N][N]; int ma ...

随机推荐

  1. html5拖动滑块

    html5中input有增加type=range.这为拖动滑块提供了很大的便利.下面是他的属性: <!DOCTYPE html> <html lang="en"& ...

  2. Less的学习和使用

    官网 http://less.bootcss.com/usage/ 在线编译器 http://tool.oschina.net/less

  3. 原来MFC窗口样式随字符集而改变

    以前好像发现,MFC窗口上按钮的自动样式有时是有亮色边框3D效果的,有时没有,不知道原因,也没有追究,今天正好有机会发现了原因,原来是随字符集而改变的. 1.Unicode版本下的窗口 2.未设置的窗 ...

  4. 大数据freestyle: 共享单车轨迹数据助力城市合理规划自行车道

    编者按:近年来,异军突起的共享单车极大地解决了人们共同面临的“最后一公里”难题,然而,共享单车发展迅猛,自行车道建设却始终没有能够跟上脚步.幸运的是摩拜单车大量的轨迹数据为我们提供了一种新的思路:利用 ...

  5. 如何在vue项目中使用sass(scss)

    1.用npm/cnpm/yarn安装sass的依赖包 npm install --save-dev sass-loader npm install --save-dev node-sass 或者: y ...

  6. C#中Lock关键字的使用

    C# 中的 Lock 语句通过隐式使用 Monitor 来提供同步功能.lock 关键字在块的开始处调用 Enter,而在块的结尾处调用 Exit. 通常,应避免锁定 public 类型,否则实例将超 ...

  7. fckeditor配置详解

    使用配置设置: . FCKConfig.CustomConfigurationsPath = '' ; // 自定义配置文件路径和名称 . FCKConfigFCKConfig.EditorAreaC ...

  8. bootstrap历练实例: 垂直胶囊式的导航菜单

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. HTML5拖放(drag和drog)作品

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. Protocol(协议)、Delegate(委托)、DataSource(数据源)

    这里以 UITableViewController 和 UITableView 的关系为例: //--------------------------------------------------- ...