c - 走最短路径
问题描述
嘿,我正在尝试用 C 编写代码,以获得从一个城市移动到另一个城市所需的最少能量。到目前为止,我已经能够从文件中读取内容。我不确定我现在应该做什么。代码和文本文件如下
#include <stdio.h>
#include <stdlib.h>
typedef struct value {
int num;
char start_vertex[250];
char destination_vertex[250];
} value;
int main()
{
const int nLines = 43; // number of lines in my text file
FILE * fptr;
value * valuesPtr = malloc(sizeof(value) * nLines);
if (!valuesPtr) {
puts("cannot allocate memory");
return -1;
}
if((fptr = fopen("energy.txt", "r")) == NULL)
{
perror("Error opening file");
return -1;
}
for(int i = 0; i < nLines; i++ )
{
if (fscanf(fptr, "%249s %249s %d",
valuesPtr[i].start_vertex,
valuesPtr[i].destination_vertex,
&valuesPtr[i].num) != 3) {
printf("errored file line %d\n", i);
break;
}
printf("\nStart vertex: %s \nDestination vertex: %s \nWeight: %d\n\n",
valuesPtr[i].start_vertex, valuesPtr[i].destination_vertex, valuesPtr[i].num);
}
free(valuesPtr);
fclose(fptr);
return 0;
}
此代码仅打印出出发城市和目的地城市以及从一个城市到另一个城市所需的能量。我一直在研究使用 Bellman-Ford 或 Dijkstra 算法,但我不确定如何在我的代码中实现它们这是能量文件:
York Hull 60
Leeds Doncaster -47
Liverpool Nottingham 161
Manchester Sheffield 61
Reading Oxford -43
Oxford Birmingham 103
Birmingham Leicester 63
Liverpool Blackpool 79
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
有人可以给我一个结构化的计划来制定我的算法需要做什么。我想看看从莱斯特到莫法特、从赫尔到牛津、从林肯到布里斯托所需要的最少能源以及你必须经过的城市。
解决方案
using System;
类 GFG {
static int V = 9;
int minDistance(int[] dist,
bool[] sptSet)
{
int min = int.MaxValue, min_index = -1;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min) {
min = dist[v];
min_index = v;
}
return min_index;
}
void printSolution(int[] dist, int n)
{
Console.Write("Vertex Distance "
+ "from Source\n");
for (int i = 0; i < V; i++)
Console.Write(i + " \t\t " + dist[i] + "\n");
}
void dijkstra(int[, ] graph, int src)
{
int[] dist = new int[V]; // The output array. dist[i]
bool[] sptSet = new bool[V];
for (int i = 0; i < V; i++) {
dist[i] = int.MaxValue;
sptSet[i] = false;
}
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u, v] != 0 &&
dist[u] != int.MaxValue && dist[u] + graph[u, v] < dist[v])
dist[v] = dist[u] + graph[u, v];
}
printSolution(dist, V);
}
public static void Main()
{
int[, ] graph = new int[, ] { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
GFG t = new GFG();
t.dijkstra(graph, 0);
}
}
推荐阅读
- r - 计算一个变量的 20 步总和
- r - 使用可自定义的插值点数计算 R 中的 2d 样条路径
- excel - AutoLisp 将特定行的块属性 ID 名称和块计数传递给 GetExcel 例程的“Putcell”命令
- console - 我想使用 close 方法关闭我的控制台。不是通过 X
- javascript - 为什么我的 redux reducer 没有设置正确的状态?
- ionic-framework - 离子中的离子分裂窗格行为
- javascript - How do I get the output to display inside the div after the user fills in the prompts?
- python - django.db.utils.ProgrammingError:(1146,“表'lab_equipment.lab_add'不存在”)
- html - 用于 mvc 的 html 中的中心页面列表
- python-3.x - threading.Lock 实际上是如何工作的?(有多种场景)