资源简介
使用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语言模拟文件管理系统844
- C语言开发实战宝典
- C++中头文件与源文件的作用详解
- C语言代码高亮html输出工具
- 猜数字游戏 c语言代码
- C语言课程设计
- 数字电位器C语言程序
- CCS FFT c语言算法
- 使用C语言编写的病房管理系统
- 通信过程中的RS编译码程序(c语言)
- 计算机二级C语言上机填空,改错,编
- 用回溯法解决八皇后问题C语言实现
- 简易教务管理系统c语言开发文档
- 操作系统课设 读写者问题 c语言实现
- 小波变换算法 c语言版
- C流程图生成器,用C语言代码 生成C语
- 3des加密算法C语言实现
- 简单的C语言点对点聊天程序
- 单片机c语言源程序(51定时器 八个按
- 个人日常财务管理系统(C语言)
- c语言电子商务系统
- 小甲鱼C语言课件 源代码
- 将图片转换为C语言数组的程序
- C语言实现的一个内存泄漏检测程序
- DES加密算法C语言实现
- LINUX下命令行界面的C语言细胞游戏
- 用单片机控制蜂鸣器播放旋律程序(
- 学校超市选址问题(数据结构C语言版
- 电子时钟 有C语言程序,PROTEUS仿真图
- 尚观培训linux许巍老师关于c语言的课
评论
共有 条评论