C++实现弗洛伊德算法

还没有正儿八经的独立实现过一个什么算法呢,以前写的时候总是把别人的程序放在旁边,自己写的时候时不时瞄两眼,最终觉得是自己写出来的,但是时间一久就又忘记怎么实现的了,其实这是没有真正的理解算法,要是理解了,再加上语言功底可以就很容易去实现它。归根到底写不出来还是没有真正的搞懂,

主函数:

// Frod.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "matrix.h"


int _tmain(int argc, _TCHAR* argv[])
{
	//1初始化图矩阵u和路径矩阵s
	//图矩阵//
	Matrix u("input.txt");
    cout<<"-------------------------------------------------------------------"<<endl;
	//路径矩阵//
	Matrix s(u.getRow(),u.getCol());
	s.initS();
	//2 更新图矩阵//
	for(int k=1;k<=u.getCol();k++)
		{	for (int i=1;i<=u.getCol();i++)
			{
				for (int j=1;j<=u.getCol();j++)
				{
					int temp =u.vistElem(i,k)+u.vistElem(k,j);
					    if (temp >Infinate&&i!=j)
					    {
							temp =Infinate;
					    }
					if(u.vistElem(i,j)>temp)
					{
					  u.setElem(i,j,temp);
					  s.setElem(i,j,s.vistElem(i,k));
					}
				}
			}
	    }
	//3结束输出
	cout<<"-------------------------------------------------------------------"<<endl;
	u.outPutElem();
	cout<<"-------------------------------------------------------------------"<<endl;
	s.outPutElem();
	cout<<"-------------------------------------------------------------------"<<endl;
	return 0;

}
图的数据结构头文件

#ifndef _MY_MATRIX_201419
#define _MY_MATRIX_201419

#include <string>
#include <iostream>
#include <cstdlib>

#define  Infinate 999999

using namespace std;
//元素行列从开始//
class Matrix
{
public:
	Matrix(int row,int col);
	Matrix(string path );
	void setRow(int row);
	void setCol(int col);
	int getRow(){return m_rows;}
	int getCol(){return m_cols;}
	int vistElem(int i,int j);
	void  setElem(int i,int j,int e);
	void initS();
	void outPutElem();
private:
	int m_rows;
	int m_cols;
	int num_of_edge;
	int* m_data;
public:
	virtual ~Matrix();
	
};
#endif
类的具体实现
#include "stdafx.h"
#include"matrix.h"
Matrix::Matrix(int row,int col):m_rows(row),m_cols(col)
{
	m_data =new int[row*col];

}
Matrix:: Matrix(string path )
{
	//用数据文件构造;
	 freopen(path.c_str(),"r",stdin);
	 
	 cin>>m_rows;
	 cin>>m_cols;
	 cin>>num_of_edge;

	  m_data =new int[m_rows*m_cols];

	 for (int i=0;i<m_rows;i++)
	 {
		 for (int j=0;j<m_cols;j++ )
		 {  if(i!=j)
		     {*(m_data+(i)*m_cols+(j))=Infinate;}
		    else
             {*(m_data+(i)*m_cols+(j))=0;}
		 }
	 }

	 int p ,q,weight;
	 for (int i=0;i<m_rows;i++)
	 {
		 for (int j=0;j<m_rows;j++ )
		 {
			 cin>>p>>q>>weight;
				 setElem(p,q,weight);//p指向q//
				// setElem(q,p,weight);//q指向p;表示无向图;
		 }
	 }

	 for(int i=1; i<=m_rows; ++i)
	 {
		 for(int j=1; j<=m_rows; ++j)
			 printf("%7d", vistElem(i,j));
		 printf("\n");
	 }
}
void Matrix:: setRow(int row)
{
	m_rows=row;
}
void Matrix:: setCol(int col)
{
	m_cols =col;
}

int Matrix:: vistElem(int i,int j)
{
  
   return *(m_data+(i-1)*m_cols+(j-1));
}

void Matrix:: setElem(int i,int j,int e)
{
	*(m_data+(i-1)*m_cols+(j-1))=e;
}

void Matrix:: initS()
{
    for (int i=0;i<m_rows;i++)
    {
		for (int j=0;j<m_rows;j++)
		{
			setElem(i+1,j+1,j+1);
		}
    }

	for(int i=1; i<=m_rows; ++i)
	{
		for(int j=1; j<=m_rows; ++j)
			printf("%7d", vistElem(i,j));
		printf("\n");
	}
}
void Matrix::outPutElem()
{
	for(int i=1; i<=m_rows; ++i)
	{
		for(int j=1; j<=m_rows; ++j)
			printf("%7d", vistElem(i,j));
		printf("\n");
	}
}
Matrix:: ~Matrix()
{
  delete []m_data;
}


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值