当前位置:网站首页>Brute force method / task assignment
Brute force method / task assignment
2022-06-10 21:09:00 【xcrj】
Task assignment problem is a full permutation problem , First, complete the permutation , Then find the minimum cost task allocation method
package xcrj.kchalgorithm.bruteForce;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/** * Task assignment problem * Yes n(n≥1) A task needs to be assigned to n Personal execution , One person performs a task * The first i The individual executes the j The cost of a task is c[i][j](1≤i,j≤n). Find the allocation scheme with the lowest total cost . * <p> * <p> * Task assignment to people is a full permutation problem , People fixed , Task assignment has a relative order , So it's a full permutation problem * <p> * Incremental brute force method * 1 * 12 | 21 * 123 312 132 | 321 231 213 * <p> * recursive : * Recursive export : The number to be added i> The maximum number n * Recursive body : Give the original arrangement each time Index based j increase i produce 1 New permutations * <p> * step : * 1. Full Permutation , Each task assignment method * 2. Traverse the full permutation result , Find the minimum cost task allocation method */
public class DutyDistribution {
// Number of people = Number of tasks =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}};
// Store in full line ,24 Is the total number of all permutations ,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);
// Put all permutations in the original permutation set into the temporary set
for (List<Integer> list : fullPerm) {
// ! Copy the permutations in the original permutation set , By index j increase 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);
}
}
// Clear the original permutation set
fullPerm.clear();
// Copy all permutations of the temporary permutation set to the original permutation set
for (List<Integer> list : fullPermTemp) {
List<Integer> l = new ArrayList<>(3);
for (Integer v : list) {
l.add(v);
}
fullPerm.add(l);
}
// Clear temporary spread collection
fullPermTemp.clear();
}
public static void fullPermutation2(int i, int n) {
if (i > n) System.out.println(" Full Permutation :" + fullPerm);
else {
insertI(i);
fullPermutation2(i + 1, n);
}
}
/* Task assignment */
public static void dutyDistribution() {
fullPermutation2(1, 4);
// Minimum task allocation cost
int minCost = Integer.MAX_VALUE;
// Minimum cost task allocation
List<Integer> minCostDuties = null;
// Traverse each task assignment method , That is, traverse the full permutation ; The first i Task allocation method in ,List<Integer> The index represents , Value represents task
for (List<Integer> duties : fullPerm) {
// The total cost of a task assignment
int sumCost = 0;
// j The number of the representative
for (int j = 0; j < duties.size(); j++) {
// -1 reason , Because the task number of the full permutation is from 1 Start
sumCost += rdCosts[j][duties.get(j) - 1];
}
// Compare costs
if (sumCost < minCost) {
minCost = sumCost;
minCostDuties = duties;
}
}
// Output the minimum cost task allocation method
System.out.println(minCostDuties);
// Output task assignment minimum cost
System.out.println(minCost);
}
public static void main(String[] args) {
dutyDistribution();
}
}
边栏推荐
- 连接mysql报错 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors
- LeetCode:497. 非重叠矩形中的随机点————中等
- 【电脑使用】如何设置没有自启项的软件开机启动
- 获取的网络时间 + 时区(+8)
- 蛮力法/1~n的全排列 v3 递归
- Meetup Preview: introduction to the new version of linkis and the application practice of DSS
- Unity analyzes the rendering of built-in terrain and does some interesting things
- Node (express) implements interfaces such as adding, deleting, modifying, and paging
- How to use Diablo immortal database
- mysql基础篇之mysql在已有表中添加自动增加的主键(或任意一个字段)
猜你喜欢

Self attention and multi head attention

synergy: server refused client with our name

RuntimeError: Attempting to deserialize object on CUDA device 1 but torch. cuda. device_ count() is 1.

Solve the problem that the idea automatically becomes * when there are more than 5 identical packages

Redis缓存穿透

知识图谱/关系可视化

Jiangbolong forestee xp2000 PCIe 4.0 SSD multi encryption function, locking data security

Knowledge map / relationship visualization

72. 编辑距离 ●●●

Game compatibility test (general scheme)
随机推荐
Attack and defense drill | network security "whistleblower": security monitoring
Elastic-Job的快速入门,三分钟带你体验分布式定时任务
保姆级教程:如何成为Apache Linkis文档贡献者
The most common habits from more than 200 English papers written by gradua
Node (express) implements interfaces such as adding, deleting, modifying, and paging
Redis缓存雪崩
pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)
Cut rope / integer split
leetcode 划分数组使最大差为 K
用一个性能提升了666倍的小案例说明在TiDB中正确使用索引的重要性
In MySQL basics, MySQL adds an automatically added primary key (or any field) to an existing table
获取的网络时间 + 时区(+8)
Monitoring is easy to create a "quasi ecological" pattern and empower Xinchuang to "replace"
服务管理与通信,基础原理分析
Is Zhongyan futures reliable? Is it a regular futures company? Is it safe to open an account?
canvas 高级功能(上)
自注意力(self-attention)和多头注意力(multi-head attention)
PDF. JS - - - - JS analyse le fichier PDF pour réaliser l'aperçu et obtenir le contenu du fichier PDF (sous forme de tableau)
Uni app custom navigation
H5 van popup full screen pop-up window, simulates the page fallback effect, supports the return button in the upper left corner, and is suitable for physical return, side sliding and bottom return key