POJ1201 Intervals 【差分约束】
题目链接
题解
差分约束
令\(a[i]\)表示是否选择\(i\),\(s[i]\)表示\(a[i]\)的前缀和
对\(s[i] \quad i \in [-1,50000]\)分别建立一个点
首先有
\]
\]
然后就是限制条件
\]
然后就没了
用\(spfa\)跑最长路
由于题目保证有解,所以不会存在正环
复杂度上界是\(O(nm)\)的,但由于保证有解,而且\(spfa\)的玄学复杂度,并不会\(T\)
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,-0x3f3f3f3f,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 50005,maxm = 200005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int h[maxn],ne,N = 50001;
struct EDGE{int to,nxt,w;}ed[maxm];
inline void build(int u,int v,int w){
ed[++ne] = (EDGE){v,h[u],w}; h[u] = ne;
}
queue<int> q;
int d[maxn],vis[maxn];
void spfa(){
for (int i = 0; i <= N; i++) d[i] = -INF; d[N] = 0;
q.push(N);
int u;
while (!q.empty()){
u = q.front(); q.pop();
vis[u] = false;
Redge(u) if (d[to = ed[k].to] < d[u] + ed[k].w){
d[to] = d[u] + ed[k].w;
if (!vis[to]) q.push(to),vis[to] = true;
}
}
}
int main(){
int m = read(),a,b,c;
while (m--){
a = read(); b = read(); c = read();
a--; if (a == -1) a = N;
build(a,b,c);
}
build(N,0,0); build(0,N,-1);
for (int i = 1; i < N; i++)
build(i - 1,i,0),build(i,i - 1,-1);
spfa();
/*for (int i = 0; i < 15; i++)
printf("d[%d] = %d\n",i,d[i]);*/
printf("%d\n",d[N - 1]);
return 0;
}
POJ1201 Intervals 【差分约束】的更多相关文章
- POJ1201 Intervals(差分约束)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 10966 Description You ...
- poj1201 Intervals——差分约束
题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...
- hdu 1384 Intervals (差分约束)
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- zoj 1508 Intervals (差分约束)
Intervals Time Limit: 10 Seconds Memory Limit: 32768 KB You are given n closed, integer interva ...
- poj 1201 Intervals(差分约束)
题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...
- poj 1201 Intervals——差分约束裸题
题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...
- POJ1201基础差分约束
题意: 有一条直线,直线上做多有50000个点,然后给你组关系 a b c表明a-b之间最少有c个点,问直线上最少多少个点. 思路: a-b最少有c个点可以想象a到b+1的距 ...
- poj1201/zoj1508/hdu1384 Intervals(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Intervals Time Limit: 10 Seconds Mem ...
- POJ1201 Intervals差分约束系统(最短路)
Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...
随机推荐
- Spring学习(十一)-----Spring使用@Required注解依赖检查
Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...
- CentOS7.2最小化安装后系统优化
系统初始化技术的演变 1.sysvinit技术 (1)Linux系统的第一个进程(pid=1)为init: Linux 操作系统的启动首先从 BIOS 开始,接下来进入 boot loader,由 b ...
- 【转载】IntelliJ IDEA 2017常用快捷键
IntelliJ IDEA 是一款致力于提供给开发工程师沉浸式编程体验的IDE工具,所以在其中提供了很多方便高效的快捷键,一旦熟练掌握,整个开发的效率和体验将大大提升.本文就按照笔者自己日常开发时的使 ...
- 408. Add Binary【LintCode java】
Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...
- jquery on函数和prop与attr区别
一.jquery on()方法 1.语法 2.例子 $(document).ready(function(){ $("p").on("click",functi ...
- asp.net mvc 使用Ajax调用Action 返回数据【转】
使用asp.net mvc 调用Action方法很简单. 一.无参数方法. 1.首先,引入jquery-1.5.1.min.js 脚本,根据版本不同大家自行选择. <script src=& ...
- postman的巨坑 之 cookie
问题描述:一个后端接口,该接口需要校验登录态,登录态通过cookie中的一个传参k判断.在保证登录的前提下,调用机器A上的接口一直报“用户未登录”,调机器B上就没问题,于是开始排查问题. 解决过程: ...
- Snapseed玩出新高度,分分钟让你成p图大神! 转
(,,・∀・)ノ゛嗨呀 小阔爱们! 不知道大家记不记得~ 上周我们的副条发了一篇: <看过他的照片,我才知道什么是创意摄影> 德国仅22岁超现实主义艺术家Justin Peters 创造了 ...
- linux 下svn忽略文件
假设想忽略文件temp 1. cd到temp所在的目录下: 2. svn propedit svn:ignore . 注意:请别漏掉最后的点(.表示当前目录),如果报错请看下面 3. 打开的文件就是忽 ...
- 微信小程序测试的策略和注意事项
一.测试前准备(环境搭建) 1.前端页面 微信Web开发者工具安装.授权测试用的微信号可预览和调试小程序...可参考此文: 微信Web开发者工具-下载.安装和使用图解 2.管理后台 配置内网测试服务器 ...