算法第五章实践报告
1.实践题目:工作分配问题
2.问题描述
7-2 工作分配问题 (20 分)
设有n件工作分配给n个人。将工作i分配给第j个人所需的费用为cij 。 设计一个算法,对于给定的工作费用,为每 一个人都分配1 件不同的工作,并使总费用达到最小。
3.算法描述
本题思想可借鉴旅行售货员算法,采用回溯子空间树的方法回溯,并利用isVisited数组和最小值比较进行剪枝。
private static void backTrack(int depth) {
if (depth >= n) {
if (currentCost < minCost) {
minCost = currentCost;
}
} else {
for (int i=0; i<n; i++) {
if (!isVisit[i]) {
currentCost += a[depth][i];
isVisit[i] = true;
if (currentCost < minCost) {
backTrack(depth + 1);
}
currentCost -= a[depth][i];
isVisit[i] = false;
}
}
}
}