当前位置:网站首页>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();
}
}
边栏推荐
- What are the conditions for opening an account for agricultural futures? How much is the service charge for opening an account now?
- Heap sorting and hardening heap code for memory
- Power consumption development experience sharing: design power consumption board
- 中衍期货公司是国内的正规平台吗?开户安全吗?想开个期货账户
- pytorch深度学习——神经网络卷积层Conv2d
- LeetCode:497. 非重叠矩形中的随机点————中等
- In depth learning experience and tools
- 20192407 2021-2022-2 experimental report on Experiment 8 of network and system attack and Defense Technology
- User defined date component. The left and right buttons control forward or backward year, month, week and day turning
- Canvas advanced functions (Part 1)
猜你喜欢

LeetCode:1037. Effective boomerang - simple

canvas 高级功能(上)

六级考试-商务英语-考前最后一背

User defined date component. The left and right buttons control forward or backward year, month, week and day turning

Construction of RT thread smart win10 64 bit compilation environment

2 pcs share a set of keyboard and mouse

In MySQL basics, MySQL adds an automatically added primary key (or any field) to an existing table

72. editing distance ●●

How to use Diablo immortal database

AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘
随机推荐
知识图谱/关系可视化
The new audio infinix machine appears in the Google product library, and Tecno CaMon 19 is pre installed with Android 13
2 pcs share a set of keyboard and mouse
Magic tower game implementation source code and level generation
Serial Print() and serial The difference of write() function, and the problem of hexadecimal and string sending and receiving format in serial port communication and detailed explanation of the conver
Game compatibility test (general scheme)
自注意力(self-attention)和多头注意力(multi-head attention)
Meetup Preview: introduction to the new version of linkis and the application practice of DSS
The excess part of the table setting is hidden. Move the mouse to display all
The most common habits from more than 200 English papers written by gradua
Canvas advanced functions (medium)
蛮力法/1~n的幂集 v4 递归
简解深度学习Attention
MySQL service startup failed
深度学习调参经验和工具
记录一下今天的MySQL故障
【生成对抗网络学习 其一】经典GAN与其存在的问题和相关改进
H5 van-popup全屏弹窗,模拟页面回退效果,支持左上角返回按钮,适用物理返回,侧滑与底部返回键
Microsoft Word tutorial, how to change page orientation and add borders to pages in word?
連接mysql報錯 errorCode 1129, state HY000, Host ‘xxx‘ is blocked because of many connection errors