急,课程设计,最短路径算法,500分

aprilis9 2003-01-08 05:23:43
明天就要交课程设计了
题目是最短路径算法。我一点都不会

题目:
输入图的边和顶点,及边的权值,创建图的邻接表存储
求指定的两个顶点间的最短路径
求任意两个顶点间的最短路径
...全文
115 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhenming_liu 2003-01-09
  • 打赏
  • 举报
回复
/*
It can be compiled in Visual C++.Net, Visual C++ 6.0 devcpp 4.9.5.0
The input format is of the following
The first line consists of two integers, indicating the number of vertices(V), and the number of edges(E)
It is followed by E lines, each line consists of 3 values, first two of which is the name of vertices that is linked (assuming using int type)
the third one is the weight of it

Note that the name of the vertices are from 0 on, but not 1

for example:
for a triangle, there are 3 vertices and 3 edges
you need to input as the following:

Please input the number of vertex: 3
Please input the number of edges: 3

Then you will have to input each edges' information,
one possible input:

please input the 1th edge's information: 0 1 5
please input the 2th edge's information: 1 2 1
please input the 3th edge's information: 2 0 2

it means that it will construct a triangle, with vertices denoted 0, 1, 2
the distance(weight) between 0, 1 is 5
the ... 1, 2 is 1
so the forth

the output of the adjacency link follows:
0->1->2->NULL
1->0->2->NULL
2->0->1->NULL

it indicates that 0 is linked to 1 and 2 (so the forth)

then you can input the 2 vertices' distance you want, take 0, 1 as instance:
Plz input 2 vertices(vertex1 vertex2), -1 indicates terminte: 0 1
Since the shortest path from 0 to 1 is 0->2->1 which is 3
the output will be 3.00000

*/

/*Firstly a matrix is constructed to store the input value. The convert them into linkList type
Note that I use to algorithm of All-Pair=Shortest-Path, which do not need the linkList*/

#include <stdio.h>
#include <stdlib.h>

#define INFINITE 100000000

struct Vertex_T
{
int vertexName;
double weight;

Vertex_T* next;
};

//typedef Vertex_T* Vertex;


int main()
{
int numVer = 0;
int numEdg = 0;

int i = 0;
int j = 0;

printf("Please input the number of vertex: ");
scanf("%d", &numVer);

printf("Please input the number of edges: ");
scanf("%d", &numEdg);

/*allocate the adjacency matrix*/
double** Matrix = NULL;

double tmpWeight = 0;

int tmpVertex_1 = 0;
int tmpVertex_2 = 0;

Matrix = (double** )malloc(numVer * sizeof(double* ));

for (i = 0; i < numVer; i++)
{
Matrix[i] = (double* ) malloc(numVer * sizeof(double));
}

/*initialize the matrix as infinate length*/

for (i = 0; i < numVer; i++)
{
for (j = 0; j < numVer; j++)
{
Matrix[i][j] = INFINITE;
}
}

/*input each edges*/
for (i = 0; i < numEdg; i++)
{
printf("Please input the %dth edge's information(vertex1 vertex2 weight) :", i + 1);

scanf("%d %d %lf", &tmpVertex_1, &tmpVertex_2, &tmpWeight);

Matrix[tmpVertex_1][tmpVertex_2] = Matrix[tmpVertex_2][tmpVertex_1] = tmpWeight;

}

/*create the adjacency list*/

Vertex_T* vertex = (Vertex_T*) malloc( numVer * sizeof(Vertex_T) );

Vertex_T* currentTail = NULL;

for (i = 0; i < numVer; i++)
{
vertex[i].vertexName = i;
vertex[i].next = NULL;

currentTail = &vertex[i];

for (j = 0; j < numVer; j++)
{
if (j == i) continue;

if (Matrix[i][j] >= INFINITE) continue;

currentTail->next = (Vertex_T*) malloc( sizeof(Vertex_T));

currentTail = currentTail->next;

currentTail->vertexName = j;
currentTail->weight = Matrix[i][j];
currentTail->next = NULL;
}

}

/*Output the adjacency link list*/

printf("The adjacency linklist:\n");
for (i = 0; i < numVer; i++)
{
currentTail = &vertex[i];

printf("%d->", i);
while(currentTail->next != NULL)
{
currentTail = currentTail->next;
printf("%d->", currentTail->vertexName);
}
printf("NULL\n");
}

/*calculate the shortest path using All-Pair Shortest Path*/
/*allocate a new matrix*/
double** MatrixResult = NULL;

MatrixResult = (double** ) malloc( numVer * sizeof(double * ) );
for (i = 0; i < numVer; i++)
{
MatrixResult[i] = (double * ) malloc( numVer * sizeof(double *) );
}

int level = 0;
int row = 0;
int col = 0;

for (level = 0; level < numVer; level++)
{
for (row = 0; row < numVer; row++)
{
for (col = 0; col < numVer; col++)
{
if (Matrix[row][col] < Matrix[row][level] + Matrix[level][col])
{
MatrixResult[row][col] = Matrix[row][col];
}
else
{
MatrixResult[row][col] = Matrix[row][level] + Matrix[level][col];
}
}
}

/*assign MatrixResult to Matrix, regard matrix as an temp*/

for (row = 0; row < numVer; row++)
{
for (col = 0; col < numVer; col++)
{
if (MatrixResult[row][col] > INFINITE) Matrix[row][col] = INFINITE;

Matrix[row][col] = MatrixResult[row][col];
}
}

}/*for (level = 0; level < numVer; level++)*/


printf("Plz input 2 vertices(vertex1 vertex2), -1 -1 indicates terminte: ");
scanf("%d %d", &tmpVertex_1, &tmpVertex_2);


while(tmpVertex_1 >= 0 && tmpVertex_2 >= 0)
{
double shtestPath = MatrixResult[tmpVertex_1][tmpVertex_2];
if (shtestPath >= INFINITE) printf("Not linked!\n");
else printf("%lf\n", shtestPath);

printf("Plz input 2 vertices(vertex1 vertex2), -1 -1 indicates terminte: ");
scanf("%d %d", &tmpVertex_1, &tmpVertex_2);
}

return 0;
}


