题意

在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点。

思路

差分约束求最小值。把不等式都转换为>=形式,那么显然有xvi >= xui-1 + ci,那么就在约束图中连一条(ui-1, vi, ci)的边;当然不要忘记隐含的不等式:xi >= xi-1 + 0;   xi-1 >= xi -1.

建完图后SPFA求最长路径即可

代码

[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <cstring>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
#define REP(i, begin, m)   for (int i = begin; i < begin+m; i ++)
using namespace std;

const int MAXN = 50005;
const int oo = 0x3fffffff;
struct Edge{
    int to;
    int w;
    Edge(){}
    Edge(int _to, int _w){  to = _to;   w = _w; }
};
struct Gragh{
    vector <Edge> adj[MAXN];
    queue <int> q;
    int vn;
    int dist[MAXN], inq_num[MAXN];
    bool inq[MAXN];
    void init(int n){
        vn = n;
        for (int i = 0; i <= n; i ++)
            adj[i].clear();
    }
    //if xj >= xi + c, add (i, j, c)
    void add_edge(int u, int v, int w){
        adj[u].push_back(Edge(v, w));
    }
    //spfa calculate longest path
    bool solve(int s, int t){
        while(!q.empty())
            q.pop();
        MEM(inq, false);    MEM(inq_num, 0);    MEM(dist, -1);      //Note : dist shouldn't initially be 0
        dist[s] = 0;    inq[s] = true;  inq_num[s] ++;  q.push(s);
        while(!q.empty()){
            int u = q.front();
            q.pop();
            inq[u] = false;
            for (int i = 0; i < adj[u].size(); i ++){
                int v = adj[u][i].to;
                if (dist[v] < dist[u] + adj[u][i].w){
                    dist[v] = dist[u] + adj[u][i].w;
                    if (!inq[v]){
                        inq[v] = true;
                        inq_num[v] ++;
                        if (inq_num[v] > vn)
                            return false;
                        q.push(v);
                    }
                }
            }
        }
        if (dist[t] < oo){
            return true;
        }
    }
}spfa;
struct intervals{
    int u, v, w;
}inte[MAXN];
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
    int n;
    while(scanf("%d", &n) != EOF){
        int maxn = 0;
        REP(i, 1, n){
            scanf("%d %d %d", &inte[i].u, &inte[i].v, &inte[i].w);
            inte[i].u ++, inte[i].v ++;
            maxn = max(maxn, inte[i].v);
        }
        spfa.init(maxn+1);
        REP(i, 1, maxn){
            spfa.add_edge(i-1, i, 0);
            spfa.add_edge(i, i-1, -1);
        }
        REP(i, 1, n){
            spfa.add_edge(inte[i].u-1, inte[i].v, inte[i].w);
        }
        if (spfa.solve(0, maxn)){
            printf("%d\n", spfa.dist[maxn]);
        }
    }
return 0;
}
[/cpp]

POJ 1201 Intervals (差分约束系统)的更多相关文章

  1. poj 1201 Intervals(差分约束)

    题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...

  2. poj 1201 Intervals——差分约束裸题

    题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...

  3. PKU 1201 Intervals(差分约束系统+Spfa)

    题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: ...

  4. poj 1201 Intervals(差分约束)

    做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...

  5. POJ 1201 Intervals(图论-差分约束)

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20779   Accepted: 7863 Descri ...

  6. POJ 1201 Intervals(差分约束 区间约束模版)

    关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至 ...

  7. POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束

    POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...

  8. poj 1201 Intervals 解题报告

    Intervals Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Submit Statu ...

  9. Intervals(差分约束系统)

    http://poj.org/problem?id=1201 题意:给定n个整数闭区间[a,b]和n个整数c,求一个最小的整数集合Z,满足Z里边的数中范围在闭区间[a,b]的个数不小于c个. 思路:根 ...

随机推荐

  1. 常见的NoSql系统使用场景分析--转载

    •Cassandra •特性:分布式与复制的权衡\根据列和键范围进行查询\BigTable类似的功能:列,列族\写比读快很多 •最佳适用:写操作较多,读比较少的时候.如果你的系统都是基于Java的时候 ...

  2. [C++]iostream的几种输入形式

    做ACM题的时候,发现cin并不能满足所有输入要求.比如说: 每行给出一款运动鞋信息,若该款写不打折,则先后给出每种运动鞋单价P,所购买的数量Q:若打折,则先后给出每种运动鞋单价P,所购买的数量Q,折 ...

  3. Codeforces Round #258 (Div. 2)(A,B,C,D)

    题目链接 A. Game With Sticks time limit per test:1 secondmemory limit per test:256 megabytesinput:standa ...

  4. db2日期和时间常用汇总

    1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2' FROM SYSIBM ...

  5. [转载] Linux poll机制

    原地址:http://hongwazi.blog.163.com/blog/#m=0&t=3&c=poll poll的是一种查询的方式,英文解释 :民意调查 函数原型:int poll ...

  6. Codeforces Round #336 (Div. 2) A. Saitama Destroys Hotel 模拟

    A. Saitama Destroys Hotel   Saitama accidentally destroyed a hotel again. To repay the hotel company ...

  7. 配置sshd_config中的PermitRootLogin设置root登录或者禁止root登录

    在etc的sshd_config文件中,默认有PermitRootLogin no的配置,这个的意思是禁止root用户登录,如果想要允许root登录,需要su root用户到sshd_config下进 ...

  8. Unable to open debugger port : java.net.ConnectException “Connection refused”

    http://stackoverflow.com/questions/28283087/unable-to-open-debugger-port-java-net-connectexception-c ...

  9. lintcode:Ugly Number I

    Ugly Number Write a program to check whether a given number is an ugly number`. Ugly numbers are pos ...

  10. ios开发--集成银联3.3.0

    项目最近需要集成银联,在网上搜了一下发现都并不是最新版的银联集成教程,自己摸索了一下,总结写了下来. 附上3.3.0的下载网址 https://open.unionpay.com/upload/dow ...