首页 > 其他 > 详细

poj 3616 Milking Time DP

时间:2014-02-22 10:34:30      阅读:338      评论:0      收藏:0      [点我收藏+]
Milking Time
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4129   Accepted: 1720

Description

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_houriN), an ending hour (starting_houri < ending_houriN), 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 ≤ RN) 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

USACO 2007 November Silver


Source Code
Problem: 3616		
Memory: 240K		Time: 47MS
Language: C++		Result: Accepted

    Source Code

    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    using namespace std;

    typedef struct{
    	int start;
    	int end;
    	int value;
    }Cow;

    Cow cow[1024];
    int dp[1024];

    int cmp(const void *a, const void *b)
    {
    	Cow *_a = (Cow *)a;
    	Cow *_b = (Cow *)b;
    	return _a->start - _b->start;	
    }

    int Max_value(int a, int b)
    {
    	return a>b?a:b;
    }

    int main()
    {
    //	freopen("in.txt","r",stdin);
    	int N,M, R;
    	cin >> N >> M >> R;
    	for(int i=0; i < M; i++)
    	{
    		cin >> cow[i].start >>cow[i].end >> cow[i].value;
    		cow[i].end += R;
    	}
    	qsort(cow, M, sizeof(cow[0]), cmp);
    	for(int i=0; i < M; i++)
    	{
    		dp[i] = cow[i].value;
    		for(int j=0; j < i; j++)
    		{
    			if(cow[i].start >= cow[j].end)
    				dp[i]=Max_value(dp[i], dp[j]+cow[i].value);
    		}
    	}
    	cout << *max_element(dp,dp+M) << endl;

    	return 0;
    }



poj 3616 Milking Time DP

原文:http://blog.csdn.net/china_zoujinyong/article/details/19636515

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!