当前位置:网站首页>蛮力法/任务分配
蛮力法/任务分配
2022-06-10 19:58:00 【xcrj】
任务分配问题是一个全排列问题,先求全排列,再求最小成本任务分配方式
package xcrj.kchalgorithm.bruteForce;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/** * 任务分配问题 * 有n(n≥1)个任务需要分配给n个人执行,一人执行一个任务 * 第i个人执行第j个任务的成本是c[i][j](1≤i,j≤n)。求出总成本最小的分配方案。 * <p> * <p> * 任务分配给人是一个全排列问题,人固定住,任务分配具有相对顺序,所以是全排列问题 * <p> * 增量蛮力法 * 1 * 12 | 21 * 123 312 132 | 321 231 213 * <p> * 递归: * 递归出口:要增加的数i>最大数n * 递归体:每次给原有排列 根据索引j增加i 产生1个新排列 * <p> * 步骤: * 1. 全排列,每种任务分配方式 * 2. 遍历全排列结果,求最小成本任务分配方式 */
public class DutyDistribution {
// 人员数量=任务数量=4
private static int n = 4;
private static int[][] rdCosts = {
{
9, 2, 7, 8}, {
6, 4, 3, 7}, {
5, 8, 1, 8}, {
7, 6, 9, 4}};
// 存放全排列,24是全排列总个数,4!
static Set<List<Integer>> fullPerm = new HashSet<>(24);
static {
List<Integer> list = new ArrayList<>(1);
fullPerm.add(list);
}
public static void insertI(int i) {
Set<List<Integer>> fullPermTemp = new HashSet<>(6);
// 将原来排列集合中所有的排列放入临时集合中
for (List<Integer> list : fullPerm) {
// !复制原来的排列集合中的排列,按索引j增加i
for (int j = 0; j < i; j++) {
List<Integer> listTemp = new ArrayList<>(3);
for (Integer v : list) {
listTemp.add(v);
}
listTemp.add(j, i);
fullPermTemp.add(listTemp);
}
}
// 清除原来的排列集合
fullPerm.clear();
// 复制临时排列集合的所有排列到原来的排列集合
for (List<Integer> list : fullPermTemp) {
List<Integer> l = new ArrayList<>(3);
for (Integer v : list) {
l.add(v);
}
fullPerm.add(l);
}
// 清除临时排列集合
fullPermTemp.clear();
}
public static void fullPermutation2(int i, int n) {
if (i > n) System.out.println("全排列:" + fullPerm);
else {
insertI(i);
fullPermutation2(i + 1, n);
}
}
/*任务分配*/
public static void dutyDistribution() {
fullPermutation2(1, 4);
// 最小任务分配成本
int minCost = Integer.MAX_VALUE;
// 最小成本任务分配
List<Integer> minCostDuties = null;
// 遍历每个任务分配方式,即遍历全排列;第i中任务分配方式,List<Integer>索引代表人,值代表任务
for (List<Integer> duties : fullPerm) {
// 一次任务分配的总成本
int sumCost = 0;
// j代表人的编号
for (int j = 0; j < duties.size(); j++) {
// -1原因,因为全排列的任务编号从1开始
sumCost += rdCosts[j][duties.get(j) - 1];
}
// 比较成本
if (sumCost < minCost) {
minCost = sumCost;
minCostDuties = duties;
}
}
// 输出最小成本任务分配方式
System.out.println(minCostDuties);
// 输出任务分配最小成本
System.out.println(minCost);
}
public static void main(String[] args) {
dutyDistribution();
}
}
边栏推荐
- Li Kou 10821084 solution_ Question of SQL query type
- AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘
- Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"
- Zero trust architecture
- Theoretical basis of distributed services
- P5723 [deep base 4. example 13] prime number pocket
- Build a BPMN modeling Web Service
- 2 pcs share a set of keyboard and mouse
- 連接mysql報錯 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors
- CET-6 - Business English - the last recitation before the test
猜你喜欢

pdf.js-----js解析pdf文件实现预览,并获取pdf文件中的内容(数组形式)

游戏兼容性测试(通用方案)

暗黑破坏神不朽数据库怎么用 暗黑破坏神手游不朽数据库使用方法

服务管理与通信,基础原理分析
![js基础及常考面试题之 [] == ![]结果为true, []==[]结果为false 详解](/img/42/bcda46a9297a544b44fea31be3f686.png)
js基础及常考面试题之 [] == ![]结果为true, []==[]结果为false 详解

JD released ted-q, a large-scale and distributed quantum machine learning platform based on tensor network acceleration

Redis cluster form - sentry mode cluster and high availability mode cluster - redis learning notes 003

2 pcs share a set of keyboard and mouse

CVPR 2022 Tsinghua University proposed unsupervised domain generalization (UDG)
![JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation](/img/42/bcda46a9297a544b44fea31be3f686.png)
JS basic and frequently asked interview questions [] = =! [] result is true, [] = = [] result is false detailed explanation
随机推荐
获取列表中最大最小值的前n个数值的位置索引的四种方法
User defined date component. The left and right buttons control forward or backward year, month, week and day turning
synergy: server refused client with our name
Tutoriel Microsoft Word "5", comment changer les marges de page et créer une barre de nouvelles en word?
Cloud native community boss blog
P5723 【深基4.例13】质数口袋
35岁被裁员,还能拥有美妙人生吗?
Test APK exception control netlocation attacker development
Mixin -- mixed
Canvas advanced functions (medium)
View play and earn will lead crypto games astray
The most common habits from more than 200 English papers written by gradua
Service management and communication, basic principle analysis
pdf.js-----js解析pdf文件实现预览,并获取pdf文件中的内容(数组形式)
【Educational Codeforces Round 120 (Rated for Div. 2)】C. Set or Decrease
LeetCode:497. Random points in non overlapping rectangles -- medium
堆排序与加强堆代码 用于记忆
[technical fragment] implementation of renaming and filtering duplicate name files with suffixes
Build a BPMN modeling Web Service
玩艺术也得学数学?