Advertisement
Guest User

Untitled

a guest
Nov 14th, 2014
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1.    
  2.  
  3.     #include <iostream>
  4.      
  5.     using namespace std;
  6.      
  7.     struct talk {
  8.         int start;
  9.         int end;
  10.         int stress;
  11.     };
  12.      
  13.     int main()
  14.     {
  15.         int N = 6;  // number of talks
  16.         int M = 600;  // duration of conference
  17.         talk T[N];
  18.         // we already have them sorted according to end times
  19.         T[0].start=0;    T[0].end=50;    T[0].stress=2;
  20.         T[1].start=50;    T[1].end=300;    T[1].stress=8;
  21.         T[2].start=0;    T[2].end=400;    T[2].stress=15;
  22.         T[3].start=250;    T[3].end=450;    T[3].stress=10;
  23.         T[4].start=400;    T[4].end=500;    T[4].stress=4;
  24.         T[5].start=450;    T[5].end=600;    T[5].stress=5;
  25.        
  26.         int infinity = 1000;
  27.         int MAX = 700; // value greater than the maximum of end times
  28.         int L[N][MAX];
  29.        
  30.         // the following loop initializes the matrix L
  31.         for (int i=0; i<N; i++)
  32.             for (int j=0; j<MAX; j++)
  33.                     if (j==0) L[i][j]=0; else L[i][j] = infinity;
  34.                    
  35.                    
  36.         // manually updating the zero-th row to avoid referring to the -1 th later
  37.         for (int y=1; y<=T[0].end; y++) L[0][y]=T[0].stress;
  38.        
  39.         for (int x=1; x<N; x++) {    
  40.             for (int y=1; y<=T[x].end; y++) {
  41.                 L[x][y] = L[x-1][y];
  42.                 int M = infinity;
  43.                 for (int z=T[x].start; z<=y; z++) {
  44.                     int c = L[x-1][z]+T[x].stress;
  45.                     if (c<M) M=c;
  46.                 }
  47.                 if (L[x][y]>M) L[x][y]=M;
  48.             }
  49.         }
  50.        
  51.         cout << "Minimum stressfulness: " << L[N-1][M] << endl;
  52.        
  53.        return 0;
  54.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement