题意

在区间[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. 【leetcode】Contains Duplicate & Rectangle Area(easy)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  2. POJ 2013

    #include <iostream> #include <string> #define MAXN 20 using namespace std; string _m[MAX ...

  3. Install wxWidgets-3.0.2 on GNU/Linux Debian

    转载自 http://www.binarytides.com/install-wxwidgets-ubuntu/ wxWidgets wxWidgets is an application devel ...

  4. 做一款仿映客的直播App

    投稿文章,作者:JIAAIR(GitHub) 一.直播现状简介 1.技术实现层面 技术相对都比较成熟,设备也都支持硬编码.iOS还提供现成的Video ToolBox框架,可以对摄像头和流媒体数据结构 ...

  5. Android 广播(内部类)

    1.广播定义在一个单独的文件中 源码: public class MessageReceiver extends BroadcastReceiver{ @Override public void on ...

  6. dwz ie10一直提示数据加载中

    dwz js资源jquery.validate.js 搜索 this.attr('novalidate', 'novalidate'); 在33行左右 用if (typeof (Worker) !== ...

  7. linux poll

    man poll: NAME poll, ppoll - wait for some event on a file descriptor SYNOPSIS #include <poll.h&g ...

  8. 16_采用SharedPreferences保存用户偏好设置参数

    按钮事件 <Button android:id="@+id/button" android:layout_width="wrap_content" and ...

  9. cmake的使用二:链接第三方静态库

    cmake的使用二:链接第三方静态库

  10. Java WEB安全问题及解决方案

    1.弱口令漏洞   解决方案:最好使用至少6位的数字.字母及特殊字符组合作为密码.数据库不要存储明文密码,应存储MD5加密后的密文,由于目前普通的MD5加密已经可以被破解,最好可以多重MD5加密.   ...