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 ...
随机推荐
- [ASP.NET Core] 建置x86版本 (workaround)
前言 本篇文章介绍如何建置ASP.NET Core项目的x86版本输出(workaround),为自己留个纪录也希望能帮助到有需要的开发人员. ASP.NET Core官网 步骤 首先到微软官网的「. ...
- [Ubuntu] <uptime>命令
uptime 命令 就是查看系统启动时间的,前几个大家应该都很熟悉:当前时间.系统启动时间.正在登陆的用户数 最后的三个数字,分别代表过去 1分钟 5分钟 15分钟 的平均负载(Load Ave ...
- python—2.x中如何使用中文
python2.x 默认使用ASCII编码格式 python3.x 默认使用UTF-8编码格式 在python2.x文件的第一行增加一下代码,解释器会以utf-8编码来处理python文件. # *_ ...
- node stream流
stream 模块可以通过以下方式使用: const stream = require('stream'); Node.js 中有四种基本的流类型: Writable - 可写入数据的流(例如 f ...
- hive on hbase 数据表关联
有时,数据可以容易的存储在hive中,但是要导入到hbase里,可以不用写MR程序来操作,可以使用hive on hbase方式来创建相应的表关联关系来将hive中的数据导入到对应的hbase的表里, ...
- 英文Datasheet没那么难读
话说学好数理化,走遍天下都不怕.可是在这个所谓的全球化时代,真要走遍天下的话,数理化还真未必比得上一门外语.作为技术人员,可以看到的是目前多数前沿的产品和技术多来自发达的欧美等国家,而英语目前才是真正 ...
- Farm Irrigation ZOJ 2412(DFS连通图)
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
- PSP DAILY软件功能说明书
PSP DAILY软件功能说明书 一.开发背景 你在完成了一周的软件工程作业后,需要提交一个PSP图表,里面有4项,如下所示: 1.本周PSP表格,包含每项任务的开始.中断.结束.最终时间,格式如下: ...
- C++:默认初始化
一.什么是默认初始化 默认初始化,顾名思义,即为在定义变量时如果没有为其指定初始化值,则该变量会被C++编译器赋予默认的值.而变量被赋予的默认值到底是什么,则取决于变量的数据类型和变量的定义位置. 二 ...
- mininet实验 连接floodlight控制器
参考博客一 参考博客二 事先准备-floodlight安装 Java安装方法及环境变量配置 执行ifconfig命令获取floodlight所在服务器的IP地址. 1.启动floodlight jav ...