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.
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
/*
差分约束问题。
不知道是什么的可以先看我的博客,转载的。。
此题有50000条边。
一:d[t]-d[s-1]>=key ==> d[s-1]-d[t]<=-key
二:d[i]-d[i-1]<=1;
三:d[i-1]-d[i]<=0;
差分约束问题用最短路来解决
我用的是bellmand-ford算法
枚举每条边,if(d[edge[i].s]-d[edge[i].t]<edge[i].w)
{
d[edge[i].t]=d[edge[i].s]+edge[i].w;
}
现在我们着重想一想初始化的问题,刚开始的时候我们一定要让
d[edge[i].s]-d[edge[i].t]<edge[i].w;==>我是说第一步的时候。
这样才会走出去。
类似于我们做裸的最短路题的时候,起点的距离置0,其他置无穷大。
哦,对了。这题我们读入边的时候同时把最大的点和最小的点存下来
因为等下就是求这两点的最短路额。。
说回刚才的问题,要第一步可走,edge[i].w都非正。
我们让d[mi-1]=inf;
则如果d[edge[i].s]-d[edge[i].t]>edge[i].w
就让d[edge[i].s]=d[edge[i].t]+edge[i].w;显然第一步能走出去吧。。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 50008
#define inf 0x3f3f3f3f
int d[maxn],n,mi,ma;
inline int max(int a,int b)
{
return a>b?a:b;
}
inline int min(int a,int b)
{
return a>b?b:a;
}
struct Edge
{
int s,t,w;
}edge[maxn];
void bellmand()
{
bool flag=true;
while(flag)
{
flag=false;
for(int i=1;i<=n;i++)
{
if(d[edge[i].s]-d[edge[i].t]>edge[i].w)
{
d[edge[i].s]=d[edge[i].t]+edge[i].w;
flag=true;
}
}
for(int i=mi;i<=ma;i++)
{
if(d[i]>d[i-1]+1)
{
d[i]=d[i-1]+1;
flag=true;
}
}
for(int i=ma;i>=mi;i--)
{
if(d[i-1]>d[i])
{
d[i-1]=d[i];
flag=true;
}
}
}
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
int u,v,w;
mi=inf,ma=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&u,&v,&w);
edge[i].s=u;
edge[i].t=v;
edge[i].w=-w;
edge[i].s--;
mi=min(mi,u);
ma=max(ma,v);
}
memset(d,0,sizeof(d));
d[mi-1]=inf;
bellmand();
printf("%d\n",d[ma]-d[mi-1]);
}
return 0;
}