资源简介
使用c语言,基于win32的工程,实现从文件读取弧段到图,然后实现Dijskra算法和floyd算法,并将结果写入txt文件
代码片段和文件信息
// ShortPath.cpp : Defines the entry point for the console application.
#include “stdlib.h“
#include “stdio.h“
#include “iostream.h“
#include “Stack.h“
#include “omp.h“
#define min(ab) a#define MAX_VERTEX_COUNT 13000
#define INFINITY 100000000.0
#define PathMatrix int*
#define ShortPathTable float*
//图的邻接矩阵数据结构
typedef struct tag_ALGraph
{
float **arcs;//邻接矩阵
int VexNumArcNum;//顶点和弧的数目
}ALGraph;
/////////////////////////////
//函数声明部分
void ShortestPath_DIJ(ALGraph Gint v0PathMatrix PShortPathTable D);
void ShortestPath_Floyd(ALGraph Gfloat **DisMatrix);
void floyd( float **_arrDis int **_arrPath int _nVertexCount );
void Show_Floyd( float **_arrDis int **_arrPath int _nVertexCount );
void Show_DIJ(PathMatrix PShortPathTable Dint _nVertexCountint v0);
//函数的调用以及数据变量的定义和使用,注意要建立在前面已经声明实现以及定义了的基础上
int main(int argc char* argv[])
{
ALGraph G;
int i = 0j=0;
int h=0t=0;
float w = 0.0;
FILE *fp=fopen(“Floyd_Graph.dat““r+“);
//s FILE *fp=fopen(“444.dat““r+“);
//由于顶点数目过多,直接分配静态栈大小将超过2G,故此处采用动态分配内存
//此处动态分配二维数组
G.arcs=(float **)malloc(sizeof(float **)*MAX_VERTEX_COUNT);
for (i=0;i {
G.arcs[i]=(float *)malloc(sizeof(float *)*MAX_VERTEX_COUNT);
}
//矩阵初始化
for (i=0;i {
for (j=0;j {
G.arcs[i][j]=INFINITY;
}
}
G.ArcNum=0;G.VexNum=0;
//读取弧段和顶点
char ch;
while(ch!=EOF)
{
fscanf(fp“%d“&h);//弧头
fscanf(fp“%d“&t);//弧尾
if (h>G.VexNum)
{
G.VexNum=h;
}
if (t>G.VexNum)
{
G.VexNum=t;
}
fscanf(fp“%f“&w);
G.arcs[h][t]=w;
G.ArcNum++;
ch=fgetc(fp);
}
fclose(fp);
printf(“请选择最短路径算法,0 for Dijistk算法,1 for Floyd算法“);
int a;
scanf(“%d“&a);
if (a==0)
{
//开辟内存
PathMatrix P = new int[G.VexNum+1];
ShortPathTable D = new float[G.VexNum+1];
int v0=0;
ShortestPath_DIJ(Gv0PD);//计算最短路径
Show_DIJ(PDG.VexNumv0);
delete []P;P=NULL;
delete []D;D=NULL;
delete []G.arcs;G.arcs=NULL;
}
else if (a==1)
{
int **arrPath=(int**)malloc((G.VexNum+1)*sizeof(int**));
for (i=0;i {
arrPath[i]=(int*)malloc((G.VexNum+1)*sizeof(int*));
}
float **arrDis=(float**)malloc((G.VexNum+1)*sizeof(float**));
for (i=0;i {
arrDis[i]=(float*)malloc((G.VexNum+1)*sizeof(float*));
}
// 先初始化_arrPath
for ( i = 0; i < G.VexNum+1; ++i )
{
for ( j = 0; j < G.VexNum+1; ++j )
{
arrPath[i][j] = i;
}
}
// 先初始化arrDis
for ( i = 0; i < G.VexNum+1; ++i )
{
for ( j = 0; j < G.VexNum+1; ++j )
{
arrDis[i][j] = G.arcs[i][j];
if (i==j) arrDis[i][j]=0;
}
}
floyd(arrDisarrPathG.VexNum);
// Show_Floyd(arrDisarrPathG.VexNum);
//销毁内存,程序注意释放内存以避免内存不足
delete []arrPath;arrPath=NULL;
delete []arrDis;arrDis=NULL;
delete []G.arcs;G.arcs=NULL;
}
return 0;
}
void
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4497 2013-04-14 16:42 NetWork Analysis.dsp
文件 540 2013-04-07 15:33 NetWork Analysis.dsw
文件 50176 2013-04-14 22:28 NetWork Analysis.ncb
文件 54784 2013-04-14 22:28 NetWork Analysis.opt
文件 1864 2013-04-14 22:23 NetWork Analysis.plg
文件 10077 2013-04-14 22:23 ShortPath.cpp
文件 2504 2013-04-13 19:56 Stack.h
文件 261332 2013-04-14 16:55 result_DIJ.txt
文件 12323741 2013-04-14 17:51 result_Floyd.txt
- 上一篇:K均值算法非监督分类
- 下一篇:C语言 决策树 用c语言实现决策树
相关资源
- C语言 决策树 用c语言实现决策树
- ARM程序C语言8个
- c语言 车牌识别
- AD5592驱动程序C语言和中文手册
- 俄罗斯方块C语言实现,用到了C语言的
- 26190C语言在测量与控制中的应用王彤
- 自己用C语言写的俄罗斯方块游戏——
- C语言非常道
- c语言实现ftp文件传输
- [数据结构(C语言版)].严蔚敏_吴伟民
- C语言写的音乐播放器含源代码
- 编译原理 课程设计 DAG 报告+源码C++版
- 用c语言进行图片处理
- C语言程序 产生服从正态分布、瑞利分
- 《大棚温湿度自动控制系统》 包括
- ATM(用C语言编写)的
- C语言程序设计(第五版) 谭浩强课件
- 分别用c语言和c++mfc实现正态分布曲线
- eDNA实时历史数据库接口功能说明及
- C语言点滴
- 手机缴费系统
- STM32实现俄罗斯方块游戏C语言
- 单片机C语言程序设计实训100例--基于
- 混沌理论的实现
- 郝斌C语言详细笔记
- C语言程序设计 中文第二版教材+习题
- C语言 opengl 3d 游戏开发 源码
- 51单片机C语言程序设计教程王云 配套
- C++ Game Development Cookbook
- ECDH加密算法 c语言版
评论
共有 条评论