TSP矩阵为什么我的结果总是为0?

时间:2016-04-01 01:36:08

标签: c++ arrays matrix traveling-salesman

我正在制定我的旅行销售计划(不使用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;
   }

1 个答案:

答案 0 :(得分:0)

以下是比较,而非作业;它不会改变totalCost

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

要解决此问题,请写

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

或等同但更简洁

totalCost += graph[i][j];