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. 基于udp协议的套接字及udp协议粘包问题

    udp协议的套接字 udp协议传输  服务端和客户端没有建立连接一说. import socket # 总结一下基础工作流程:服务端生成套接字并绑定ip_port,进入数据传输循环,服务端接受客户端发 ...

  2. Web前端开发的四个阶段(小白必看)

    第一阶段:HTML的学习 超文本标记语言(HyperText Mark-up Language 简称HTML)是一个网页的骨架,无论是静态网页还是动态网页,最终返回到浏览器端的都是HTML代码,浏览器 ...

  3. iOS 解决iOS 9下的http请求发送失败问题

    iOS9中 因为系统要求所有的请求都必须使用https, 所以发送http请求会失败,如果想让程序能够兼容http请求 在info.plist中添加以下代码: 这里需要做的是右键info.plist文 ...

  4. FileZilla Server 端设置passive模式注意事项

    1,需求和问题的产生 实践中需要分布在各地的各个客户端向云端服务器上传文件,因此在阿里云服务器上安装了FileZilla Server软件作为文件FTP服务端. 客户端程序采用FTP方式向服务端传输文 ...

  5. SQLite运算符

    SQLite运算符 SQLite的运算符是什么? 运算符是一个保留字或一个字符主要用于SQLite语句的WHERE子句来执行操作,如比较和算术运算. 操作符用于指定条件的SQLite语句和作为连词在一 ...

  6. Low Speed High Torque Hydraulic Motor: Motion Performance

    Crank connecting rod type low speed high torque hydraulic motor is used earlier, which is called Sta ...

  7. 阿里云ECS搭建node/mongodb开发环境及部署

    一.前端的er在window或mac上安装开发环境应该再清楚不过了.但在服务器上安装还是有点不同的,毕竟是 centOS,从此不得不走上用命令操作…… 二.前期准备 1.首先,我们去阿里云网站阿里云服 ...

  8. 02.28 day03

    print(1 or 3 > 2 and 4 < 5 or 6 and 2 < 7)## while True:# print(11)# print(22)# # break# # ...

  9. Java开发者写SQL时常犯的10个错误

        首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - 导航条 - 首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - iOS ...

  10. django--基础操作

    Django基础操作 django常用命令 创建django项目 django-admin startproject mysite 创建项目完成以后,文件目录结构为: 修改settings文件内容 A ...