poj 1201 Intervals

本文介绍了一道典型的差分约束系统问题,并详细解释了解题思路与算法实现过程。通过实例演示如何构建图模型并运用SPFA算法求解最小路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Southwestern Europe 2002

分析:
差分约束系统模版题。
d[i]表示左边界到i的个数。
设读入x,y,w。
关系为y-x>=w,也就是说x-y<=-w,x到y有一条-w的边。
然后我们知道,
0<=d[i]-d[i-1]<=1(自己脑补)
也就是
d[i]-d[i-1]<=1,d[i-1]-d[i]<=0
就是说i和i-1有一条1的边,i-1和i有一条0的边,跑最短路。(这题本来为最长路,强行写成最短路的话要输出相反数)。

代码:

const
  MaxE=500001;
  MaxV=150001;

type
  rec=record
   x,y,w,next:longint;
  end;
var
  n,m,c,qx,i,x,y,w,o,q,maxx,maxy:longint;
  g:array [0..Maxv] of rec;
  ls:array [0..Maxe] of longint;
  v,d,list,sum:array [0..maxe] of longint;

procedure spfa(first:longint);
var
  head,tail,t,i,qe:longint;
begin
  tail:=1;
  list[1]:=first;
  for i:=maxx-1 to maxy do
  d[i]:=maxlongint;
  d[first]:=0;
  v[first]:=1;
  while tail<>0 do
    begin
      t:=ls[list[tail]];
      qe:=list[tail];
      tail:=tail-1;
      while t>0 do
        with g[t] do
          begin
            if d[x]+w<d[y] then
              begin
                d[y]:=d[x]+w;
                if v[y]=0 then
                  begin
                    v[y]:=1;
                    tail:=tail+1;
                    list[tail]:=y;
                    inc(sum[y]);
                    if (sum[y]>=n) then
                     begin
                      writeln('No');
                      halt;
                     end;
                  end;
              end;
            t:=next;
          end;
      v[qe]:=0;
    end;
end;

procedure add(x,y,w:longint);
 begin
  inc(o);
  g[o].x:=x;
  g[o].y:=y;
  g[o].w:=w;
  g[o].next:=ls[x];
  ls[x]:=o;
 end;

begin
   read(n);
   maxx:=maxlongint;
  for i:=1 to n do
   begin
    readln(x,y,w);
    add(x-1,y,-w);
    if y>maxy then maxy:=y;
    if x<maxx then maxx:=x;
   end;
  for i:=maxx to maxy do
   begin
    add(i,i-1,1);
    add(i-1,i,0);
   end;
  spfa(maxx-1);
  writeln(-d[maxy]);
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值