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 ...
随机推荐
- Leetcode 974. Subarray Sums Divisible by K
前缀和(prefix sum/cumulative sum)的应用. 还用了一个知识点: a≡b(mod d) 则 a-b被d整除. 即:a与b对d同余,则a-b被d整除. class Solutio ...
- Android 关于后台杀死App之后改变服务器状态的一些尝试
前言: 如题,我的需求是:我需要在App在后台运行(未退出),调出最近运行记录,杀死App服务时,程序能够向服务器发送一条指令,以此达到我想要的目的. Android方面刚刚才开始玩,我一开始想的是可 ...
- 洛谷P2706 巧克力
题目背景 王7的生日到了,他的弟弟准备送他巧克力. 题目描述 有一个被分成n*m格的巧克力盒,在(i,j)的位置上有a[i,j]块巧克力.就在送出它的前一天晚上,有老鼠夜袭巧克力盒,某些位置上被洗劫并 ...
- list_for_each_entry
内核里面用list_for_each_entry实在太多了,定义在linux-3.10/include/linux/list.h: /** * list_for_each_entry - iterat ...
- Operating System-进程/线程内部通信-信号量、PV操作的实现和应用(解决哲学家进餐和生产者消费者问题)
本文主要内容: 信号量的实现 利用信号量解决哲学家用餐问题 利用信号量解决生产者消费者问题 一.信号量的实现 1.1 信号量结构 typedef struct { int value; struct ...
- Installing Redis more properly
Installing Redis more properly Running Redis from the command line is fine just to hack a bit with i ...
- 用phpinfo( )打印出来的php版本和在服务器上用php -v打印出来的版本不同的原因
php -v 是linux系统的php版本,而phpinfo里显示的是WEB Server中配置的版本.说简单点,你的系统中有两个php版本. 如果您阅读过此文章有所收获,请为我顶一个,如果文章中有错 ...
- hihoCoder#1121(二分图判定)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Nettle,从这个星期开始由我来完成我们的Weekly. 新年回家,又到了一年一度大龄 ...
- hihoCoder#1181(欧拉路径)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上一回中小Hi和小Ho控制着主角收集了分散在各个木桥上的道具,这些道具其实是一块一块骨牌. 主角继续往前走,面前出现了一 ...
- 机器学习:线性回归法(Linear Regression)
# 注:使用线性回归算法的前提是,假设数据存在线性关系,如果最后求得的准确度R < 0,则说明很可能数据间不存在任何线性关系(也可能是算法中间出现错误),此时就要检查算法或者考虑使用其它算法: ...