After resigning from academic positions due to high stress, Socrates founded a trading company called "Socrates & Sons Trading". In this company
N
N work and
N
There are N employees, each employee can do any of the tasks. But the cost of doing each job by each employee is different. Since Socrates has seen a lot of financial losses to date, he wants all the company's work to be done at the lowest possible cost.
You have to write a program that tells Socrates which job to choose for each employee so that the cost spent on the employees is minimized and his situation gets a little better!
input
First line: number of employees and jobs
Line 2
N
+
1
N+1: cost matrix of each employee to perform each task. The first line is related to the expenses of the first employee and the second line is related to the expenses of the second employee and...
1
≤
N
≤
10
1≤N≤10
output
to the number
N
N line that line
i
iam indicates the work number that the employee must have
i
to do
To solve this assignment problem, we are dealing with a classic combinatorial optimization challenge where we need to assign N employees to N tasks such that the total cost is minimized. The costs are given in a matrix where each cell ( C[i][j] ) represents the cost of assigning employee ( i ) to task ( j ).
Given the small constraints ( ( N \leq 10 ) ), we can efficiently solve this problem using a brute-force approach by generating all possible permutations of task assignments and selecting the one with the minimal total cost.
Here's how the solution works:
Read the Input: Parse the number of employees/tasks and read the cost matrix.
Generate All Permutations: Use Python's built-in itertools.permutations function to generate all possible permutations of task assignments.
Calculate Total Costs: For each permutation, calculate the total cost by summing up the costs of assigning each employee to a task according to the permutation.
Keep Track of the Minimum: Keep track of the permutation with the minimal total cost.
Output the Assignment: Once all permutations have been evaluated, output the task indices assigned to each employee.
Here is the implementation of the above approach:
python
import sys
from itertools import permutations
def main():
import sys
import threading
def run():
import sys
N = int(sys.stdin.readline())
C = []
for _ in range(N):
C.append(list(map(int, sys.stdin.readline().split())))
min_total_cost = None
min_assignment = None
tasks = range(N)
# Initialize minimal cost with a large number
min_total_cost = float('inf')
# For N>8, the permutations get above 40,000, so we might do some pruning
# However, since N<=10 (up to 3.6 million permutations), we can process all permutations.
for perm in permutations(tasks):
total_cost = sum(C[i][perm[i]] for i in range(N))
if total_cost < min_total_cost:
min_total_cost = total_cost
min_assignment = perm
# Output the assignment
for i in range(N):
print(min_assignment[i])
threading.Thread(target=run).start()
if __name__ == '__main__':
main()
Explanation:
We read the number of tasks/employees, and then construct the cost matrix.
We then generate all possible permutations of task indices (from 0 to ( N-1 )) since each task can be assigned to exactly one employee.
For each permutation, we calculate the total cost and compare it with the current minimal total cost.
If we find a permutation with a lower total cost, we update our minimal total cost and store the corresponding assignment.
After evaluating all permutations, we output the tasks assigned to each employee in order, starting from employee 0.
This approach is feasible for ( N \leq 10 ) as given in the constraints, even though ( 10! = 3,628,800 ) permutations might seem large, modern computers can handle this in reasonable time.