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 ...
随机推荐
- Codeforces 1106F(数论)
要点 998244353的原根g = 3,意味着对于任意\[1 <= x,y<p\]\[x\neq\ y\]\[g^x\%p\neq\ g^y\%p\]因此可以有构造序列\(q(a)与a一 ...
- 自己写的Grid组件,第二版
大体没什么变化,主要是添加了一个方法,getSelectedItems(),返回当前选中的数据项. (function ($) { $.fn.GridView = function (setting) ...
- Linux磁盘根目录满了问题解析
linux里的log文件被删除后,空间没有被释放,是因为在Linux系统中,通过rm或者文件管理器删除文件将会从文件系统的目录结构上解除链接(unlink).然而如果文件是被打开的(有一个进程正在使用 ...
- AmazeUI 保存浏览器数据 永久性
//保存永久缓存数据function SaveAmuiStore(ItemName, ItemData){ if (window.localStorage) { var store = $.AMUI. ...
- I/O————数据流
如何将一个long类型的数据写入文件中? 转字符串 → 通过 getbytes() 写进去,费劲,而且在此过程中 long 类型的数需要不断地转换. 现在,Java 中的数据流能够很好的解决这个问题( ...
- plpgsql: 动态插入数据 1
--目标:1.建立一个函数实现 输入一个表名(tableName)tableName,一个JSON串{feildName1:feildVale1,feildName2:feildVale2} -- 然 ...
- css制作三分圆形
效果图展示: 原理很简单,主要运用transform这个样式,通过斜切和旋转达成 html: css: 怎样,是不是很简单
- pingall脚本
p i n g a l l:一个按照/ e t c / h o s t s文件中的条目逐一p i n g所有主机的脚本 它能够按照/ e t c / h o s t s文件中的条目逐一p i n g所 ...
- mac下相关操作命令
查看端口使用情况 lsof -i tcp:
- vue+element ui项目总结点(二)table合计栏目,按照起始年份--截止年份 插入数据并向后追加数据以最后一条年份+1
1.oninput 事件在用户输入时触发; <template> <div class="test_box"> <p>hell,你好</p ...