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 ...
随机推荐
- L115
The reasons of reading books - part I1. You will optimize your brain powerThis shouldn't come as a s ...
- 学习汤姆大叔《深入理解JavaScript系列》有感(1) —— 立即调用的函数表达式
一. 下面代码用于理解函数的声明和调用. function makeCounter() { // 只能在makeCounter内部访问i var i = 0; return function () { ...
- map的内存分配机制分析
该程序演示了map在形成的时候对内存的操作和分配. 因为自己对平衡二叉树的创建细节理解不够,还不太明白程序所显示的日志.等我明白了,再来修改这个文档. /* 功能说明: map的内存分配机制分析. 代 ...
- Linux下系统监控工具nmon使用
Mongodb安装在Centos7或以上的版本,对于系统的监控方法如下: 1.从\\10.10.10.1\ShareDoc\User\Zchen\linux系统监控下下载2个工具 nmon16e_mp ...
- linux下ioctl遇到的坑
在驱动编程里面经常会用到ioctl的系统调用,发现cmd = 2的时候,用户ioctl直接返回-1. 原因在于在linux-x.xx/fs/ioctl.c定义的do_vfs_ioctl函数 int d ...
- 使用Jsonp实现跨域请求
来自百度百科的一段话: JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题.由于同源策略,一般来说位于 server1.exampl ...
- C# 常用文件操作
public class IoHelper { /// <summary> /// 判断文件是否存在 /// </summary> /// <param name=&qu ...
- Azure上通过haproxy实现APP Gateway或WAF的http跳转https
Azure上的APP Gateway是七层负载均衡服务,WAF是APP Gateway服务的扩展.在实现七层负载均衡的同时,增加了WAF的功能,可以对后台的HTTP服务进行保护. Azure WAF采 ...
- 【转】Inter-eNB S1 切换
本博客介绍Inter-eNB的S1切换的流程 当eNB收到测量报告,或是因为内部负荷分担等原因,触发了切换判决,进行eNB间小区间通过S1口的切换. 源eNB通过S1接口的 HANDOVER REQU ...
- PostgreSQL 监控数据库活动
监控数据库活动 1. 标准Unix 工具 [root@mysqlhq ~]# ps auxww | grep ^postgrespostgres 12106 0.0 0.0 340060 15064 ...