任意两点间最短距离floyd-warshall ---- POJ 2139 Six Degrees of Cowvin Bacon
floyd-warshall算法 通过dp思想 求任意两点之间最短距离
重复利用数组实现方式dist[i][j] i - j的最短距离
for(int k = 1; k <= N; k++)
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);
非常好实现 O(V^3)
这里贴一道刚好用到的题
http://poj.org/problem?id=2139
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXV 307
using namespace std; //floyd-warshall int dist[MAXV][MAXV];
int main()
{
int N, M;
int cow[MAXV];
double sum[MAXV];
freopen("in.txt", "r", stdin);
scanf("%d%d", &N, &M);
for (int i = ; i <= N; i++)
for (int j = ; j <= N; j++)
if (i != j) dist[i][j] = INF;
else dist[i][j] = ;
for (int i = ; i < M; i++)
{
int mi = ;
scanf("%d", &mi);
for (int j = ; j < mi; j++)
{
scanf("%d", &cow[j]);
}
for (int j = ; j < mi; j++)
{
for (int k = ; k < mi; k++)
{
if (cow[j] == cow[k]) continue;
else dist[cow[j]][cow[k]] = dist[cow[k]][cow[j]] = ;
}
}
}
for (int k = ; k <= N; k++)
for (int i = ; i <= N; i++)
for (int j = ; j <= N; j++) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
for (int i = ; i <= N; i++)
{
sum[i] = ;
for (int j = ; j <= N; j++)
{
sum[i] += dist[i][j];
}
}
sort(sum+, sum+N+);
//注意题目最后的要求 A single integer that is 100 times the shortest mean degree of separation of any of the cows.
//所以要变为int
cout << int(sum[] / (N-)*) << endl;
}
任意两点间最短距离floyd-warshall ---- POJ 2139 Six Degrees of Cowvin Bacon的更多相关文章
- 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 ...
- 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
水题,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 ...
- Floyed-Warshall算法(求任意两点间最短距离)
思路:感觉有点像暴力啊,反正我是觉得很暴力,比如求d[i][j],用这个方法求的话,就直接考虑会不会经过点k(k是任意一点) ,最终求得最小值 看代码 #include<iostream> ...
- 图算法之Floyd-Warshall 算法-- 任意两点间最小距离
1.Floyd-Warshall 算法 给定一张图,在o(n3)时间内求出任意两点间的最小距离,并可以在求解过程中保存路径 2.Floyd-Warshall 算法概念 这是一个动态规划的算法. 将顶点 ...
随机推荐
- 11.1Java-接口
一.接口 interface定义:固定格式 public abstract 返回值类型 方法名字(参数列表);代码: public interface AMyInterface { public ab ...
- AJPFX总结方法的特点
它可以实现独立的功能; 必须定义在类里面; 它只有被调用才会执行; 它可以被重复使用; 方法结束后方法里的对象失去引用; 如何定义一个功能,并通过方法体现出来: ① 明确该功能运算后的结果.明确返 ...
- 洛谷P3773 [CTSC2017]吉夫特(Lucas定理,dp)
题意 满足$b_1 < b_2 < \dots < b_k$且$a_{b_1} \geqslant a_{b_2} \geqslant \dots \geqslant a_{b_k} ...
- php 生成饼状图,折线图,条形图 通用类
生成饼状图,折线图,条形图通用的php类,这里使用的是百度 Echart. Echart 官方网站 http://echarts.baidu.com/ <?php class Echarts ...
- PMP项目管理学习笔记(5)——整合管理之制定项目章程
关于两个输入 在很多过程中,会用到这两个输入: 企业环境要素 是关于你的公司如何开展业务所需要知道的所有信息. 在你计划项目时,有很多关于公司的信息会非常有用,你需要知道各个不同部门是如何运作的,你所 ...
- Android中单选框RadioButton的基本用法
总结一下设置图标的三种方式: (1)button属性:主要用于图标大小要求不高,间隔要求也不高的场合. (2)background属性:主要用于能够以较大空间显示图标的场合. (3)drawableL ...
- 如何开发 Laravel 扩展包并发布到 Composer
如何开发 Laravel 扩展包并发布到 Composer 发布于 2019-01-22 cxp1539 1074 Vie 开发扩展包 我们来做一个根据第一个字符或者汉字生成头像的larave ...
- Python3简明教程(一)—— 开始Python之旅
第一个Python程序 作为我们第一个Python程序——打印"Hello World!". 在终端输入Python3进入交互界面: 输入print("Hello W ...
- 爬虫学习之第一次获取网页内容及BeautifulSoup处理
from urllib.request import urlopen from urllib.request import HTTPError from bs4 import BeautifulSou ...
- python之字符串str操作方法
str.upper() (全部大写) str.lower() (全部小写) str.startswith() (以什么开头) str.endswith() (以什么结尾) str.count() (统 ...