所有顶点间最短路径FloydWarshall算法主例程C++

本文介绍了一次图算法的学习成果,重点展示了使用C++实现的Floyd-Warshall算法过程。该算法用于寻找加权图中所有顶点对之间的最短路径。文章通过具体实例演示了如何构建邻接矩阵并利用该算法解决问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   这个东西,是新学的.上一本书虽然有学过图,但是,这个问题还是没有学到,这一次,学到了.前前后后2天,写出来了.

  最近的主要精力在C++与<<Windows via>> 还有 线性代数,算法的东西实在是前进得不多.

  还好,通过今天上午的努力,知道自己的脑袋,还没有退化.真的,每次实现了一个算法,都兴奋得不得了,确实啊,呵呵.

//	graph.cpp
#include "stdafx.h"
#include "graphRepresentAsAdjacentMatrix.h"
#include <iostream>
#include <vector>

const int Size = 5 ;

using std ::vector ;

int _tmain(int argc, _TCHAR* argv[])
{
	Graph g(Size) ;
	vector<int> indexVector ;
	vector<int> weightVector ;

	indexVector.push_back(1) ;
	indexVector.push_back(2) ;
	indexVector.push_back(4) ;
	weightVector.push_back(3) ;
	weightVector.push_back(8) ;
	weightVector.push_back(-4) ;
	g.importARowOfGraph(indexVector, weightVector) ;		//	0
	indexVector.erase(indexVector.begin(), indexVector.end()) ;
	weightVector.erase(weightVector.begin(), weightVector.end()) ;

	indexVector.push_back(3) ;
	indexVector.push_back(4) ;
	weightVector.push_back(1) ;
	weightVector.push_back(7) ;
	g.importARowOfGraph(indexVector, weightVector) ;		//	1
	indexVector.erase(indexVector.begin(), indexVector.end()) ;
	weightVector.erase(weightVector.begin(), weightVector.end()) ;

	indexVector.push_back(1) ;
	weightVector.push_back(4) ;
	g.importARowOfGraph(indexVector, weightVector) ;		//	2
	indexVector.erase(indexVector.begin(), indexVector.end()) ;
	weightVector.erase(weightVector.begin(), weightVector.end()) ;

	indexVector.push_back(0) ;
	indexVector.push_back(2) ;
	weightVector.push_back(2) ;
	weightVector.push_back(-5) ;
	g.importARowOfGraph(indexVector, weightVector) ;		//	3
	indexVector.erase(indexVector.begin(), indexVector.end()) ;
	weightVector.erase(weightVector.begin(), weightVector.end()) ;

	indexVector.push_back(3) ;
	weightVector.push_back(6) ;
	g.importARowOfGraph(indexVector, weightVector) ;		//	4
	indexVector.erase(indexVector.begin(), indexVector.end()) ;
	weightVector.erase(weightVector.begin(), weightVector.end()) ;

	g.floydWarshall() ;
	g.printPathBetweenTwoVertexes(2, 4) ;

	std ::cin.get() ;


	return 0 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值