POJ1201-Intervals(差分约束)

来源:互联网 发布:eclipse是什么软件 编辑:程序博客网 时间:2024/05/16 00:25

Intervals
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 27284 Accepted: 10474

Description

You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. 
Write a program that: 
reads the number of intervals, their end points and integers c1, ..., cn from the standard input, 
computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i=1,2,...,n, 
writes the answer to the standard output. 

Input

The first line of the input contains an integer n (1 <= n <= 50000) -- the number of intervals. The following n lines describe the intervals. The (i+1)-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50000 and 1 <= ci <= bi - ai+1.

Output

The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i=1,2,...,n.

Sample Input

53 7 38 10 36 8 11 3 110 11 1

Sample Output

6

Source

Southwestern Europe 2002


题意:有n个区间,每个区间有3个值,ai,bi,ci代表,在区间[ai,bi]上至少要选择ci个整数点,ci可以在区间内任意取不重复的点,问最少选多少个点能够满足

解题思路: 差分约束问题,根据题意可以得到每个前缀和建一个点s[bi]-s[ai-1]>=ci,同时要满足前缀和的性质,即:s[i]-s[i-1]>=0,s[i]-s[i-1]<=1,Sbi - Sai >= ci,Si - S(i - 1) >= 0,S(i - 1) - Si >= -1(关于有向边和权值的确定:不管是求最短路(上界,最大值)还是最长路(下界,最小值),先把式子统一化成 X-Y<=C的形式。如果要求的是最大值(最短路),那么就建立<Y,X>的有向边,权值为C;如果要求的是最小值(最长路),那么就建立<X,Y>的有向边,权值为-C)


#include <iostream>    #include <cstdio>    #include <string>    #include <cstring>    #include <algorithm>    #include <cmath>    #include <vector>    #include <map>    #include <set>    #include <queue>    #include <stack>    #include <functional>    #include <climits>    using namespace std;#define LL long long    const int INF = 0x3f3f3f3f;int s[50005],nt[4* 50005],e[4*50005],w[4*50005];int dis[50005], vis[50005], n;void SPFA(int ss){memset(dis, -INF, sizeof dis);dis[ss] = 0;queue<int>q;q.push(ss);while (!q.empty()){int pre = q.front(); q.pop();vis[pre] = 0;for (int i = s[pre]; ~i; i=nt[i]){int ee = e[i];if (dis[ee] < dis[pre] + w[i]){dis[ee] = dis[pre] + w[i];if (!vis[ee]){q.push(ee);vis[ee] = 1;}}}}}int main(){while (~scanf("%d", &n)){int u, v, ww, cnt = 0;memset(s, -1, sizeof s);int mi = INF,ma = -INF;for (int i = 0; i < n; i++){scanf("%d%d%d", &u, &v, &ww);nt[cnt] = s[u], s[u] = cnt, e[cnt] = v+1,w[cnt++]=ww;mi = min(mi, u);ma = max(ma, v + 1);}for (int i = mi; i < ma; i++){nt[cnt] = s[i], s[i] = cnt, e[cnt] = i + 1, w[cnt++] = 0;nt[cnt] = s[i + 1], s[i+1] = cnt, e[cnt] = i, w[cnt++] = -1;}SPFA(mi);printf("%d\n", dis[ma]);}return 0;}

原创粉丝点击