2016-04-01 48 views
0

我正在做我的旅游销售计划(不使用STL)TSP矩阵为什么我的结果总是0?

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

任何人都可以在这里看到问题吗?无论我输入什么东西,我总是得到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; 
    } 

回答

0

以下是比较,而不是转让;它不会改变totalCost

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

为了解决这个问题,写

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

,或等效但更简洁

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

我这样做,但它仍然返回0显然有多个问题 – Remixt

+0

@ClaytonBrant:你是否在'graph [c1] [c2] == cost'中修正了相同的问题? – NPE

+0

是的,我做过,我甚至检查过,以确保第2行4和第6行有元素。 – Remixt