hdu acm 1384 Intervals

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3576    Accepted Submission(s): 1308


Problem 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.
 

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

Sample Output
  
  
6
 

尼玛,差分约束问题。。。刚学习。

 题意:求符合题意的最小集合的元素个数  
题目要求的是求的最短路,  
则对于 不等式  f(b)-f(a)>=c,建立 一条 b 到 a 的边 权值为 c,则求的最长路 即为 最小值(集合)  
并且有隐含条件:0<=f(a)-f(a-1)<=1  则有边权关系(a,a-1,0)以及(a-1,a,-1);  
将源点到各点的距离初始化为INF(无穷大),其中之1为0,最终求出的最短路满足 它们与该点之间相互差值最大  
差分约束  
 在实际的应用中,一般使用SPFA(Shortest Path Fast Algorithm)算法来实现。  
  差分约束系统中源点到每个点的距离确定  
  关于Dist[]的初始化化  
  1.如果将源点到各点的距离初始化为0,最终求出的最短路满足 它们之间相互最接近了  
  2.如果将源点到各点的距离初始化为INF(无穷大),其中之1为0,最终求出的最短路满足 它们与该点之间相互差值最大。  
  3.差分约束系统的确立要根据自己确定的约束条件,从约束点走向被约束点  
  连边一般有两种方法,第一种是连边后求最长路的方法,第二种是连边后求最短路的方法。  
  例:d[x]-d[y]>=Z  
  如果想连边后求最长路 那么将不等式变形为这种形式 d[x]>=d[y]+z y---x连一条权值为z的边  
  求最短路则变形成d[y]<=d[x]-z x---y连一条权值为-z的边。  
  如果是别的不等式,也可以根据情况变形。但是要保证的是 两个变量(x,y)的系数一定要是正的。而常量则不一定。  
第一:  
感觉难点在于建图  
第二:  
①:对于差分不等式,a - b <= c ,建一条 b 到 a 的权值为 c 的边,求的是最短路,得到的是最大值  
②:对于不等式 a - b >= c ,建一条 b 到 a 的权值为 c 的边,求的是最长路,得到的是最小值  
③:存在负环的话是无解  
④:求不出最短路(dist[ ]没有得到更新)的话是任意解  
第三:  
一种建图方法:  
设x[i]是第i位置(或时刻)的值(跟所求值的属性一样),那么把x[i]看成数列,前n项和为s[n],则x[i] = s[i] - s[i-1];  

那么这样就可以最起码建立起类似这样的一个关系:0 <= s[i] - s[i-1] <= 1; 

为什么不能用队列,还要手动模拟。。。。

#include<string.h>
#define MS(a,b) memset(a,b,sizeof(a))
#define INF 0x7fffffff
#include<stdio.h>
int f,vis[50010],d[50010],head[50010],q[5000000];
struct node
{
    int e,next,w;
}edge[1000000];
void add(int s,int e,int w)
{
    edge[f].e=e;
    edge[f].w=w;
    edge[f].next=head[s];
    head[s]=f++;
}
void spfa(int s)
{
    int i,k,ss,ee;
    d[s]=0;
    vis[s]=1;
    MS(vis,0);
    ss=ee=0;
    q[ss++]=s;
 while(ss>ee)
 {
   int x=q[ee++];
   vis[x]=0;
   for(i=head[x];i!=-1;i=edge[i].next)
   {
       k=edge[i].e;
       if(d[k]<d[x]+edge[i].w)
       {
          d[k]=d[x]+edge[i].w;
          if(!vis[k])
          {
           vis[k]=1;
           q[ss++]=k;
          }
       }
   }
 }
 return ;
}
int main()
{
    int a,b,n,u,v,w,i,s;
    while(~scanf("%d",&n))
    {
      MS(head,-1);
     f=1;
     a=INF;b=-INF;
    for(i=0;i<50005;i++)
        d[i]=-INF;
   for(i=0;i<n;i++)
   {
       scanf("%d %d %d",&u,&v,&w);
       u++,v++;
       add(u-1,v,w);
       if(a>u-1)
        a=u-1;
       if(b<v)
        b=v;
   }
   for(i=a;i<=b;i++)
   {
       add(i-1,i,0);
       add(i,i-1,-1);
   }
   s=a;
   spfa(s);
   printf("%d\n",d[b]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值