DS实验题 Floyd最短路径 & Prim最小生成树
题目:

提示:

Floyd最短路径算法实现(未测试):
//
// main.cpp
// Alg_Floyd_playgame
//
// Created by wasdns on 16/11/19.
// Copyright ? 2016年 wasdns. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <string.h>
using namespace std;
#define endless 1000000001;
int Floydgh[5005][5005];
void Inigh(int n)
{
for (int i = 1; i <= n; i++)
{
Floydgh[i][i] = 0;
for (int j = 1; j <= n; j++)
{
if (i != j) {
Floydgh[i][j] = endless;
}
}
}
}
void Creatgh(int n, int m)
{
Inigh(n);
int i, u, v, w;
for (i = 1; i <= m; i++)
{
cin >> u >> v >> w;
Floydgh[u][v] = w;
Floydgh[v][u] = w;
}
}
void Alg_Floyd(int n)
{
int i, j, k;
for (k = 1; k <= n; k++)
{
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
int t = Floydgh[i][k] + Floydgh[k][j];
if (t < Floydgh[i][j]) {
Floydgh[i][j] = t;
Floydgh[j][i] = t;
}
}
}
}
}
int minjudge(int n)
{
int i, j;
int minlen = endless;
for (i = 1; i <= n; i++)
{
int cnt = 0;
for (j = 1; j <= n; j++)
{
cnt += Floydgh[i][j];
}
if (cnt < minlen) {
minlen = cnt;
}
}
return minlen;
}
int main()
{
int n, m;
cin >> n >> m;
Creatgh(n, m);
Alg_Floyd(n);
cout << minjudge(n) << endl;
return 0;
}
Prim生成树算法实现:
关于Prim算法,请参考我的另外一篇博客:hdoj-1233-还是畅通工程
//
// main.cpp
// Prim
//
// Created by wasdns on 16/11/24.
// Copyright © 2016年 wasdns. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <string.h>
#define maxn 10000005;
using namespace std;
int Primgh[10000][10000];
bool refer[10005];
void Initial(int n, int m)
{
int i, j;
for (i = 1; i <= n; i++)
{
refer[i] = false;
for (j = 1; j <= n; j++)
{
if (i == j) {
Primgh[i][j] = 0;
}
else Primgh[i][j] = maxn;
}
}
int u, v, w;
for (i = 1; i <= m; i++)
{
cin >> u >> v >> w;
Primgh[u][v] = w;
Primgh[v][u] = w;
}
}
int Prim_Alg(int n, int m)
{
Initial(n, m);
int i, j, k;
int ans = 0;
refer[1] = true; //起点为1
for (i = 1; i <= n-1; i++)
{
int minlen = maxn;
int rcd = 1;
for (j = 1; j <= n; j++)
{
if (!refer[j]) continue;
int len1 = maxn;
int rcd1 = 1;
for (k = 1; k <= n; k++)
{
if (!refer[k])
{
if (Primgh[j][k] < len1) {
len1 = Primgh[j][k];
rcd1 = k;
}
}
}
if (len1 < minlen) {
minlen = len1;
rcd = rcd1;
}
}
//char check = 'A'+rcd-1;
//cout << "rcd: " << check << endl;
//cout << "minlen: " << minlen << endl;
refer[rcd] = true;
rcd = 1;
ans += minlen;
}
return ans;
}
int main()
{
int n, m;
cin >> n >> m;
cout << Prim_Alg(n, m) << endl;
return 0;
}
测试样例:
/*
eg1.
Input:
4 6
1 2 1
2 3 2
1 3 3
2 4 3
3 4 5
1 4 4
Output:
6
eg2.
Input:
7 11
1 2 7
1 4 5
2 4 9
2 3 8
2 5 7
3 5 5
4 5 15
4 6 6
5 6 8
5 7 9
6 7 11
Output:
39
*/
2016/11/24
DS实验题 Floyd最短路径 & Prim最小生成树的更多相关文章
- DS实验题 融合软泥怪-2 Heap实现
题目和STL实现:DS实验题 融合软泥怪-1 用堆实现优先队列 引言和堆的介绍摘自:Priority Queue(Heaps)--优先队列(堆) 引言: 优先队列是一个至少能够提供插入(Insert) ...
- DS实验题 Old_Driver UnionFindSet结构 指针实现邻接表存储
题目见前文:DS实验题 Old_Driver UnionFindSet结构 这里使用邻接表存储敌人之间的关系,邻接表用指针实现: // // main.cpp // Old_Driver3 // // ...
- DS实验题 Dijkstra算法
参考:Dijkstra算法 数据结构来到了图论这一章节,网络中的路由算法基本都和图论相关.于是在拿到DS的实验题的时候,决定看下久负盛名的Dijkstra算法. Dijkstra的经典应用是开放最短路 ...
- DS实验题 sights
算法与数据结构实验题 6.3 sights ★实验任务 美丽的小风姑娘打算去旅游散心,她走进了一座山,发现这座山有 n 个景点, 由于山路难修,所以施工队只修了最少条的路,来保证 n 个景点联通,娇弱 ...
- DS实验题 order
算法与数据结构 实验题 6.4 order ★实验任务 给出一棵二叉树的中序遍历和每个节点的父节点,求这棵二叉树的先序和后序遍历. ★数据输入 输入第一行为一个正整数n表示二叉树的节点数目,节点编号从 ...
- DS实验题 PlayGame Kruskal(UnionFindSet)
题目: 思路: 有两种做法,一种是Prim算法,另外一种则是我所使用的Kruskal算法,Kruskal的算法实现可以参考:最小生成树-Prim算法和Kruskal算法,讲的已经是十分清楚了. 具体算 ...
- DS实验题 Order 已知父节点和中序遍历求前、后序
题目: 思路: 这题是比较典型的树的遍历问题,思路就是将中序遍历作为位置的判断依据,假设有个节点A和它的父亲Afa,那么如果A和Afa的顺序在中序遍历中是先A后Afa,则A是Afa的左儿子,否则是右儿 ...
- DS实验题 Inversion
题目: 解题过程: 第一次做这题的时候,很自然的想到了冒泡和选择,我交的代码是用选择写的.基本全WA(摊手). 贴上第一次的代码: // // main.cpp // sequenceschange ...
- DS实验题 Missile
题目: 提示:并没有精度问题. 原题 NOIP2010 导弹拦截 思路 设源点为A(x1, y1)和B(x2, y2). 第一步,用结构体存节点,包括以下元素: 1.横坐标x 2.纵坐标y 3.节点和 ...
随机推荐
- Android数据存储之sharedpreferences与Content Provider
android中对数据操作包含有: file, sqlite3, Preferences, ContectResolver与ContentProvider前三种数据操作方式都只是针对本应用内数据,程序 ...
- spring 源代码地址
spring的源代码地址发生了更改,以前的地址是 https://src.springframework.org/svn/spring-framework/.但现在spring的代码开始使用Git进行 ...
- 电赛初探(一)——正弦波、方波、锯齿波转换
一.题目要求: 1.使用555做出脉冲方波 2.使用TL084运放做出方波和锯齿波 3.使用TLM314稳压做直流偏置 4.方波要求峰峰值为1V,正弦波要求峰值为0~2V,锯齿波要求峰峰值为1V. 二 ...
- “无法加载一个或多个请求的类型。有关更多信息,请检索 LoaderExceptions 属性 “之解决
今天在学习插件系统设计的时候遇到一个问题:“System.Reflection.ReflectionTypeLoadException: 无法加载一个或多个请求的类型. 于是百度一下,很多内容都差不多 ...
- 2016.6.23 PHP实现新闻发布系统主体部分
1.新闻发布系统的列表: <html><meta http-equiv="Content-Type" content="text/html; chars ...
- 用indexOf判断设备
通过userAgent去判断,先判断是否为移动端,可以判断是iOS终端和Android终端,也可以具体到应用进行判断微信,微博,qq访问. <!DOCTYPE html> <html ...
- 金子上的友情[XDU1011]
Problem 1011 - 金子上的友情 Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 336 Ac ...
- javascript生成n至m的随机整数
摘要: 本文讲解如何使用js生成n到m间的随机数字,主要目的是为后期的js生成验证码做准备. Math.random()函数返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) 生成n-m,包 ...
- xamarin studio And linq 查询方式分析
在 Windows 操作系统可以正常读取网络上的 https 数据流,在 Linux 操作系统中会失败:http://www.cnblogs.com/skyivben/archive/2012/03/ ...
- 原来还有这样的记词方法_Java版记不规则动词_博主推荐
昨天在看一本英语书的不规则动词的时候,突然产生的灵感:就是想把这样记单词简单方式,用程序代码实现,然后,使用户可以与之进行交互 这样,在用户背不规则动词的时候就会轻松把它给记住.基于这一点,于是我就思 ...