本文整理汇总了C++中Subset::add方法的典型用法代码示例。如果您正苦于以下问题:C++ Subset::add方法的具体用法?C++ Subset::add怎么用?C++ Subset::add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Subset
的用法示例。
在下文中一共展示了Subset::add方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: solveTSP
float TSPProblem::solveTSP()
{
allocateTSPCache_();
//base cases
{
for (unsigned int i=0; i<rows_; i++) {
cache_[i][0] = std::numeric_limits<float>::max();
}
Subset s; s.add(0);
cache_[s.repr()][0] = 0; // from node to itself using exactly itself
}
//meat of the algorithm
for (unsigned int m=2; m <= cities_.size(); m++) { //subset size (including 0)
vector<Subset> subsets;
getSubsets(subsets, m, cities_.size());
for (const Subset& s : subsets) {
for (unsigned int j=1; j<cities_.size(); j++) { //omit start node 0
if (!(s.contains(j))) { // omit stuff not in subset
continue;
}
Subset sprime(s);
sprime.remove(j);
float minval = std::numeric_limits<float>::max();
for (unsigned int k=0; k<cities_.size(); k++) {
if (!(s.contains(k)) || k == j) {
continue;
}
float localCost = cache_[sprime.repr()][k] + distance(cities_[k], cities_[j]);
if (localCost < minval) {
minval = localCost;
}
}
cache_[s.repr()][j] = minval;
}
}
}
//finally find minimum of all paths computed so far
float minval = std::numeric_limits<float>::max();
{
Subset s;
for (unsigned int i=0; i<cities_.size() ; i++) {
s.add(i);
}
for (unsigned int j=1; j < cities_.size(); j++) {
float localCost = cache_[s.repr()][j] + distance(cities_[j], cities_[0]);
if (localCost < minval) {
minval = localCost;
}
}
}
return minval;
}