POJ1201 Intervals(差分约束系统)
与ZOJ2770一个建模方式,前缀和当作点。
对于每个区间[a,b]有这么个条件,Sa-Sb-1>=c,然后我就那样连边WA了好几次。
后来偷看数据才想到这题还有两个隐藏的约束条件。
这题前缀和表示的是区间内点存在的个数,因此:
- Si>=Si-1
- Si-Si-1<=1
所以,差分约束系统的构图一定要考虑全面。
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 65555
#define MAXM 411111
#define INF (1<<30) struct Edge{
int v,cost,next;
}edge[MAXM];
int head[MAXN],NE,NV,vs;
void addEdge(int u,int v,int cost){
edge[NE].v=v; edge[NE].cost=cost; edge[NE].next=head[u];
head[u]=NE++;
} int d[MAXN];
bool vis[MAXN];
void SPFA(){
for(int i=; i<NV; ++i){
vis[i]=; d[i]=INF;
}
vis[vs]=; d[vs]=;
queue<int> que;
que.push(vs);
while(!que.empty()){
int u=que.front(); que.pop();
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(d[v]>d[u]+edge[i].cost){
d[v]=d[u]+edge[i].cost;
if(!vis[v]){
vis[v]=;
que.push(v);
}
}
}
vis[u]=;
}
}
int main(){
int n,a,b,c;
while(~scanf("%d",&n)){
memset(head,-,sizeof(head));
NE=;
int maxu=;
while(n--){
scanf("%d%d%d",&a,&b,&c);
++a; ++b;
addEdge(b,a-,-c);
maxu=max(b,maxu);
}
vs=maxu+; NV=vs+;
for(int i=; i<=maxu; ++i) addEdge(i-,i,);
for(int i=; i<=maxu; ++i) addEdge(i,i-,);
for(int i=; i<=maxu; ++i) addEdge(vs,i,);
SPFA();
int res=INF;
for(int i=; i<=maxu; ++i) res=min(res,d[i]);
printf("%d\n",-res);
}
return ;
}
POJ1201 Intervals(差分约束系统)的更多相关文章
- POJ1201 Intervals差分约束系统(最短路)
Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...
- POJ1201 Intervals[差分约束系统]
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26028 Accepted: 9952 Descri ...
- Intervals(差分约束系统)
http://poj.org/problem?id=1201 题意:给定n个整数闭区间[a,b]和n个整数c,求一个最小的整数集合Z,满足Z里边的数中范围在闭区间[a,b]的个数不小于c个. 思路:根 ...
- POJ 1201 Intervals (差分约束系统)
题意 在区间[0,50000]上有一些整点,并且满足n个约束条件:在区间[ui, vi]上至少有ci个整点,问区间[0, 50000]上至少要有几个整点. 思路 差分约束求最小值.把不等式都转换为&g ...
- 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 ...
- poj 1201/zoj 1508 intervals 差分约束系统
// 思路 : // 图建好后 剩下的就和上一篇的 火烧连营那题一样了 求得解都是一样的 // 所以稍微改了就过了 // 最下面还有更快的算法 速度是这个算法的2倍#include <ios ...
- PKU 1201 Intervals(差分约束系统+Spfa)
题目大意:原题链接 构造一个集合,这个集合内的数字满足所给的n个条件,每个条件都是指在区间[a,b]内至少有c个数在集合内.问集合最少包含多少个点.即求至少有多少个元素在区间[a,b]内. 解题思路: ...
- 【POJ 1716】Integer Intervals(差分约束系统)
id=1716">[POJ 1716]Integer Intervals(差分约束系统) Integer Intervals Time Limit: 1000MS Memory L ...
随机推荐
- 破解LR时,解决loadrunner 破解错误:license security violation.Operation is not allowed
一.由于在压力测试执行中,出现一个-10803的错误 ,为解决这个错误,重新设置的环境变量,在次执行错误,这个问题解决了,但另外一个问题出来了,LR,打开脚本编辑器老提示找不到TEMP目录,当时没有想 ...
- 微信开发学习日记(八):7步看懂weiphp插件机制,核心目标是响应微信请求
又经过了几个小时的梳理.回顾,截至目前,终于对weiphp这个框架的机制搞明白了些.想要完全明白,自然还需要大把的时间.第1步: 配置微信公众号,http://weiphp.jiutianniao ...
- zookeeper 配置详解
http://blog.csdn.net/shenlan211314/article/details/6185176 因博主原创,所以不能转载 下面是更为详细的配置说明: 前面两篇文章介绍了Zook ...
- Linux 怎么把自己写的脚本添加到服务里面,即可以使用service命令来调用
chmod 755 filename; mv filename /etc/init.d/; chkconfig --add filename #!/bin/bash #chkconfig: 345 8 ...
- 54. 八皇后问题[eight queens puzzle]
[本文链接] http://www.cnblogs.com/hellogiser/p/eight-queens-puzzle.html [题目] 在8×8的国际象棋上摆放八个皇后,使其不能相互攻击,即 ...
- css form 表单组对齐
2014年7月1日 15:31:17 第一次写css,见谅 css: .form-box .form-group .form-label {text-align: right; width: 200p ...
- Java for LeetCode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- JQuery发送ajax请求不能用数组作为参数
JQuery发送ajax请求不能用数组作为参数,否则会接收不到参数, 一.js代码如下: $('#delete-button').click(function(){ var select ...
- 【python】在python中调用mysql
资料:http://www.runoob.com/python/python-mysql.html 例子: 假设有数据路HTMLHASH, 其中有表htmlhash, 表中包括两个varchar(10 ...
- osg 中鼠标拾取线段的端点和中点
//NeartestPointNodeVisitor.h #pragma once #include <osg\Matrix> #include <vector> #inclu ...