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 ...
随机推荐
- Python基础入门(模块和包)
1 模块 1.1 什么是模块 在 Python 中,一个 .py 文件就称之为一个模块(Module). 我们学习过函数,知道函数是实现一项或多项功能的一段程序 .其实模块就是函数功能的扩展.为什么这 ...
- Tree - Gradient Boosting Machine with sklearn source code
This is the second post in Boosting algorithm. In the previous post, we go through the earliest Boos ...
- 基于kcp,consul的service mesh实现
名字kmesh 技术:proxy,kcp,consul proxy proxy分为前端和后端 前端代理服务层,包括外部的service 后端实现负债均衡 kcp kcp 基于udp,能够实现快速的传输 ...
- 所见即所得:七大无需编程的DIY开发工具
现如今,各种DIY开发工具不断的出现,使得企业和个人在短短几分钟内就能完成应用的创建和发布,大大节省了在时间和资金上的投入.此外,DIY工具的出现,也帮助广大不具备专业知识和技术的“移动开发粉”创建自 ...
- [leetcode-811-Subdomain Visit Count]
A website domain like "discuss.leetcode.com" consists of various subdomains. At the top le ...
- Fluent Python: memoryview
关于Python的memoryview内置类,搜索国内网站相关博客后发现对其解释都很简单, 我觉得学习一个新的知识点一般都要弄清楚两点: 1, 什么时候使用?(也就是能解决什么问题) 2,如何使用? ...
- 只执行一次的js 函数。
function runOnce(fn, context) { //控制让函数只触发一次 return function () { try { fn.apply(context || this, ar ...
- 2016-2017 ACM-ICPC, NEERC, Moscow Subregional Contest Problem L. Lazy Coordinator
题目来源:http://codeforces.com/group/aUVPeyEnI2/contest/229511 时间限制:1s 空间限制:512MB 题目大意: 给定一个n 随后跟着2n行输入 ...
- C语言的知识与能力的自评
1.我希望将来上班的地方是自己所感兴趣的,正在寻找自己感兴趣的,并且正在普及IT行业的相关知识. 2.我认为学习就是一个自我成长和自我提升以及认识世界的方法,学习的作用是可以不断的提升对这个世界的认识 ...
- 引用百度bcebos jar 503问题
最近使用jeecms管理公司各个站点,关于附件部分采用bos进行上传处理. 在引用bosjar的时候,出现503问题,打断点打印异常堆栈信息,也获取不到.后来使用watch,发现BosClientCo ...