UVA1723 Intervals
这题$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的更多相关文章
- [LeetCode] Non-overlapping Intervals 非重叠区间
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- POJ1201 Intervals[差分约束系统]
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26028 Accepted: 9952 Descri ...
- Understanding Binomial Confidence Intervals 二项分布的置信区间
Source: Sigma Zone, by Philip Mayfield The Binomial Distribution is commonly used in statistics in a ...
- Leetcode Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。
感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * ...
- Merge Intervals 运行比较快
class Solution { public: static bool cmp(Interval &a,Interval &b) { return a.start<b.star ...
- [LeetCode] 435 Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the ...
随机推荐
- sass入门篇
CSS 预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言,为 CSS 增加了一些编程的特性,将 CSS 作为目标生成文件,然后开发者就只要使用这种语言进行编码工作. 通俗的说,“CSS ...
- 机器学习(六)— logistic回归
最近一直在看机器学习相关的算法,今天学习logistic回归,在对算法进行了简单分析编程实现之后,通过实例进行验证. 一 logistic概述 个人理解的回归就是发现变量之间的关系,也就是求回归系数, ...
- UVA - 242 Stamps and Envelope Size (完全背包+bitset)
题意:给你一些邮票面值的集合,让你选择其中一个集合,使得“能用不超过n枚集合中的邮票凑成的面值集合S中从1开始的最大连续面值”(即mex(S)-1)最大.如果有多解,输出集合大小最小的一个:如果仍有多 ...
- UVA - 11925 Generating Permutations (思维,构造)
给你一个长度为n(n<=300)的排列,有两种操作,第一种是交换前两个数,第二种是把第一个数放到最后,让你用不超过2n^2次的操作把一个初始为1-n升序的排列变为该排列. 一开始被紫薯蛋疼的翻译 ...
- Python 2.7_爬取妹子图网站单页测试图片_20170114
1.url= http://www.mzitu.com/74100/x,2为1到23的值 2.用到模块 os 创建文件目录; re模块正则匹配目录名 图片下载地址; time模块 限制下载时间;req ...
- python 集合和深浅copy
#1数据类型的补充#2.集合set#3.深浅copy 补充:str --> bytes s.encode('gbk')bytes --> str s.decode('gbk') 1.数据类 ...
- 深入理解http/https之缓存 2
1:web缓存的实现 web缓存: WEB缓存(cache)位于Web服务器和客户端之间. 缓存会根据请求保存输出内容的副本,例如html页面,图片,文件,当下一个请求来到的时候:如果是相同的URL, ...
- bzoj 4712 洪水——动态DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4712 因为作为动态DP练习而找到,所以就用动态DP做了,也没管那种二分的方法. 感觉理解似乎 ...
- asp.net给按钮添加删除确认
if (!IsPostBack) { BtnDel.Attributes["onclick"] = "javascript:return window.confirm(' ...
- 把ASM下的HDD VM转换成ARM下Managed Disk的SSD VM
在ASM下,要把HDD的VM转换成SSD的VM步骤非常复杂.需要手工把Disk从普通存储账户复制到高级存储账户.再通过这个Disk创建VM. 目前在有了ASM到ARM的迁移工具,以及Managed D ...