warshall-floyd算法:POJ No 2139 Six Degress of Cowvin Bacon(任意两点最短路径))
题目: http://poj.org/problem?id=2139
题解:N只牛,在一组的两只牛,分别两只之间为 “1度”,自己到自己为0度,M组牛。求,N只牛之中,两只牛之间 平均最短度数*100。模板Floyd算法,求任意两点之间最短路径。
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std; const int maxn = + ;
const int INF = ;
int N, M; // N个牛 ,M组电影
//不存在的时候等于无穷大 或者
//d[i][i] = 0
int d[maxn][maxn]; //d[u][v]表示边e=(u,v)的权值
int x[maxn]; void warshall_floyd();
void init(); void init()
{
for (int i = ; i < N; i++) {
for (int j = ; j < N; j++) {
if (i == j) {
d[i][j] = ;
} else {
d[i][j] = INF;
}
}
}
} void warshall_floyd()
{
//经过顶点和不经过顶点的情况
for (int k = ; k < N; k++) {
for (int i = ; i < N; i++) {
for (int j = ; j < N; j++) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
} void solve()
{
int n;
scanf("%d%d", &N, &M);
init(); while (M--)
{
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d", &x[i]);
--x[i];
} for (int i = ; i < n; i++)
{
for (int j = i + ; j < n; j++) {
d[x[i]][x[j]] = d[x[j]][x[i]] = ;
}
}
}
warshall_floyd();
int ans = INF;
for (int i = ; i < N; i++)
{
int sum = ;
for (int j = ; j < N; j++) {
sum += d[i][j];
}
ans = min(ans, sum);
} printf("%d\n", * ans / (N - )); } int main()
{
solve(); return ; }
warshall-floyd算法:POJ No 2139 Six Degress of Cowvin Bacon(任意两点最短路径))的更多相关文章
- <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 ...
- AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...
- 任意两点间最短距离floyd-warshall ---- POJ 2139 Six Degrees of Cowvin Bacon
floyd-warshall算法 通过dp思想 求任意两点之间最短距离 重复利用数组实现方式dist[i][j] i - j的最短距离 for(int k = 1; k <= N; k++) f ...
- javascript实现有向无环图中任意两点最短路径的dijistra算法
有向无环图 一个无环的有向图称做有向无环图(directed acycline praph).简称DAG 图.DAG 图是一类较有向树更一般的特殊有向图, dijistra算法 摘自 http://w ...
- 【算法】单源最短路径和任意两点最短路径总结(补增:SPFA)
[Bellman-Ford算法] [算法]Bellman-Ford算法(单源最短路径问题)(判断负圈) 结构: #define MAX_V 10000 #define MAX_E 50000 int ...
- 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& ...
随机推荐
- 结对项目:四则运算web
1)Coding.Net项目地址 https://git.coding.net/DandelionClaw/WEB_Calculator.git 注:本项目为web端,并且需要连接SQL Server ...
- sleep与信号唤醒的问题 & 内核对信号的处理方式 & udelay
http://www.cnblogs.com/charlesblc/p/6277848.html 注意,sleep是会被信号唤醒的. sleep函数:#include <unistd.h&g ...
- Spring 计划 7.0
Sprint回顾 让我们一次比一次做得更好. 1.回顾组织 主题:“我们怎样才能在下个sprint中做的更好?” 时间:设定为1小时. 参与者:整个团队. 场所:宿舍. 秘书:李新佳. 2. ...
- Alpha阶段敏捷冲刺⑤
1.提供当天站立式会议照片一张. 每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 新增了一个登录界面 完成记账的分类模块 报表界面初步设计 今天要完成 ...
- ES6 常用1
( (1)交换变量的值 ) [x, y] = [y, x]; ( (2)从函数返回多个值 // 返回一个数组function example() { return [1, 2, 3]; } var [ ...
- docker weave安装
1.升级内核到3.10.0以上,安装iproute22.安装 0.80版本:#wget -O /usr/local/bin/weave https://raw.githubusercontent.co ...
- 如何调换antd中Modal对话框确认按钮和取消按钮两个按钮的位置
今天有个工作是把所有的确认按钮放在取消按钮的左边,类似于下图这样的,公司用的时antd组件 但是antd组件的按钮时确认键放在右边的 可以采用下面的方式,将按钮调换过来: 对的,就是在modal里面的 ...
- innobackupx备份原理
1.工具内容 该软件安装完成会有四个工具,如下所示: usr├── bin│ ├── innobackupex│ ├── xbcrypt│ ├── xbstream│ └── xtrabackup 其 ...
- java 连接oracle 进行增删改查
1.在DAO层新增类OraclePersionDao package com.test.dao; import java.sql.*; /** * Created by wdw on 2017/9/1 ...
- 【原创】查询某个SQL在Oracle的会话
select sql_Text,last_active_time from v$sql where sql_text like '%sql语句%'order by last_active_time