最短路径,带有两个变量

shortest path with two variables

本文关键字:两个 变量 最短路径      更新时间:2023-10-16

因此,我正在尝试使用修改后的贝尔曼·福特算法来找到从启动顶点到结束顶点的最短路径,但我无法走一定距离。因此,给定一个边缘的图:

0 1 100 30
0 4 125 50
1 2 50 250 
1 2 150 50 
4 2 100 40 
2 3 90 60 
4 3 125 150

每条线代表边缘,第一个值是启动顶点,第二个值是端顶点,第三个是成本,第四个是距离。当我尝试找到从0到3的最便宜的路径而不超过140时,我现在拥有的代码会产生0(默认情况下找不到路径时)而不是340(最便宜的路径的成本)。关于如何更改我的代码的任何建议。

只是将代码复制在下面,因为此站点没有让我做其他任何事情。

 static void BellmanFord(struct Graph *graph, int source, int ending, int max){
 int edges = graph->edgeCount;
 int vertices = graph->verticesCount;
 int* money = (int*)malloc(sizeof(int) * vertices);
 int* distance = (int*)malloc(sizeof(int) * vertices);
 for (int I = 0; I < vertices; I++){
       distance[I] = INT_MAX;
       money[I] = INT_MAX;
  }
  distance[source] = 0;
  money[source] = 0;
 for (int I = 1; I <= vertices - 1; ++I){
      for int j = 0; j < edges; ++j){
           int u = graph->edge[j].Source;
           int v = graph->edge[j].Destination;
           int Cost = graph->edge[j].cost;
           int Duration = graph->edge[j].duration;
           if ((money[u] != INT_MAX) && (money[u] + Cost < money[v])){
               if (distance[u] + Duration <= max){
                    money[v] = money[u] + Cost;
                    distance[v] = distance[u] + Duration;
                }
           }
      }
  }
  if (money[ending] == INT_MAX) cout << "0" << endl;
  else cout << money[ending] << endl;
}

请帮忙!这可能不是那么困难,但决赛使我感到压力

这个问题,称为"约束最短路径" 问题,难以解决。您提供的算法无法解决,它只能根据图的结构来 仅通过运气

当您提供的图表上应用此算法时,使用max-distance = 140,它将无法找到解决方案,即0-->1-->2-->3(使用Edge 1 2 150 50),总成本为340,距离为140。

我们可以通过在更新时记录到节点的更新来观察失败的原因,这是结果:

from node       toNode      newCost      newDistance
0                1              100          30
0                4              125          50
1                2              250          80
4                2              225          90

在这里,算法被卡住了,不能进一步走,因为从这一点开始的任何进展都会导致超过最大距离(140)的路径。如您所见,节点2在尊重最大距离约束的同时,找到了节点0的路径0-->4--2。但是现在,从2到3的任何进展都将超过140的距离(因为2-> 3的距离为60。)

再次使用max-distance=150运行会找到路径0-> 4-> 3,成本315和距离150。

from node       toNode      newCost      newDistance
0                1              100          30
0                4              125          50
1                2              250          80
4                2              225          90
2                3              315         150

显然,这不是尊重距离约束的最低成本路径。在第一个示例中,正确的应该相同(未能找到)。这再次证明了算法的失败;这次提供了一个解决方案,但不是最佳的解决方案。

总而言之,这不是代码中的编程错误或错误,仅仅是该算法不足以适合既定问题。

好吧,在

之前
if (money[ending] == INT_MAX) cout << "0" << endl;

我添加了一些使它起作用的代码,但我想知道这将适用于每种情况,还是需要对其进行更改。

   if (money[ending] == INT_MAX){
       for (int j = 0; j < edges; ++j){
             int u = graph->edge[j].Source;
             int v = graph->edge[j].Destination;
             int Cost = graph->edge[j].cost;
             int Duration = graph->edge[j].duration;
             if ((distance[u] != INT_MAX) && (distance[u] +Duration < distance[v])){
                 if (distance[u] + Duration <= max){
                      money[v] = money[u] + Cost;
                       distance[v] = distance[u] + Duration;
                  }
             }
       }
 }