任意两点间最短距离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 算法概念 这是一个动态规划的算法. 将顶点 ...
随机推荐
- sql 防注入插入
var strsql = "insert into Staff_Answer (ExamTitleID,QuestionsID,MultipleChoice,RightOption,Answ ...
- LN : leetcode 746 Min Cost Climbing Stairs
lc 746 Min Cost Climbing Stairs 746 Min Cost Climbing Stairs On a staircase, the i-th step has some ...
- 8 Explicit Animations 指明的动画 笔记
8 Explicit Animations 指明的动画 笔记 If you want something done right, do it yourself. 如果你想让事情做好,那就自动来 ...
- 在Servlet中使用@Autowire的方法
在你调用的Servlet中添加如下代码: public void init(ServletConfig config) { try { super.init(config); SpringBeanAu ...
- android 图片叠加效果——两种方法的简介与内容 ,带解决Immutable bitmap passed to Canvas constructor错误
第一种是通过canvas画出来的效果: public void first(View v) { // 防止出现Immutable bitmap passed to Canvas constructor ...
- python大文件读取
python大文件读取 https://stackoverflow.com/questions/8009882/how-to-read-a-large-file-line-by-line-in-pyt ...
- Android(java)学习笔记158:多线程断点下载的原理(JavaSE实现)
1. 为什么需要多线程下载? 服务器的资源有限,同时的平均地分配给每个客户端.开启的线程越多抢占的服务的资源就越多,下载的速度就越块. 2. 下载速度的限制条件? (1)你的电脑手机宽带的带宽 ...
- Unity复杂的旋转-欧拉角和四元数
一.欧拉角欧拉角最容易表示,用三个变量X,Y,Z可以直观的表示绕着某个轴的旋转角度. 在Unity里就是Transform组件的Rotation里的X Y Z三个变量代表了欧拉角 二.四元数四元数相比 ...
- PHP20 PHP面向对象辅助
学习要点 常用函数 命名空间 类的自动加载 常用函数 class_exists 作用:检查类是否已定义 语法格式: bool class_exists ( string $class_name [, ...
- 02.28 day03
print(1 or 3 > 2 and 4 < 5 or 6 and 2 < 7)## while True:# print(11)# print(22)# # break# # ...