POJ 3171 (minimum interval coverage cost)

Source: Internet
Author: User


Language:
Cleaning Shifts
Time Limit: 1000 MS Memory Limit: 65536 K
Total Submissions: 2093 Accepted: 735
Description
Returns the minimum price of covering [M, E] (0 <= M <= E <= 10,000) with N (1 <= N <= 86,399) intervals.
The price of each interval is S (where 0 <= S <= 500,000 ).

Input
3 integers in the first row: N, M, E.

From the second row to the n + 1 row, each row has three numbers, representing the left endpoint T1, the right endpoint T2, and the cost S.
Output www.2cto.com
Only one line indicates the minimum cost, with no solution loss-1.
Sample Input
3 0 4
0 2 3
3 4 2
0 0 1
Sample Output
5
Hint
Example
Take the first and second intervals.
Source
USACO 2005 December Silver
This is a Dp problem. List the Dp equation first.
F [I] indicates the cost of the range [M, I ].
Apparently F [M-1] = 0, the answer is F [E]
Then the equation is F [a [I]. T2] = min (F [j]) + a [I]. S (T1-1 <= J <= T2-1)
A [I] is arranged in ascending order by T2;
Then apparently a [I] To get, [M, T1-1] has been given in front,
If it is obtained by the following [t1, t2], then there must be t1 <T1 T2 <t2, and there is no need to take [T1, T2.

The minimum number can be used as the O (NlogN) by using the line segment tree ).

[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
# Include <cmath>
# Include <cctype>
# Include <iostream>
# Include <functional>
# Include <algorithm>
Using namespace std;
# Define MAXN (10000 + 10)
# Define MAXE (86399)
# Define MAXS (500000 + 10)
# Define INF (9187201950435737471)
Int n, s, e;
Struct SegMent
{
Int l, r;
Long S;
SegMent (){}
SegMent (int _ l, int _ r, long _ S): l (_ l), r (_ r), S (_ S ){}
Friend bool operator <(const SegMent a, const SegMent B) {return a. r <B. r ;}
} A [MAXN];
Struct SegMentTree // min ()
{
Int n, M;
Long t [MAXE * 10];
Void fillchar (int _ n)
{
N = _ n + 2;
M = 1; while (M-2 <n) M <= 1;
Memset (t, 127, sizeof (t ));
}
Void update (int x)
{
For (x> = 1; x> = 1) t [x] = min (t [x <1], t [(x <1) ^ 1]);
}
Void insert (int x, long c)
{
X = x + 2;
X + = M;
If (t [x]> c) {t [x] = c; update (x );}
}
Long find (int l, int r)
{
L = l + 2; r = r + 2;

L = L-1 + M; r = r + 1 + M;
Long ans = INF;
While (l ^ r ^ 1)
{
If (~ L & 1) ans = min (ans, t [l + 1]);
If (r & 1) ans = min (ans, t [r-1]);
L >>> = 1; r >>> = 1;
}
Return ans;
}
} T;
Int main ()
{
// Freopen ("poj3171.in", "r", stdin );
Scanf ("% d", & n, & s, & e );
For (int I = 1; I <= n; I ++) scanf ("% d", & a [I]. l, & a [I]. r, & a [I]. S );
Sort (a + 1, a + 1 + n );
T. fillchar (e );
T. insert (S-1, 0 );
For (int I = 1; I <= n; I ++)
{
If (a [I]. r <s) continue;
T. insert (a [I]. r, t. find (max (S-1, a [I]. l-1), a [I]. r-1) + a [I]. S );
}
/* For (int I = t. M; I <= t. M * 2; I ++) cout <t. t [I] <'';
Cout <endl;
*/If (t. t [e + 2 + t. M] = INF) cout <"-1 \ n ";
Else cout <t. t [e + 2 + t. M] <endl;


Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.