-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwintsch.cpp
More file actions
93 lines (79 loc) · 2.39 KB
/
wintsch.cpp
File metadata and controls
93 lines (79 loc) · 2.39 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Structure to represent a job
struct Job {
int start;
int finish;
int value;
};
// Function to compare jobs based on finish time
bool compareJobs(Job& job1, Job& job2) {
return job1.finish < job2.finish;
}
// Function to find the latest non-overlapping job
int latestNonOverlap(vector<Job>& jobs, int index) {
for (int i = index - 1; i >= 0; i--) {
if (jobs[i].finish <= jobs[index].start) {
return i;
}
}
return -1;
}
// Function to find the maximum sum of values of jobs that can be selected
int weightedIntervalScheduling(vector<Job>& jobs) {
// Sort jobs based on finish times
sort(jobs.begin(), jobs.end(), compareJobs);
int n = jobs.size();
// Initialize DP table to store maximum values
vector<int> dp(n);
dp[0] = jobs[0].value;
// Trace back to find the selected jobs
vector<int> selectedJobs(n, -1);
selectedJobs[0] = 0;
for (int i = 1; i < n; i++) {
// Include the current job
int inclValue = jobs[i].value;
int latest = latestNonOverlap(jobs, i);
if (latest != -1) {
inclValue += dp[latest];
}
// Store maximum value at this index
dp[i] = max(inclValue, dp[i - 1]);
// Update selectedJobs list
if (inclValue > dp[i - 1]) {
selectedJobs[i] = i;
} else {
selectedJobs[i] = selectedJobs[i - 1];
}
}
// Trace back to find selected jobs
int maxVal = dp[n - 1];
int selectedAmount = 0;
vector<Job> selectedList;
for (int i = n - 1; i >= 0; i--) {
if (dp[i] != dp[i - 1]) {
selectedList.push_back(jobs[i]);
selectedAmount++;
}
}
// Output the result
cout << "Maximum sum of selected job values: " << maxVal << endl;
cout << "Number of selected jobs: " << selectedAmount << endl;
cout << "Selected jobs:" << endl;
for (int i = selectedList.size() - 1; i >= 0; i--) {
cout << "Job: " << i + 1 << " Start: " << selectedList[i].start << " Finish: " << selectedList[i].finish << " Value: " << selectedList[i].value << endl;
}
return maxVal;
}
int main() {
vector<Job> jobs = {
{1, 3, 5},
{2, 5, 6},
{4, 6, 5},
{6, 7, 4}
};
cout << weightedIntervalScheduling(jobs);
return 0;
}