Repository navigation
Expand file tree
/
Copy pathtask3a.cpp
More file actions
58 lines (51 loc) · 1.6 KB
/
Copy pathtask3a.cpp
File metadata and controls
58 lines (51 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
// Greedy approach
void greedyApproach(vector<vector<int>>& costs) {
// Get the number of persons and initialize the total cost
int n = costs.size();
int totalCost = 0;
// Initialize an array to store the assigned jobs
vector<int> assignedJobs(n, -1);
// Iterate through each person
for (int P = 1; P < n; ++P) {
// Find the job with minimum cost for the current person
int minCost = INT_MAX;
int minJob = -1;
for (int J = 1; J < n; ++J) {
if (costs[P][J] < minCost && assignedJobs[J] == -1) {
minCost = costs[P][J];
minJob = J;
}
}
// Assign the person to the job with minimum cost
assignedJobs[minJob] = P;
totalCost += minCost;
}
// Display the assignment and total cost in a linear format
cout << "Job\tPerson\tCost\n";
cout << "--------------------\n";
for (int job = 1; job < n; ++job) {
cout << "J" << job << "\tP" << assignedJobs[job] << "\t" << costs[assignedJobs[job]][job] << "\n";
}
cout << "--------------------\n";
// Calculate and display the total cost
cout << "Total Cost: " << totalCost << "\n";
}
// Main function
//int main() {
// // The table of jobs
// vector<vector<int>> costs = {
// {0, 0, 0, 0},
// {0, 10, 5, 5},
// {0, 2, 4, 10},
// {0, 5, 1, 7}
// };
//
// // Greedy approach
// greedyApproach(costs);
//
// return 0;
//}