title : http://poj.org/problem?id=1201
Intervals
Time limit:2000ms Memory limit:65536k
Total submissions:24502 accepted:9317
Description
You are given n closed, integer intervals [AI, bi] and n integers c1, ..., CN.
Write a program:
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 have at least CI common elements with interval [AI, bi], for each i= ,..., N,
Writes the answer to the standard output.
Input
The first line of the input contains an integer n (1 <= n <= 50000), haven 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 & lt;= 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
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
Test Instructions : satisfies the smallest set of Bi-ai selection >=ci;
Ideas :
Title: S[n] Represents the smallest set of top N, which can be:
s[bi]-s[ai-1]>=ci;(题) s[I+1]-s[I]>=0;(本身) s[I+1]-s[I]<=1;-->s[l]-s[I+1]>=-1(本身)
Using the minimum value to build the edge SPFA to find the longest road of Maxn-minn;
Code :
#include <iostream>#include <stdio.h>#include <string.h>#include <queue>using namespace STD;inthead[60005],val[160005];inttot,to[160005],nex[160005];intNintAA,BB,CC; Queue<int>Qintd[60005];intvis[60005];voidSPFA (intx) {memset(d,-1,sizeof(d)); d[x]=0; vis[x]=1; Q.push (x); while(!q.empty ()) {intNow=q.front (); Q.pop (); vis[now]=0; for(inti=head[now];i!=-1; I=nex[i]) {intV=to[i];if(d[v]==-1|| D[v]<d[now]+val[i]) {d[v]=d[now]+val[i];if(!vis[v]) {vis[v]=1; Q.push (v); } } } }}voidAddintXintYintV) {intTMP=HEAD[X]; Head[x]=++tot; nex[tot]=tmp; To[tot]=y; Val[tot]=v; }intMain () {scanf("%d", &n);intminn=10000000;intmaxn=-10000000;memset(head,-1,sizeof(head)); for(intI=1; i<=n;i++) {scanf("%d%d%d", &AA,&BB,&CC); aa--; Add (BB,AA,CC); Minn=min (Aa,minn); Maxn=max (MAXN,BB); } for(inti=minn;i<maxn;i++) {Add (i+1I0); Add (i,i+1,-1); } SPFA (MAXN);printf("%d\n", D[minn]);}
[POJ 1201] Intervals differential constraints