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.节点和 ...
随机推荐
- JS生成某个范围的随机数(四种情况)
前言: JS没有现成的函数,能够直接生成指定范围的随机数. 但是它有个函数:Math.random() 这个函数可以生成 [0,1) 的一个随机数. 利用它,我们就可以生成指定范围内的随机数. 而涉 ...
- python联接主流SQL的类库个人收藏
我现在主要是用以下这个类库来分别连接oracle,postgresql,mysql,mssql的. PyMySQL,pymssql,cx_Oracle,psycopg2 收藏 一下.. https:/ ...
- ytu 1980:小鼠迷宫问题(DFS 深度优先搜索)
小鼠迷宫问题 Time Limit: 2 Sec Memory Limit: 64 MB Submit: 1 Solved: 1 [Submit][Status][Web Board] Desc ...
- GRE红宝书5-6
page5 adopt: adoration: adore: --ore讲话, oration演讲 adorn: orn表示装饰, ornate adulation: adulate ...
- Visual Studio 2015 RC中的ASP.NET新特性和问题修正
(此文章同时发表在本人微信公众号"dotNET每日精华文章") 微软在Build大会上发布了Visual Studio 2015 RC,这也预示着Visual Studio 201 ...
- kinect学习笔记(三)——深度数据的提取
一.创建Console工程 二.添加kinect引用 里面用引用,打开后 选择然后OK. 三.编写代码(有附加注释) using System; using System.Collections.Ge ...
- python 把函数作为参数 ---高阶函数
把函数作为参数 在2.1小节中,我们讲了高阶函数的概念,并编写了一个简单的高阶函数: def add(x, y, f): return f(x) + f(y) 如果传入abs作为参数f的值: add( ...
- Ajax过程
http://www.cnblogs.com/daicunya/p/6227550.html 1.创建XMLHttpRequest对象,也就是创建一个异步调用对象. 2.创建一个新的HTTP请求,并指 ...
- topcoder SRM 593 DIV2 RaiseThisBarn
#include <vector> #include <string> #include <list> #include <map> #include ...
- cocos2d 遍历CCAarray
cocos2d 遍历CCAarray CCARRAY_FOREACH(children_, node){ }