POJ 3616 B - Milking Time dp(深搜超时)

来源:互联网 发布:windows下latex 编辑:程序博客网 时间:2024/05/21 16:02

Description

Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her nextN (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 intervali 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 restR (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 theN 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 theN hours

Sample Input

12 4 21 2 810 12 193 6 247 10 31
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <queue>#include <set>#include <map>#include <algorithm>using namespace std;typedef pair<int, int> P;//#define LOCAL#define INF 0x3f3f3f3f#define MAX_N 1005struct node{    int st, ed , value;} A[MAX_N];long long dp[MAX_N];int N, M, R;bool mycmp(node a, node b){    return a.st < b.st;}int main(){#ifdef LOCALfreopen("b:\\data.in.txt", "r", stdin);#endif    cin >> N >> M >> R;    for(int i = 0; i < M; i++)    {        cin >> A[i].st >> A[i].ed >> A[i].value;    }    sort(A, A + M, mycmp);    for(int i = 0; i < M; i++)    {        dp[i] = A[i].value;//更新每一个点的dp初始值    }    for(int i = 0; i < M; i++)    {        for(int j = i + 1; j < M; j++)        {            if(A[j].st >= A[i].ed + R)                dp[j] = max(dp[i] + A[j].value, dp[j]);        }    }    long long ans = -1;    for(int i = 0; i < M; i++)    {        ans = max(ans, dp[i]);    }    cout << ans << endl;    return 0;}
//深搜是可行的但是超时,所以要从另一个角度解决,也就是每次从前面最大的花费和中更新这次花费
tle代码
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <queue>#include <set>#include <map>#include <algorithm>using namespace std;typedef pair<int, int> P;#define LOCAL#define INF 0x3f3f3f3f#define MAX_N 1005struct node{    int st, ed , value;} A[MAX_N];int N, M, R;bool mycmp(node a, node b){    return a.st < b.st;}int ma = -1;void bfs(int index, int t, int v)//传入操作数的角标{    ma = max(ma, v);//更新结果    if(index == M)    {        return;    }//    if(index != 0)//    {//        cout << A[index - 1].st <<  " " << A[index-1].ed << endl;//            cout << v << endl;//    }    if(index != 0)    {        if(A[index].st >= A[t].ed + R)        {            //可以加上,加或者不加            bfs(index+1, index, A[index].value + v);//加上        }        bfs(index+1, t, v);//不加    }    else    {        //当操作第一个组别时        bfs(index + 1, index, A[index].value + v);//加上        bfs(index + 1, t, v);// 不加    }}int main(){#ifdef LOCALfreopen("b:\\data.in.txt", "r", stdin);#endif    cin >> N >> M >> R;    for(int i = 0; i < M; i++)    {        cin >> A[i].st >> A[i].ed >> A[i].value;    }    sort(A, A + M, mycmp);//    for(int i = 0; i < M; i++)//        cout << A[i].st << endl;    bfs(0, 0, 0);    cout << ma << endl;    return 0;}
0 0