所以我试图使用结构数组中的链表生成一个图。每个结构都包含一个图边,即2个节点和一个权重。该图是无向的,因此从节点2到节点1还有一条边。每个结构的内容都是从一个文本文件中扫描进来的(如果有帮助的话,我也可以上传(。
//
struct Edge{
char node_1[20];
char node_2[20];
int weight;
};
\declaring the array of structures
struct Edges edge[49];
一旦完成,我需要实现一个最短路径算法,但我已经在这个步骤上停留了一段时间。
更新;这是我在中扫描的文本文件
Carlisle Newcastle 92
Nottingham Birmingham 77
Leeds York 39
Glasgow Edinburgh 74
Moffat Carlisle 65
Doncaster Hull 76
Northampton Birmingham 90
Leicester Lincoln 82
Sheffield Birmingham 122
Lincoln Doncaster 63
Sheffield Doncaster 29
Bristol Reading 130
Hull Nottingham 145
Blackpool Leeds 116
Birmingham Bristol 139
Manchester Leeds 64
Carlisle Blackpool 140
Leicester Northampton -61
Newcastle York 135
Glasgow Moffat -28
Leicester Sheffield 100
Carlisle Liverpool -30
Birmingham Manchester 129
Oxford Bristol 116
Leeds Hull 89
Edinburgh Carlisle 154
Nottingham Sheffield 61
Liverpool Manchester 56
Carlisle Glasgow 50
Sheffield Lincoln 74
York Doncaster 55
Newcastle Edinburgh 177
Leeds Sheffield 53
Northampton Oxford 68
Manchester Carlisle 20
1节点的数据结构
例如(单个(链表:
typedef struct node {
char name[20];
struct node* next;
} node;
2寻找最短路径的算法
您应该在实现边之前选择算法,因为其中一些算法在某些数据结构上非常有效或无效。
由于你的数据中有负权重,像Dijkstra这样的知名人士是不可能的。由于负循环,您的程序甚至可能没有解决方案。
据我所知,贝尔曼·福特应该工作。
最终
现在由你决定。如果你在上面的某个步骤上遇到了困难,请随时询问。