任意两点间最短距离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 算法概念 这是一个动态规划的算法. 将顶点 ...
随机推荐
- apt-get的一些坑
apt-get update:更新安装列表apt-get upgrade:升级软件apt-get install software_name :安装软件apt-get --purge remove ...
- AJPFX总结多线程编程的注意事项
多线程编程的注意事项 1.明确目的,为什么要使用多线程?如果是由于单线程读写或者网络访问(例如HTTP访问互联网)的瓶颈,可以考虑使用线程池.如果是对不同的资源(例如SOCKET连接 ...
- Android Studio Terminal 不是内部或外部命令,也不是可运行程序或批处理文件
1.Android Studio Terminal 命令行无效的问题 在Android Studio中自带了命令行终端Terminal,但是我们在输入命令时经常会发现:“XXX”不是内部或外部命令,也 ...
- Load average in Linux的精确含义
Man 上的解释: load average System load averages is the average number of processes that are either in a ...
- Selenium私房菜系列2 -- XPath的使用【ZZ】
在编写Selenium案例时,少不免是要用到XPath的,现在外面关于XPath使用的参考资料很多,下面我直接转一篇关于XPath使用的文档.如果对XPath不熟悉请参考下文,你不需要去百度/Goog ...
- 回顾Spring MVC_01_概述_入门案例
SpringMVC: SpringMVC是Spring为展现层提供的基于MVC设计的优秀的Web框架,是目前最主流的MVC框架之一 SpringMVC通过注解,让POJO成为处理请求的控制器,而无须实 ...
- 技术抄录_Java高级架构师教程
1.B2C商城项目实战 2.高性能架构专题 3.架构筑基与开源框架解析专题 4.团队协作开发专题 5.微服务架构专题 6.设计模式 附上[架构资料] ...
- Linux常用的操作指令
1.pwd-显示当前所在位置 2.cd-进入当前目录 3.cd..-返回上一级目录 4..ls命令参数选项有很多,ls也是经常使用到的命令.如果不清楚命令的使用方式可以直接 ls --help来查看 ...
- input_shape { dim: 1 dim: 3 dim: 224 dim: 224 }
http://blog.csdn.net/u010417185/article/details/52619593
- 【软件构造】(转)Git详解、常用操作与版本图
版本控制与Git 转自:http://www.cnblogs.com/angeldevil/p/3238470.html 版本控制 版本控制是什么已不用在说了,就是记录我们对文件.目录或工程等的修改历 ...