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 ...
随机推荐
- iOS 关于内购
最近项目的第三方支付导致项目被拒,记录一下关于内购 #import <StoreKit/StoreKit.h> //沙盒测试环境验证 #define SANDBOX @"http ...
- 2019年猪年颁奖典礼、公司年会、跨年晚会、科技会议、年终答谢会之幕布背景展板PSD模板-第三部分
16套--2019年猪年颁奖典礼.公司年会.跨年晚会.科技会议.年终答谢会之幕布.背景和展板PSD模板,免费颁奖典礼PSD展板背景幕布,下载地址:百度网盘,https://pan.baidu.com/ ...
- eclipse查看源代码问题
最近分析源代码时,eclipse总是出错,显示org.eclipse.core.runtime.CoreException,解决方法:在builderpath点击 add external jars, ...
- mongodb4简明笔记
就一数据库,掌握基本用法,其他的现学现卖就行了. 所以要把握基本套路. 创建数据库=>使用数据库=>创建集合=>使用集合=>创建文档=>使用文档 1.数据库 mongod ...
- IO多路复用(二) -- select、poll、epoll实现TCP反射程序
接着上文IO多路复用(一)-- Select.Poll.Epoll,接下来将演示一个TCP回射程序,源代码来自于该博文https://www.cnblogs.com/Anker/p/3258674.h ...
- Fulfilling Work: The Shippers More entrepreneurs hire 'fulfillment' outfits to store and ship their products
By Stu Woo June 23, 2011 Brett Teper faced a logistical problem when he and a partner founded ModPro ...
- python format用法详解
#常用方法:print('{0},{1}'.format('zhangk', 32)) print('{},{},{}'.format('zhangk','boy',32)) print('{name ...
- 原生开发小程序 和 wepy 、 mpvue 对比
1.三者的开发文档以及介绍: 原生开发小程序文档:点此进入 wepy 开发文档:点此进入 mpvue 开发文档:点此进入 2.三者的简单对比: 以下用一张图来简单概括三者的区别: 小程序支持的是 WX ...
- ECharts模块化使用5分钟上手
什么是EChats? 一句话: 一个数据可视化(图表)Javascript框架,详细?移步这里,类似(推荐)的有 HighCharts,其他? 嗯,先看看吧-- 快速上手: 模块化单文件引入(推荐). ...
- daterangepicker时间段插件
1.序言: daterangepicker是Bootstrap的一个时间组件,使用很方便 用于选择日期范围的JavaScript组件. 设计用于Bootstrap CSS框架. 它最初是为了改善报表而 ...