当前位置:网站首页>蛮力法/任务分配
蛮力法/任务分配
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();
}
}
边栏推荐
- Why do some web page style attributes need a colon ":" and some do not?
- Cloud native community boss blog
- pytorch深度学习——卷积操作以及代码示例
- 【Educational Codeforces Round 120 (Rated for Div. 2)】C. Set or Decrease
- LeetCode:497. Random points in non overlapping rectangles -- medium
- 【电脑使用】如何设置没有自启项的软件开机启动
- 农产品期货开户的条件是什么?现在开户的手续费是多少?
- Redis cluster form - sentry mode cluster and high availability mode cluster - redis learning notes 003
- Lengsuanling, a 30-year tortuous history of IPO of a domestic brand
- 连接mysql报错 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors
猜你喜欢

Finally, someone explained the difference among cookies, sessions and tokens. Detailed explanation, interview questions.

LeetCode:497. 非重叠矩形中的随机点————中等

自定义日期组件,左右按钮控制向前或向后翻年、翻月、翻周、翻日

Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"

暗黑破坏神不朽WIKI地址 暗黑破坏神不朽数据库地址分享

Quick start to elastic job, three minutes to experience distributed scheduled tasks

synergy: server refused client with our name

Game compatibility test (general scheme)

Niuke.com: numbers that appear more than half of the times in the array

Arduino中Serial.print()与Serial.write()函数的区别,以及串口通信中十六进制与字符串的收发格式问题和转换过程详解
随机推荐
Mysql database foundation
RuntimeError: Attempting to deserialize object on CUDA device 1 but torch.cuda.device_count() is 1.
pytorch深度学习——卷积操作以及代码示例
中衍期货公司是国内的正规平台吗?开户安全吗?想开个期货账户
【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进
2台电脑共享一套键盘鼠标
Self attention and multi head attention
AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘
Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?
User defined date component. The left and right buttons control forward or backward year, month, week and day turning
You have to learn math to play art?
Pytorch deep learning -- convolution operation and code examples
pytorch深度学习——神经网络卷积层Conv2d
堆排序与加强堆代码 用于记忆
An old programmer of about 10 years said: simple crud function enters the era of codeless development 1. Adding, deleting, modifying and checking interface information
农产品期货开户的条件是什么?现在开户的手续费是多少?
请问九洲期货是正规的吗?开户安不安全
leetcode 划分数组使最大差为 K
Can you still have a wonderful life if you are laid off at the age of 35?
Cut rope / integer split