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