(POJ1201)Intervals <差分约束系统-区间约束>

2023-05-16

Intervals
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

关于差分约束系统的详细解释与入门,还有各个最短路算法的详解与优化大家可以看看这篇博客。写的非常的好!!!

夜深人静写算法:
http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html

题意:
给定n(n <= 50000)个整点闭区间和这个区间中至少有多少整点需要被选中,每个区间的范围为[ai, bi],并且至少有ci个点需要被选中,其中0 <= ai <= bi <= 50000,问[0, 50000]至少需要有多少点被选中。

分析:
这是一个典型的差分约束系统的题目。

例如3 6 2 表示[3, 6]这个区间至少需要选择2个点,可以是3,4也可以是4,6(总情况有 C(4, 2)种 )。

这类问题就没有线性约束那么明显,需要将问题进行一下转化,考虑到最后需要求的是一个完整区间内至少有多少点被选中,试着用d[i]表示[0, i]这个区间至少有多少点能被选中,根据定义,可以抽象出 d[-1] = 0,对于每个区间描述,可以表示成d[ bi ] - d[ ai - 1 ] >= ci,而我们的目标要求的是 d[ 50000 ] - d[ -1 ] >= T 这个不等式中的T,将所有区间描述转化成图后求-1到50000的最长路。
这里忽略了一些要素,因为d[i]描述了一个求和函数,所以对于d[i]和d[i-1]其实是有自身限制的,考虑到每个点有选和不选两种状态,所以d[i]和d[i-1]需要满足以下不等式: 0 <= d[i] - d[i-1] <= 1 (即第i个数选还是不选)
这样一来,还需要加入 50000*2 = 100000 条边,由于边数和点数都是万级别的,所以不能采用单纯的Bellman-Ford ,需要利用SPFA进行优化,由于-1不能映射到小标,所以可以将所有点都向x轴正方向偏移1个单位(即所有数+1)。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

const int INF = 0xffffff;
struct Edge
{
    int u,v,w,next;
    Edge(){}
    Edge(int u_,int v_,int w_)
    {
        u = u_;
        v = v_;
        w = w_;
    }
}edges[200000];

int head[50010];
int d[50010];
int e = 0,maxnum = 0;
void addedge(int u,int v,int w)
{
    edges[e] = Edge(u,v,w);
    edges[e].next = head[u];
    head[u] = e++;
}

int inq[50010];
void spfa()
{
    queue<int> q;
    while(!q.empty()) q.pop();
    for(int i=0;i<=maxnum;i++) d[i] = -INF;
    d[0] = 0;
    memset(inq,0,sizeof(inq));
    q.push(0);
    inq[0] = 1;
    while(!q.empty())
    {
        int u = q.front(); q.pop();
        inq[u]--;
        for(int i=head[u];i!=-1;i=edges[i].next)
        {
            int v = edges[i].v;
            if(d[v] < d[u] + edges[i].w)
            {
                d[v] = d[u] + edges[i].w;
                if(!inq[v])
                {
                    inq[v]++;
                    q.push(v);
                }
            }
        }
    }
}

