POJ1201（Hdu1384） Intervals差分约束系统

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 endpoints 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 <= 50 000) - 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 <= 50 000 and 1 <= ci <= bi - ai + 1.

Process to the end of file.

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.

5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1

Sample Output

6

dis[i+1]-dis[i]>=0；
dis[i]-dis[i+1]>=-1；

代码如下：

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define INF 1<<29
using namespace std;
struct node
{
int to,len,next;
}edge[200050];
{
edge[k].to=en;
edge[k].len=len;
}
void init()
{
memset(used,0,sizeof(used));
memset(inqueue,0,sizeof(inqueue));
minx=INF,maxx=-1;
for(int i=1;i<=n;i++)
{
int st,en,len;
scanf("%d%d%d",&st,&en,&len);
minx=min(minx,st);
maxx=max(maxx,en+1);
}
for(int i=minx;i<=maxx;i++)
{
}
}
void spfa(int st,int time)
{
for(int i=minx;i<=maxx;i++)
dis[i]=-INF;
queue<int>q;
dis[st]=0;
used[st]=1;
q.push(st);
inqueue[st]++;
while(!q.empty())
{
int top=q.front();
q.pop();
used[top]=0;
{
if(dis[edge[j].to]<dis[top]+edge[j].len)
{
dis[edge[j].to]=dis[top]+edge[j].len;
if(!used[edge[j].to])
{
used[edge[j].to]=1;
if(++inqueue[edge[j].to]>time-1)
{
printf("%d\n",dis[maxx]);
}
q.push(edge[j].to);
}
}
}
}
printf("%d\n",dis[maxx]);
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
k=0;
init();
spfa(minx,maxx-minx+1);
}
return 0;
}


03-18 283

01-28 524

07-14 173

04-16 393

05-22 1307

05-11 505

04-10 382

05-17 5038

05-23 132

01-07 501