Milking Time
64-bit integer IO format: %lld Java class name: Main
Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible.
Farmer John has a list of M (1 ≤ M ≤ 1,000) possibly overlapping intervals in which he is available for milking. Each interval i has a starting hour (0 ≤ starting_houri ≤ N), an ending hour (starting_houri < ending_houri ≤ N), and a corresponding efficiency (1 ≤ efficiencyi ≤ 1,000,000) which indicates how many gallons of milk that he can get out of Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must be milked through an entire interval.
Even Bessie has her limitations, though. After being milked during any interval, she must rest R (1 ≤ R ≤ N) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in the N hours.
Input
* Line 1: Three space-separated integers: N, M, and R
* Lines 2..M+1: Line i+1 describes FJ's ith milking interval withthree space-separated integers: starting_houri , ending_houri , and efficiencyi
Output
* Line 1: The maximum number of gallons of milk that Bessie can product in the N hours
Sample Input
12 4 2 1 2 8 10 12 19 3 6 24 7 10 31
Sample Output
43
Source
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 /* 2 @author: Lev 3 @date: 4 */ 5 #include <iostream> 6 #include <cstdio> 7 #include <cmath> 8 #include <cstring> 9 #include <string> 10 #include <cstdlib> 11 #include <algorithm> 12 #include <map> 13 #include <set> 14 #include <queue> 15 #include <climits> 16 #include <deque> 17 #include <sstream> 18 #include <fstream> 19 #include <bitset> 20 #include <iomanip> 21 #define LL long long 22 #define INF 0x3f3f3f3f 23 24 using namespace std; 25 const int maxn = 1000010; 26 struct arc{ 27 int to,next,cost; 28 arc(int x = 0,int y = 0,int z = -1){ 29 to = x; 30 cost = y; 31 next = z; 32 } 33 }; 34 arc e[maxn]; 35 int head[maxn],tot,x[maxn],y[maxn],c[maxn]; 36 LL d[maxn]; 37 void add(int u,int v,int cost){ 38 e[tot] = arc(v,cost,head[u]); 39 head[u] = tot++; 40 } 41 queue<int>q; 42 bool in[maxn]; 43 void spfa(){ 44 for(int i = 0; i < maxn; ++i){ 45 d[i] = 0; 46 in[i] = false; 47 } 48 while(!q.empty()) q.pop(); 49 q.push(0); 50 while(!q.empty()){ 51 int u = q.front(); 52 q.pop(); 53 in[u] = false; 54 for(int i = head[u]; ~i; i = e[i].next){ 55 if(d[e[i].to] < d[u] + e[i].cost){ 56 d[e[i].to] = d[u] + e[i].cost; 57 if(!in[e[i].to]){ 58 in[e[i].to] = true; 59 q.push(e[i].to); 60 } 61 } 62 } 63 } 64 } 65 int main(){ 66 int N,M,R; 67 while(~scanf("%d %d %d",&N,&M,&R)){ 68 for(int i = 1; i <= M; ++i) 69 scanf("%d %d %d",x+i,y+i,c+i); 70 memset(head,-1,sizeof(head)); 71 tot = 0; 72 for(int i = 1; i <= M; ++i) 73 for(int j = 1; j <= M; ++j) 74 if(x[j] >= y[i]+R) add(i,j,c[j]); 75 for(int i = 1; i <= M; ++i) 76 add(0,i,c[i]); 77 spfa(); 78 LL ans = 0; 79 for(int i = 1; i <= M; ++i) 80 ans = max(ans,d[i]); 81 printf("%I64d\n",ans); 82 } 83 return 0; 84 }