All Posts All Posts

POJ 3616 Milking Time

July 29, 2018·
CS Theory
·1 min read
Tecker Yu
Tecker Yu
AI Native Cloud Engineer × Part-time Investor

Original Problem Link

Knowledge Point: Weighted Interval DP

Solution Report

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct P {
  int start, end, e;
};

bool cmp(const P &a, const P &b) {
  return a.start < b.start;
}

int N, M, R;
struct P a[1002];
int dp[1002];

int main() {
  scanf("%d %d %d", &N, &M, &R);
  int i, j;
  for(i=0;i<M;i++) {
    struct P p;
    scanf("%d %d %d", &p.start, &p.end, &p.e);
    // Convert to weighted interval DP
    p.end += R;
    a[i] = p;
  }

  sort(a, a+M, cmp);
  for(i=0;i<M;++i) {
    dp[i] = a[i].e;
    for(j=0;j<i;++j) {
      // Select all previous time periods allowed by current period and take maximum value
      if (a[j].end <= a[i].start) dp[i] = max(dp[i], dp[j]+a[i].e);
    }
  }

  cout << *max_element(dp, dp+M) << endl;
  return 0;
}

Views