Assignment Problem | Practice | GeeksforGeeks

You are the head of a firm and you have to assign jobs to people. You have N persons working under you and you have N jobs that are to be done by these persons. Each person has to do exactly one job and each job has to be done by exactly one person. Each person has his own capability (in terms of time taken) to do any particular job. Your task is to assign the jobs to the persons in such a way that the total time taken is minimum. A job can be assigned to only one person and a person can do only one job.
 
Example 1:
Input:
N = 2
Arr[] = {3, 5, 10, 1}
Output:
4
Explanation:
The first person takes times 3 and 5
for jobs 1 and 2 respectively. The second
person takes times 10 and 1 for jobs 1 and
2 respectively. We can see that the optimal
assignment will be to give job 1 to person 1
and job 2 to person 2 for a total for 3+1 = 4.
Example 2:
Input:
N = 3
Arr[] = {2, 1, 2, 9, 8, 1, 1, 1, 1}
Output:
3 
Explanation: 
The optimal arrangement would be to assign 
job 1 to person 2,job 2 to person 3 and job 
3 to person 1.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function assignmentProblem() which takes an Integer N and an array Arr[] of size N2 as input and returns the time taken for the best possible assignment.
 
Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(N2)
 
Constraints:
1 <= N <= 30
1 <= Arr[i] <= 100
0
shankar shakya9 months ago
shankar shakya
int assignmentProblem(int Arr[], int N) {        vector<vector<int>> v;        int mn=0;
        for(int i=0;i*i<n*n;i++) {="" vector<int=""> t;            // cout<<i<<endl; for(int="" j="i;j&lt;N*N;j+=N)" {="" t.push_back(arr[j]);="" }="" v.push_back(t);="" }="" for(auto="" i:v)="" {="" for(auto="" j:i)="" cout<<j<<"="" ";="" mn+="*min_element(i.begin(),i.end());" cout<<endl;="" }="" return="" mn;="" }="">
0
Jdragon jds2 years ago
Jdragon jds
jeez this office has only one desk cuz when one is working others are waiting for their turn  ps i didnt want to use some hungarian algo without even doing it myself but some help is appreciated#include <bits stdc++.h="">using namespace std;int main() {int t,n,tej;cin >> t;    while(t--) {        tej=0;cin >> n;vector<vector<int>> app(n);        vector<pair<int,pair<int,int>>> application ;        for (int e = 0; e < n; ++e)              for (int j = 0; j < n; ++j) {                int v;cin >> v;                application.push_back({v,{e,j}});                app[e].push_back(v);           }         sort(application.begin(),application.end());         reverse(application.begin(),application.end());        bool vise[n],visj[n];        memset(vise,false,n);memset(visj,false,n);        vector<int> ce(n,0); vector<int> cj(n,0);        int ae=-1,aj=-1;        for(int i=0;i<n*n;i++ ){="" time="" ,{e,j}="" pair<int,int=""> u=application[i].second;              if(vise[u.first] || visj[u.second])continue;            ce[u.first]+=1;cj[u.second]+=1;             if(ce[u.first]==n || cj[u.second]==n ){            vise[u.first]=true;tej+=application[i].first;            visj[u.second]=true; }        }    for(int i=0;i<n;i++ )="" if(!vise[i]="" )ae="i;" for(int="" i="0;i&lt;n;i++" )="" if(!visj[i])aj="i;" if(ae!="-1" &&="" aj!="-1)tej+=app[ae][aj];" cout<<tej<<endl;="" }="" return="" 0;="" }="" <="" code="">
0
Harsh Vardhan2 years ago
Harsh Vardhan
Hated this problem *Ruined 2 days*
0
Pallabi Mondal2 years ago
Pallabi Mondal
using hungerian method getting wrong output for-Input:1619 22 22 79 31 2 77 47 8 28 9 57 54 81 18 8 2 61 78 98 51 47 63 55 7 93 27 59 49 24 56 27 4 22 70 68 93 75 68 35 68 13 27 80 29 87 9 72 36 87 60 76 5 98 5 37 50 29 52 73 18 17 77 95 87 68 9 9 29 94 93 28 25 65 62 50 73 77 22 92 1 71 94 71 71 36 36 20 66 88 95 76 23 39 84 73 96 28 19 50 54 81 31 67 50 2 34 65 22 77 16 51 100 24 30 17 27 45 54 60 14 43 29 6 50 66 80 43 43 93 23 52 13 54 7 87 95 18 70 100 40 77 40 30 53 16 60 68 19 48 88 37 73 86 69 10 13 74 26 84 88 9 14 18 51 38 44 52 27 34 39 40 95 6 66 35 97 29 49 16 57 3 17 96 37 29 37 81 94 42 73 33 75 34 31 65 44 25 20 19 68 21 48 19 83 96 57 37 78 72 41 63 19 40 50 44 81 4 61 22 8 55 98 88 29 52 51 87 4 78 35 75 49 73 50 44 69 14 66 33 33 37 11 95 80 88 82 46 97 62 14 13 67 33 97 47
Its Correct output is:140
And Your Code's output is:118.0
0
Danail Kozhuharov4 years ago
Danail Kozhuharov
Yesss :D !
> Correct Answer.> Execution Time:9.46> Next Suggested Problem: Travelling Salesman Problem
And you know what - no Hungarian at all - just basic DP: https://ide.geeksforgeeks.o...I know, I know - it's impossible, cause its complexity is O( C(n, (n/2)) ), but the stupid judge pissed me off and I just hacked it ...
Just added a "Combination Trimmer" that after each DP iteration trimmed the worst combinations.It didn't work the first several times as I was trimming too low and was throwing out the best solution, but after some "fine-tuning" of the combination trim counter - the judge gave up and accepted the solution :-)
PS: Follow the link above to find several good test cases for N = 16, 16, 18 and 19
0
Nick4 years ago
Nick
Solved it using the Hungarian algorithm in C++ but still hit TLE :(https://ide.geeksforgeeks.o...For the example given earlier with 16 jobs: Time(sec) : 0 Memory(MB) : 3.2166575738525
0
Michael Greene
This comment was deleted.
0
Hemant Dhanuka4 years ago
Hemant Dhanuka
Getting TLE dont know why.. i passed all cases which are coming in my mind...please have a look and tell how to improve time complextiy of my code written in javahttps://ide.geeksforgeeks.o...
0
Wonkyu Lee4 years ago
Wonkyu Lee
I solved it. but time Limit Exceeded.https://ide.geeksforgeeks.o...Time(sec) : 0.16 Memory(MB) : 59.1289
0
Hussain Vali5 years ago
Hussain Vali
its giving expected output is wrong.16
19 22 22 79 31 2 77 47 8 28 9 57 54 81 18 8 2 61 78 98 51 47 63 55 7 93 27 59 49 24 56 27 4 22 70 68 93 75 68 35 68 13 27 80 29 87 9 72 36 87 60 76 5 98 5 37 50 29 52 73 18 17 77 95 87 68 9 9 29 94 93 28 25 65 62 50 73 77 22 92 1 71 94 71 71 36 36 20 66 88 95 76 23 39 84 73 96 28 19 50 54 81 31 67 50 2 34 65 22 77 16 51 100 24 30 17 27 45 54 60 14 43 29 6 50 66 80 43 43 93 23 52 13 54 7 87 95 18 70 100 40 77 40 30 53 16 60 68 19 48 88 37 73 86 69 10 13 74 26 84 88 9 14 18 51 38 44 52 27 34 39 40 95 6 66 35 97 29 49 16 57 3 17 96 37 29 37 81 94 42 73 33 75 34 31 65 44 25 20 19 68 21 48 19 83 96 57 37 78 72 41 63 19 40 50 44 81 4 61 22 8 55 98 88 29 52 51 87 4 78 35 75 49 73 50 44 69 14 66 33 33 37 11 95 80 88 82 46 97 62 14 13 67 33 97 47
expected output :: 140
actually correct output is :95. I  calculated it  manually.but i don't know how its became 140.can any one help me
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.