【问题标题】:TSP matrix why is my result always 0?TSP矩阵为什么我的结果总是0?
【发布时间】:2016-07-20 16:29:15
【问题描述】:

我正在执行我的旅行销售计划(不使用 STL)

我知道这不应该给我正确的答案。我正在尝试确保首先正确加载我的矩阵。

谁能看到这里的问题?无论我投入什么,总成本总是为 0。

附带说明:如何从一行中读取多个字符。我实际上需要从位置 6 开始的角色。

//method for getting the minimum cost of the given routes.

void getCost(){

for(int i = 0; i <50; i++){


for(int j = 0; j <50; j++){

    if (graph[i][j]>0)
        totalCost == totalCost + graph[i][j];


   }

}

}

   switch (line[0]) {

      case 'c':


        cCount++;

        cout << "Added City: " << line << "\n";

         break;

      case 'a':

         aCount++;

         c1 = line[2];
         c2 = line[4];
         cost = line[6];
         cout << "Added Route: " << line << "\n";
         graph[c1][c2] == cost;



         break;

      default:

        getCost();
        cout << totalCost;

         stop = true;
         break;
   }

【问题讨论】:

    标签: c++ arrays matrix traveling-salesman


    【解决方案1】:

    以下是比较,不是赋值;它不会改变totalCost:

    totalCost == totalCost + graph[i][j];
    

    要解决这个问题,写

    totalCost = totalCost + graph[i][j];
    

    或者,等价但更简洁

    totalCost += graph[i][j];
    

    【讨论】:

    • 我这样做了,但它仍然返回 0。显然问题不止一个
    • @ClaytonBrant:你解决了graph[c1][c2] == cost中的相同问题吗?
    • 是的,我做到了,我什至检查以确保第 2 4 和 6 行有元素。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 2020-01-03
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    相关资源
    最近更新 更多