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 ...
随机推荐
- I.MX6 make menuconfig进入x86模式
/************************************************************************ * I.MX6 make menuconfig进入x ...
- 关于 freetds pymssql 的安装部署
关于 freetds pymssql 的安装部署一.安装: (freetds-0.91 pymssql 2.0.1) 如果要在linux机器 连mysqlsever 1.需要安装freetds./co ...
- LeetCode Longest Continuous Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/ 题目: Giv ...
- 下载安装tomcat至服务器
1.安装JDK之后,下载Tomcat:http://tomcat.apache.org/download-70.cgi选择下载32-bit/64-bit Windows Service Install ...
- Python 算法之冒泡排序
冒泡排序 冒泡排序算法的原理如下:(从后往前) 1.比较相邻的元素.如果第一个比第二个大,就交换他们两个. 2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是 ...
- Dockerfile 部署应用执行脚本文件
FROM centos6.6:0.0.1 MAINTAINER syberos:wangmo RUN mv /etc/yum.repos.d/ /etc/yum.repos.d_bak/ && ...
- PHPExcel如何把该列的值设置为文本无科学计数?
$obj_sheet->setCellValueExplicit($cells[$_counter].$i, (isset($val[$_value_key]) ...
- 蓝桥杯 算法训练 ALGO-151 6-2递归求二进制表示位数
算法训练 6-2递归求二进制表示位数 时间限制:10.0s 内存限制:256.0MB 问题描述 给定一个十进制整数,返回其对应的二进制数的位数.例如,输入十进制数9,其对应的二进制数是1001 ...
- Java 的基本语法
Java 语言严格区分大小写 一个 Java 源文件里可以定义多个类,但其中只能有一个类被定义为 public 类 如果源文件中包含了 public 类,源文件的名称必须和该 public 类同名 p ...
- “百度杯”CTF比赛 2017 二月场(Misc Web)
爆破-1: 打开链接,是502 我直接在后面加个变量传参数:?a=1 出了一段代码 var_dump()函数中,用了$$a,可能用了超全局变量GLOBALS 给hello参数传个GLOBALS 得到f ...