# POJ1201（Hdu1384） Intervals差分约束系统

185人阅读 评论(0)

## 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;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：5892次
• 积分：274
• 等级：
• 排名：千里之外
• 原创：22篇
• 转载：2篇
• 译文：0篇
• 评论：0条
文章分类
阅读排行
评论排行