只访问选定城市并返回开始的最低成本
Minimum cost to visit only selected cities and come back to start?
我了解旅行推销员的问题。假设我只想访问选定的城市,然后回来开始,该怎么做?
假设我的成本矩阵是
A B C D
A 0 1 2 1
B 1 0 1 2
C 2 1 0 1
D 1 2 1 0
如果我想参观所有的城市并回到A。我的最短路径是A->B->C->D
,最小距离是4
。
假设我只想访问B和D。我如何找到最小距离?
这是修改旅行推销员的问题吗?有人能帮我做暴力算法吗?
您可以首先运行Floyd-Warshall来计算所有节点对之间的最短路径。请参阅维基百科文章。一旦你有了精简的成本矩阵,你就可以消除所有你不感兴趣的城市。从那里开始,就是标准的旅行推销员。
由于旅行推销员是NP完全的,所以在它之前运行Floyd Warshall对复杂性来说并不重要。
如果你想要完整的路线(包括绕过无趣的城市以缩短路径),你必须回到Floyd Warshall并重建路径。
我手头没有代码,但这里有一些建议和伪代码可以帮助您:我会通过在内存中存储一个向量和上面的距离矩阵来解决这个问题。类似于:
struct Location{
bool visited;
bool mustVisit;
}
Vector<Location> locationVec;
用问题中的位置填充向量,标记是否必须访问这些位置,并始终将visited设置为false。然后是有趣的部分!您需要创建位置Vec的排列。我会递归地这样做,类似于:
void addLocation(int & curLength, int & maxLength, int & curDistance, Vector<Location> &locationVec, Location* lastVisited)
if(curLenth == maxLength){
//check if currentDistance is less than the previously generated best difference, if so
//replace it
lastVisited->visited=0;
return;
}
//Add the next location
for (int& i : locationVec){
//Check if the location has been visited, and if it must be visited.
//If so: mark as visited, point lastVisited to it, and break
//Also add from lastVisited to the new location to your curDistance
curLength++;
}
addLocation(curLength, maxLength, curDistance, locationVec, lastVisited);
return;
}
这应该会让你开始。当您将visited从visited=1更改为visited=0时,请记住减去currentDist,因为您基本上是在"取消访问"城市。您可能还需要跟踪lastlastvisited,具体取决于您的具体实现。
如果你需要加快速度(你可能会的,旅行推销员很慢),请查看Branch and Bound:http://en.wikipedia.org/wiki/Branch_and_bound
相关文章:
- 当回溯以零开始时,如何调试崩溃
- 被解释为低级别const的const对象的地址
- 为什么当我解模块化时,这个C++代代码"效率较低"?
- 根据用户输入用字母填充矢量,并将"开始"和"结束"放在四肢
- 如何从头开始创建 avi 文件?
- 为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?
- 自一周开始以来的秒数?
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 一旦双簧管录制开始,主 UI 线程就会被阻止
- 使用专用显卡进行 OpenGL 渲染时帧速率较低
- GStreamer在开始任何播放之前进行搜索
- 使 std::fstream 写入文件末尾,但从头开始读取
- 在打开多个其他窗口时使用全屏窗口时帧速率非常低
- QPainter或QLabel绘制QPixmap的成本较低
- 区域设置特定于boost区域设置的低成本问题
- 开源或低成本的跨平台视频编解码器库,可用于商业目的,并支持RGBA格式
- 初学者:我应该从高阶还是低阶开始
- 只访问选定城市并返回开始的最低成本
- 将有符号整数转换为二进制浮点数,比反运算成本低
- 使用移动成本低但复制量大的对象初始化容器的首选方法是什么