这题$n$倍经验……

考虑差分约束:

我们设$s_i$表示$[-1, i]$这个区间中数字的种类数,那么一个条件的限制相当于$s_{b_i} - s_{a_i - 1} \leq c_i$,那么连边$(a_i - 1, b_i, c_i)$。

再挖掘一些隐含条件:$0 \leq s_i - s_{i - 1} \leq 1$,那么再连边$(i - 1, i, 0)$和$(i, i - 1, -1)$。

然后从$s_{-1}$开始跑最长路即可,因为题目中保证了$c_i \leq b_i - a_i + 1$,所以不会有正环,也就是说最后的$dis_{50000}$就是答案。

时间复杂度$O(spfa ???)$。

感觉这些最长路的题目反正要把边权倒过来,不如直接用最短路建模求解。

Code:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std; const int N = ;
const int M = 2e5 + ;
const int Maxn = ; int testCase, n, tot, head[N], dis[N];
bool vis[N]; struct Edge {
int to, nxt, val;
} e[M]; inline void add(int from, int to, int val) {
e[++tot].to = to;
e[tot].val =val;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} queue <int> Q;
void spfa(int st) {
memset(vis, , sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
vis[st] = , dis[st] = ;
Q.push(st);
for(; !Q.empty(); ) {
int x = Q.front(); Q.pop();
vis[x] = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(dis[y] > dis[x] + e[i].val) {
dis[y] = dis[x] + e[i].val;
if(!vis[y]) {
vis[y] = ;
Q.push(y);
}
}
}
}
} int main() {
for(read(testCase); testCase--; ) {
read(n);
tot = ; memset(head, , sizeof(head));
for(int x, y, v, i = ; i <= n; i++) {
read(x), read(y), read(v);
x += , y += ;
add(x, y, -v);
}
for(int i = ; i < Maxn; i++) add(i, i + , );
for(int i = Maxn; i > ; i--) add(i, i - , ); spfa(); printf("%d\n", -dis[Maxn]);
if(testCase) printf("\n");
}
return ;
}

UVA1723 Intervals的更多相关文章

  1. [LeetCode] Non-overlapping Intervals 非重叠区间

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

  2. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  3. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  4. POJ1201 Intervals[差分约束系统]

    Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26028   Accepted: 9952 Descri ...

  5. Understanding Binomial Confidence Intervals 二项分布的置信区间

    Source: Sigma Zone, by Philip Mayfield The Binomial Distribution is commonly used in statistics in a ...

  6. Leetcode Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  7. LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。

    感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * ...

  8. Merge Intervals 运行比较快

    class Solution { public: static bool cmp(Interval &a,Interval &b) { return a.start<b.star ...

  9. [LeetCode] 435 Non-overlapping Intervals

    Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...

随机推荐

  1. java学习笔记 --- IO(3)

    1.FileReader:读取字符流,默认GBK public class CharStreamDemo { public static void main(String[] args) throws ...

  2. 服务器端使用DeferredResult异步推送技术

    注意1: org.springframework.web.context.request.async.DeferredResult; 是Spring4.0以后的版本才有的,注意Spring版本,然后添 ...

  3. 【pandas】pandas.DataFrame.rename()---重置索引名称

    官方文档 github地址 例子: 创建DataFrame ### 导入模块 import numpy as np import pandas as pd import matplotlib.pypl ...

  4. bzoj 5016 一个简单的询问

    THUWC 考了莫队(这个应该可以说吧) 然而不会莫队,签到失败,所以找到了一道长得差不多的题写一写 为什么这么长时间都没有发现这道题(半恼 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问 ...

  5. BZOJ - 2142 礼物 (扩展Lucas定理)

    扩展Lucas定理模板题(貌似这玩意也只能出模板题了吧~~本菜鸡见识鄙薄,有待指正) 原理: https://blog.csdn.net/hqddm1253679098/article/details ...

  6. hawq创建filespace,tablespace,database,table

    使用HAWQ   在HAWQ的使用上跟Greenplum基本就一样一样的了.比如:   1. 创建表空间 #选创建filespace,生成配置文件 [gpadmin@master ~]$ hawq f ...

  7. php 服务器的安全笔记

    php 服务器的安全笔记 操作系统安全 默认端口修改 MySQL 端口禁止外网访问 用户权限 父进程 子进程 目录权限 TODO Web Server 版本信息 服务器版本信息 PHP 版本 open ...

  8. linux NTP配置

    时间是由计算机内的石英晶体振荡电路以:NetworkTimeProtocol(NTP):通常情况下,时间同步是按以下步骤进行的::(1):NTP客 户端向NTP服务器发出一个时间请:(2):当服务器接 ...

  9. 把Azure专线从Class模式迁移到ARM模式

    前面几篇文章介绍了Azure的ASM模式和ARM模式.很多用户已经在ASM模式下部署了Azure的专线服务,如果部署的应用是ARM模式,或ASM模式和ARM模式都有,就需要把ASM模式的专线迁移到AR ...

  10. C++字符集问题终极分析(可解决乱码问题)

    最近研究vc,windows的东西真是很傻瓜,啥都给你做好,有个好处就是开发方便了. 有个弊端就是完全按微软的一套进行,规则都是它定的,你得知道它的很多api, 开发出来的代码效率不高,不过却可以比较 ...