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
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
题意: ai bi ci 代表序列在[ai,bi]之间最少ci个整数,问你这个序列最少有多少个整数
题解:由差分约束系统,首先可以得到的是 dis[b+1] - dis[a] >=c[i]
然后 这样还不够建图 对于每一个点 还有以下的不等式
dis[b+1]-dis[b] >=0
dis[b]-dis[b+1]>=-1
这样就可以建图了
题目的意思就可以转化为 从minm 到maxm最少有多少个整数了
#include <bits/stdc++.h>
#define maxn 500000+5
#define INF 0x3f3f3f3f
using namespace std;
int n,dis[maxn],head[maxn],len;
int vis[maxn],maxm=-INF,minm=INF;
struct edge{
int to,val,next;
}e[maxn];
void add(int from ,int to ,int val){
e[len].to=to;
e[len].val=val;
e[len].next=head[from];
head[from]=len++;
}
void spfa(int s){
memset(vis,0,sizeof(vis));
for(int i=minm;i<=maxm;i++){
dis[i]=-INF;
}
queue<int>q;
q.push(s);
vis[s]=1;
dis[s]=0;
while(!q.empty()){
// cout<<233;
int cur =q.front();q.pop();
vis[cur]=0;
for(int i=head[cur];i!=-1;i=e[i].next){
int id=e[i].to;
if(dis[id]<dis[cur]+e[i].val){
dis[id]=dis[cur]+e[i].val;
if(!vis[id]){
vis[id]=1;
q.push(id);
}
}
}
}
}
int main(){
while(scanf("%d",&n)!=EOF){
len=0;
memset(head,-1,sizeof(head));
for(int i=0;i<n;i++){
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
minm=min(u,minm);
maxm=max(v+1,maxm);
add(u,v+1,c);
}
for(int i=minm;i<maxm;i++){
add(i,i+1,0);
add(i+1,i,-1);
}
// cout<<233;
spfa(minm);
printf("%d\n",dis[maxm]);
}
return 0;
}
(poj崩了。。)