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的更多相关文章

  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. POJ 2139 Six Degrees of Cowvin Bacon (Floyd)

    题意:如果两头牛在同一部电影中出现过,那么这两头牛的度就为1, 如果这两头牛a,b没有在同一部电影中出现过,但a,b分别与c在同一部电影中出现过,那么a,b的度为2.以此类推,a与b之间有n头媒介牛, ...

  4. POJ 2139 Six Degrees of Cowvin Bacon (floyd)

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

  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 (弗洛伊德最短路)

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

  7. POJ:2139-Six Degrees of Cowvin Bacon

    传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...

  8. Floyed-Warshall算法(求任意两点间最短距离)

    思路:感觉有点像暴力啊,反正我是觉得很暴力,比如求d[i][j],用这个方法求的话,就直接考虑会不会经过点k(k是任意一点) ,最终求得最小值 看代码 #include<iostream> ...

  9. 图算法之Floyd-Warshall 算法-- 任意两点间最小距离

    1.Floyd-Warshall 算法 给定一张图,在o(n3)时间内求出任意两点间的最小距离,并可以在求解过程中保存路径 2.Floyd-Warshall 算法概念 这是一个动态规划的算法. 将顶点 ...

随机推荐

  1. Java-注解(不包含元注解)

    @SuppressWarnings:抑制警告 package java.lang; import java.lang.annotation.*; import static java.lang.ann ...

  2. iframe及其引出的页面跳转问题

    前提:在前一段的工作中碰到了一些页面跳转,子页面跳到父页面上的等等问题,当时页面总是跳不对,或者跳错,要不就是不需要重新打开窗口,却又重新打开一个了,特此搜寻网上各大博客论坛,加上项目经验整理一篇文章 ...

  3. CF765C Table Tennis Game 2

    题意: Misha and Vanya have played several table tennis sets. Each set consists of several serves, each ...

  4. RecyclerView 缓存机制学习笔记1

    盗用别人图片 获取VIew的方法的流程 最先调用 其次调用 这个方法调用会先去缓存 这个是是否有动画,有动画就去里面取. 如果取不到就接着调用 如果在没有继续调用 都取不到就去实例化 调用的次数取决于 ...

  5. spark性能调优--jvm调优(转)

    一.问题切入 调用spark 程序的时候,在获取数据库连接的时候总是报  内存溢出 错误 (在ideal上运行的时候设置jvm参数 -Xms512m -Xmx1024m -XX:PermSize=51 ...

  6. 初用emmet

    下载emmet的pspad插件emmet.js.复制到pspad目录下的 script\JScript 文件夹. 输入 ul#nav>li.item$*4>{Item $} 但是没反应. ...

  7. vue路由导航守卫及前置后置钩子函数参数详解

    首先构建一个测试demo如下图: 接着来探讨路由配置界面 import Vue from 'vue' import Router from 'vue-router' // import HelloWo ...

  8. MYSQL 二次筛选,统计,最大值,最小值,分组,靠拢

    HAVING 筛选后再 筛选 SELECT CLASS,SUM(TOTAL_SCORES) FROM student_score GROUP BY CLASS HAVING SUM(TOTAL_SCO ...

  9. UVA 1175 Ladies' Choice 女士的选择(稳定婚姻问题,GS算法)

    题意: 给出每个男的心目中的女神排序,给出每个女的心目中的男神排序,即两个n*n的矩阵,一旦任意两个非舞伴的男女同学觉得对方都比现任舞伴要好,他们就会抛弃舞伴而在一起.为了杜绝这种现象,求每个男的最后 ...

  10. Ubuntu下编辑并编译运行c++程序

    一.使用vim编辑c++代码: vim hello.cpp 输入如下代码: #include <iostream> using namespace std; int main() { co ...