int main()
{
    int n,u,v,w;
    scanf("%d",&n);
    memset(head,-1,sizeof(head));
    maxnum = 0;
    e = 0;
    while(n--)
    {
        scanf("%d%d%d",&u,&v,&w);
        if(v+1 > maxnum) maxnum = v+1;//整体+1了,所以是v+1
        addedge(u,v+1,w); //整体+1
    }

    //0 <= d[i] - d[i-1] <= 1(即第i个数选还是不选)
    for(int i=0;i<=maxnum;i++)
    {
        addedge(i,i+1,0);
        addedge(i+1,i,-1);
    }
    spfa();
    printf("%d\n",d[maxnum]);
    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

(POJ1201)Intervals <差分约束系统-区间约束> 的相关文章

  • miui 安装app闪退问题

    android版本 xff1a 7 0 MIUI版本 xff1a 8 2 手机 xff1a 小米5 之前老版本可以运行 xff0c 今天用AS的run xff0c 在安装apk时报application installation faile
  • 在x64上构建智能家居(home assistant) (一) Supervised版本安装

    我的上一篇文章 在嵌入式x86上构建我的智能家居 home assistant 中本来希望在一个低功耗的x86嵌入式上安装home assistant xff0c 但是因为一些限制没有成功 找到一个低功耗的笔记本 xff08 东芝的dyna
  • 安装YMFE/yapi API管理服务器(Ubuntu20)

    GitHub YMFE yapi YApi 是一个可本地部署的 打通前后端及QA的 可视化的接口管理平台 YApi 是一个可本地部署的 打通前后端及QA的 可视化的接口管理平台 Contribute to YMFE yapi develop
  • 安装nodejs18 + yapi(Debian11)

    安装nodejs Node js Node js is a JavaScript runtime built on Chrome 39 s V8 JavaScript engine https nodejs org zh cn 官方手顺 通
  • Postgresql count 慢的处理方法

    performance Postgresql extremely slow count with index simple query Database Administrators Stack Exchange https dba sta
  • 解决Referenced file contains errors(struts-2.0.dtd)

    解决方法 两种 1 这个可能是你的DTD文件找不到 或者解析有错 才发生的错误 你可以在地址栏里输入http struts apache org dtds struts 2 0 dtd 这个看能查看不 如果不能 应该是网络的问题或XML解析
  • 使用POI向Excel中插入多张图片

    最近在大量使用poi对Excel进行操作 xff0c 可以说是越用越气愤 xff0c 很多功能支持得不完善 xff0c 一个在VB里很简单的操作 xff0c 你用poi实现可能就要多几倍甚至是数10倍的代码 但是我们搞JAVA的总不能丢掉J
  • 将“存储卡”改名为Storage Card的方法

    HKEY LOCAL MACHINE System StorageManager Profiles SDMemory 34 Name 34 61 34 SD Memory Card 34 34 Folder 34 61 34 Storage
  • ubuntu 驱动更新后导致无法进入界面

    问题描述 xff1a 安装新ubuntu系统后未禁止驱动更新导致无法进入登录界面 解决办法 xff1a 首先在进入BIOS中 xff0c 修改设置以进行命令行操作 xff0c 然后卸载已有的系统驱动 xff0c 最后安装新的驱动即可 开机按
  • PPC WM6.1智能手机上使用日语辞典浅谈

    在PPC手机上用日语辞典 xff08 広辞苑 xff0c 三省堂等 xff09 http bulo hjenglish com group topic 144804 PPC上的日文输入法 http bulo hjenglish com gr
  • PPC音量太小和听筒音太小的解决方法

    1下载注册表修改器 2复制修改器到PPC xff08 最好是卡上啦 xff09 3在PPC上运行修改器 我用的是华硕P525 以下是我小P的设置 xff1a 找到HKEY CURRENT USER ControlPanel Phone 项下
  • Delphi中的集成VBS脚本语言应用

    罗焱 从薇 王正浩 摘 要 xff1a 使用ActiveX Scripting技术 xff0c 可以在应用程序中集成使用脚本语言 本文介绍如何应用这一技术在Delphi应用程序中添加VBScript支持 关键词 xff1a ActiveX脚
  • python中嵌入C语言脚本

    借助Cinpy 和C语言解释器TinyCC xff0c 可以在python 程序里面直接嵌入C语言片断 不经编译直接使用C编写的函数了 win2k平台上 xff0c 简单的测试对比数据如下 xff08 递归方法计算第四十项兔子数列fib x
  • 最新Ubuntu内网源部署方法(Ubuntu20、Ubuntu21)

    最新Ubuntu内网源部署方法 1 下载公网离线源 安装apt mirror xff0c 并修改 etc apt mirror list内容 xff0c 以ubuntu20 04 为例说明 xff1a span class token co
  • wsl2-ubuntu安装图形界面;windows安装miniconda

    一 wsl2 ubuntu安装图形界面 1 安装 xff1a wsl2安装ubuntu Download VcXsrv Windows X Server from SourceForge net 2 配置 xff08 链接wsl和VcXsr
  • 双曲函数系列

    定义 双曲函数 xff08 Hyperbolic Function xff09 包括下列六种函数 xff1a sinh 双曲正弦 xff1a sinh x 61 e x e x 2 cosh 双曲余弦 xff1a cosh x 61 e x
  • 虚拟机增加磁盘空间(VMware虚拟机)

    1 写在前面 对于VMware虚拟机 xff0c 经常有最初分配的磁盘空间大小最后不够用的情况 xff0c 因此需要我们增加磁盘空间 网上看了一些博客资料 xff0c 大多不能完全照着做完 xff0c 参照了几个才实现 xff0c 2 操作
  • openSUSE通过snapper恢复系统

    事由 xff1a 系统中存在两个版本python xff0c 导致程序无法找到pygtk xff0c 遂打算删除所有python重新安装 xff0c KDE 崩溃只能用控制台 过程 xff1a 1 查找控制台安装kde的方法 xff0c 未
  • Scrapy 中 ImagesPipeline 无法执行,不起作用,图片无法下载的原因!

    经过检查scrapy样例程序的执行情况 xff0c 发现如下警告信息 xff1a 2021 04 11 22 33 59 scrapy middleware WARNING Disabled PianImgPipeline ImagesPi
  • 移植jpeg库文件到ARM开发板

    我们知道 xff0c jpeg格式的图片是经过压缩处理的 xff0c 所以想要在ARM开发板中显示 xff0c 就需要一些库文件的支持 xff0c 当然牛逼的人也可以自己写解压算法做库文件 xff0c 不过作为小白的我 xff0c 还是先借

随机推荐