【发布时间】:2011-09-12 17:17:11
【问题描述】:
我遇到了一个问题,在网上找不到太多帮助。我需要从多个数字向量中找到数字的最小成本组合。所有向量的向量大小相同。 例如,考虑以下内容:
row [0]: a b c d
row [1]: e f g h
row [2]: i j k l
现在我需要通过从每一行(即向量)中取一个元素来找到数字的组合,例如:aei
在此之后,我需要找到其他三个不相交的组合,例如:bfj、cgk、dhl。我根据选择的这四种组合计算成本。目标是找到成本最低的组合。另一种可能的组合可以是:afj、bei、chk、dgl。如果总列数为 d,行数为 k,则可能的总组合为 d^k。这些行存储为向量。我被困在这里,我发现很难为上述过程编写算法。如果有人可以提供帮助,我将不胜感激。
谢谢。
// I am still working on the algorithm. I just have the vectors and the cost function.
//Cost Function , it also depends on the path chosen
float cost(int a, int b, PATH to_a) {
float costValue;
...
...
return costValue;
}
vector< vector < int > > row;
//populate row
...
...
//Suppose
// row [0]: a b c d
// row [1]: e f g h
// row [2]: i j k l
// If a is chosen from row[0] and e is chosen from row[1] then,
float subCost1 = cost(a,e, path_to_a);
// If i is chosen from row[2] ,
float subCost2 = cost(e,i,path_to_e);
// Cost for selecting aei combination is
float cost1 = subCost1 + subCost2;
//similarly other three costs need to be calculated by selecting other remaining elements
//The elements should not intersect with each other eg. combinations aei and bej cannot exist on the same set.
//Suppose the other combinations chosen are bfj with cost cost2, cgk with cost cost3 and dhl with cost cost4
float totalCost = cost1 + cost2 + cost3 + cost4;
//This is the cost got from one combination. All the other possible combinations should be enumerated to get the minimum cost combination.
【问题讨论】:
-
如果你只是在寻找 C++ 的答案,正如标题所暗示的那样,为什么还要用 C 来标记它?
-
这是作业题吗?如果是这样,它应该被标记为这样
-
如果它们被存储为向量,
C标签可能不合适 -
这似乎是一个基于成本的图路由问题。如果您编辑以显示您已经拥有的一些代码,我们可能会为您提供一些开始。
-
@awoodland 好吧,如果有 c 算法,它也可以应用于 c++。所以我最初标记了 C。
标签: c++ algorithm graph permutation shortest-path