POJ 1201 Intervals【差分约束】
传送门:http://poj.org/problem?id=1201
题意:
有n个如下形式的条件:,表示在区间[
, 
]内至少要选择
个整数点.问你满足以上所有条件,最少需要选多少个点?
思路:第一道差分约束题,有关差分约束知识详见https://blog.csdn.net/whereisherofrom/article/details/78922648(个人感觉这个博主写的挺好的)
令表示从区间[0,x]中选择的整数点个数,则有
=c_i\rightarrow s_{b_i+1}-s_{a_i}>=c_i" class="mathcode" src="https://private.codecogs.com/gif.latex?s_%7Bb_i%7D-s_%7Ba_i-1%7D%3E%3Dc_i%5Crightarrow%20s_%7Bb_i+1%7D-s_%7Ba_i%7D%3E%3Dc_i">(因为
=0" class="mathcode" src="https://private.codecogs.com/gif.latex?a_i%3E%3D0">,会出现数组下标为-1的情况),如果只靠这个式子是无法得出答案的,题中还有隐藏的约束
即是
=0" class="mathcode" src="https://private.codecogs.com/gif.latex?s_%7Bi+1%7D-s_i%3E%3D0">和
=-1" class="mathcode" src="https://private.codecogs.com/gif.latex?s_i-s_%7Bi+1%7D%3E%3D-1">,题中要求最小值,所以用spfa跑一遍最长路即可。
代码:
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn = 50005;
const int INF = 0x3f3f3f3f;
int tot;
struct node
{
    int to;
    int next;
    int w;
};
node edges[maxn * 3];
int head[maxn];
int d[maxn];
bool vis[maxn];
void add_edges(int u, int v, int w)
{
    edges[++tot].to = v;
    edges[tot].w = w;
    edges[tot].next = head[u];
    head[u] = tot;
}
int l = 50005;
int r = -1;
void spfa()
{
    for(int i = l; i <= r; i++)
        vis[i] = false, d[i] = -INF;
    d[l] = 0;
    queue<int>q;
    q.push(l);
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        vis[x] = false;
        for(int i = head[x]; i; i = edges[i].next)
        {
            node v = edges[i];
            if(d[v.to] < d[x] + v.w)
            {
                d[v.to] = d[x] + v.w;
                if(!vis[v.to])
                {
                    vis[v.to] = true;
                    q.push(v.to);
                }
            }
        }
    }
}
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add_edges(a, b + 1, c);
        r = max(r, b + 1);	  //求区间右端点
        l = min(l, a);        //求区间左端点
    }
    for(int i = l; i < r; i++)
    {
        add_edges(i + 1, i, -1);
        add_edges(i, i + 1, 0);
    }
    spfa();
    cout << d[r] << endl;
}												
											POJ 1201 Intervals【差分约束】的更多相关文章
- poj 1201 Intervals(差分约束)
		
题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...
 - poj 1201 Intervals——差分约束裸题
		
题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...
 - POJ 1201 Intervals (差分约束系统)
		
题意 在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点. 思路 差分约束求最小值.把不等式都转换为&g ...
 - POJ 2101 Intervals 差分约束
		
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27746 Accepted: 10687 Description You ...
 - poj 1201 Intervals(差分约束)
		
做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...
 - POJ 1201 Intervals (经典) (差分约束)
		
<题目链接> 题目大意:给你$n$段区间,$a_i,b_i,c_i$ 表示在 $[a_i,b_i]$ 区间内至少要选择$c_i$个点.现在问你在满足这n个条件的情况下,最少要选多少个点? ...
 - 【题解】 POJ 1201 Intervals(差分约束)
		
懒得复制,戳我戳我 Solution: 这道题就是一个板子题 抽象成第\(a\)至第\(b\)间选择数的个数为\(c\),我们就可以用前缀和来表示,这样就可以得到不等式\(s[b]-s[a-1]> ...
 - POJ 1201 Intervals(差分约束 区间约束模版)
		
关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至 ...
 - poj 1201 Intervals【差分约束+spfa】
		
设s为前缀和,首先显然的条件是\[ s_{bi}-s_{ai-1}>=c \],然后隐含的是\[ s_i-s_{i-1}>=0 s_i-s_{i-1}<=1 \] 然后根据差分约束, ...
 
随机推荐
- uni-app实现弹窗遮罩
			
<template> <view> <view class="systemboxItem" @click="showSystemDialog ...
 - TextBoxFor()扩展方法
			
1.扩展方法 public static class TextBoxForHelper { public static MvcHtmlString CreateTextBoxFor<TModel ...
 - javacv 通过rtsp 获取视频流 设置帧率
			
原文章:https://blog.csdn.net/eguid_1/article/details/52680802 原代码: /** * 按帧录制视频 * * @param inputFile-该地 ...
 - Python练习题3
			
1.九九乘法表 li = [1,2,3,4,5,6,7,8,9] for i in li: for j in li: if i >= j: print(i,'*',j,'=',i*j,end=& ...
 - crmv2项目
			
maven -----------------------------------------------------------------------------感谢打赏!
 - Java自学-集合框架 Comparator和Comparable
			
Java Comparator和Comparable 步骤 1 : Comparator 假设Hero有三个属性 name,hp,damage 一个集合中放存放10个Hero,通过Collection ...
 - 093-PHP数组比较
			
<?php $arra=array('hello','hi'); //定义多个数组 $arrb=array(1=>'hi',0=>'hello'); $arrc=array(1,2) ...
 - Power BI Premium
			
Power BI Premium的设计是为了满足拥有大量数据的大公司的需求.微软已经重新构建了Power BI的架构,以允许大量的“只读”用户.Premium用户还可以利用很多新功能. Power B ...
 - idea新建maven web项目
			
一.新建maven web项目 二.配置服务器 三.测试运行
 - java如何连接Oracle数据库问题
			
Oracle数据库纯属自学,不对请留言改正! 在学Oracle前相信已经大致知道mysql或sqlserver数据库,这个跟前面两个不大一样,你安装的时候让你输入一个密码,貌似是一个系统管理员密码,跟 ...