/*After all, I hope this will set an good example for you but not make you be lazier in the future.
Hopefully there won't be another guy that help you to write such a program*/
danger1900 2003-01-08
  • 打赏
  • 举报
回复
void Dijkstral(int v0)
{

int i;
bool s[MAX_VEX];
for(i=0;i<dim;i++)
{
d[v0][i]=map[v0][i];
s[i]=false;
if((i!=0)&&(d[v0][i]<INF))
p[v0][i]=v0;
else
p[v0][i]=-1;
}
s[v0]=true;
d[v0][v0]=0;
for(i=0;i<dim;i++)
{
double min=INF;
int u=v0;
for(int j=0;j<dim;j++)
if(!s[j]&&d[v0][j]<min)
{
u=j;
min=d[v0][j];
}
s[u]=true;

for(int w=0;w<dim;w++)
{
if((!s[w])&&(d[v0][w]>d[v0][u]+map[u][w]))
{
d[v0][w]=d[v0][u]+map[u][w];
p[v0][w]=u;
}
}
}


}
aprilis9 2003-01-08
  • 打赏
  • 举报
回复
frman() :
大哥
明天中午就要交

你现在还在吗?
我现在要
lengxueke 2003-01-08
  • 打赏
  • 举报
回复
用PASCAL写的行吗?
frman 2003-01-08
  • 打赏
  • 举报
回复
要去睡觉了,明天晚上写给你不急吧?
aprilis9 2003-01-08
  • 打赏
  • 举报
回复
我是没看书啊
我本来就不喜欢学计算机
但是无奈学了这个专业
我现在只想混过去

谁来帮我?

输入和输出格式不限
frman 2003-01-08
  • 打赏
  • 举报
回复
嘿嘿。。。。别把俺说的这么。。什么嘛

俺只是看了这么多分眼谗:)
snjsj 2003-01-08
  • 打赏
  • 举报
回复
绝对是没看过书的,楼上的不要姑息养奸——害人之心不可有。
frman 2003-01-08
  • 打赏
  • 举报
回复
说清楚些,输入输出的格式
snjsj 2003-01-08
  • 打赏
  • 举报
回复
nnnnnn
aprilis9 2003-01-08
  • 打赏
  • 举报
回复
分数不是问题啊
大家救我

问题解决了我给1000分都可以

要全套的原代码
谢谢谢谢!!!!

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