Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct talk {
- int start;
- int end;
- int stress;
- };
- int main()
- {
- int N = 6; // number of talks
- int M = 600; // duration of conference
- talk T[N];
- // we already have them sorted according to end times
- T[0].start=0; T[0].end=50; T[0].stress=2;
- T[1].start=50; T[1].end=300; T[1].stress=8;
- T[2].start=0; T[2].end=400; T[2].stress=15;
- T[3].start=250; T[3].end=450; T[3].stress=10;
- T[4].start=400; T[4].end=500; T[4].stress=4;
- T[5].start=450; T[5].end=600; T[5].stress=5;
- int infinity = 1000;
- int MAX = 700; // value greater than the maximum of end times
- int L[N][MAX];
- // the following loop initializes the matrix L
- for (int i=0; i<N; i++)
- for (int j=0; j<MAX; j++)
- if (j==0) L[i][j]=0; else L[i][j] = infinity;
- // manually updating the zero-th row to avoid referring to the -1 th later
- for (int y=1; y<=T[0].end; y++) L[0][y]=T[0].stress;
- for (int x=1; x<N; x++) {
- for (int y=1; y<=T[x].end; y++) {
- L[x][y] = L[x-1][y];
- int M = infinity;
- for (int z=T[x].start; z<=y; z++) {
- int c = L[x-1][z]+T[x].stress;
- if (c<M) M=c;
- }
- if (L[x][y]>M) L[x][y]=M;
- }
- }
- cout << "Minimum stressfulness: " << L[N-1][M] << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement