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 ... 
随机推荐
- 201621123014《Java程序设计》第十三周学习总结
			1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系统可以被多个用户通过网 ... 
- hdoj-1037-Keep on Truckin'(水题)
			题目链接 /* 题意:三个通道,如果比168低,那么过不去,输出最先碰到的低的通道高度值 */ #include <iostream> using namespace std; int ... 
- Project://STARK
			数据添加&编辑 删除&分页 搜索框功能 action批量操作 filter多条件过滤 pop_up弹窗 
- OpenCV那个版本的比较好用、稳定,参考资料也较多的
			2.4.8.上opencv官网就能下载到,对应不同版本的VS有编译好的文件. 2.4以后的变化不大.所以你可以百度opencv,有中文网站,上面有详细的说明.如果是在windows系统的话,可以使用v ... 
- Agc001_D Arrays and Palindrome
			传送门 题目大意 给定一个元素和为$N$的有$M$个数的序列$A$,请你可以$A$元素排列的顺序,并需要构造一个有$K$个($K$可以自己定)数的数列,使得任意一个长度为$N$的字符串,若满足:前$A ... 
- CodeForces - 687D: Dividing Kingdom II (二分图&带权并查集)
			Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great ... 
- idea 创建maven工程(入门)
			转:http://blog.csdn.net/qq_32588349/article/details/51461182 1. 下载Maven 官方地址:http://maven.apache.org/ ... 
- python函数-filter()
			filter(func, seq) filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断, ... 
- 如何隐藏掉Nginx的版本号
			最近新学习了一个命令curl,里面有一个参数-I可以查看到网站使用的是哪种服务器,比如: zhangxiaoliudeMacBook-Pro-2:~ zhangxiaoliu$ curl -I htt ... 
- HDU5469(树的dfs)
			Antonidas Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ... 
