POJ1201 Intervals (差分约束)
Write a program that:
reads the number of intervals, their end points and integers c1, ..., cn from the standard input,
computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i=1,2,...,n,
writes the answer to the standard output.
Input
Output
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
题意:给定X轴上一些区间[ai,bi],其间至少有ci个点,求X轴上至少多少个点。
思路:记得高中是用bellman-ford优化到了NKOJ的第一名。。。。但是差不多搞忘差分约束了。所以现在再来整理总结一下。
此处求最小,显然需要构造T>=S+dist,然后用最长路求。 由于是点段,处理区间最好半开半闭。
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const int inf=0x7fffffff;
int cnt,n,Max,Min;
int Laxt[maxn],Next[maxn],To[maxn],Len[maxn];
int times[maxn],st[maxn],vis[maxn],inq[maxn];
void update()
{
cnt=;Min=inf;Max=-inf;
memset(Laxt,,sizeof(Laxt));
memset(vis,,sizeof(vis));
memset(inq,,sizeof(inq));
}
void add(int u,int v,int d)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
Len[cnt]=d;
}
bool spfa()
{
for(int i=Min-;i<=Max+;i++) st[i]=-inf;
queue<int>q;
q.push(Min); st[Min]=; inq[Min]=;
while(!q.empty()){
int u=q.front(); q.pop(); inq[u]=;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(st[v]<st[u]+Len[i]){
st[v]=st[u]+Len[i];
if(!inq[v]){
inq[v]=; vis[v]++; q.push(v);
//if(vis[v]>Max-Min) return false;
}
}
}
} return true;
}
int main()
{
int a,b,c;
while(~scanf("%d",&n)){
update();
for(int i=;i<=n;i++) {
scanf("%d%d%d",&a,&b,&c);
a++;b++;
if(b>Max) Max=b;
if(a-<Min) Min=a-1;
add(a-,b,c);
}
for(int i=Min;i<Max;i++) add(i+,i,-),add(i,i+,);
spfa();
printf("%d\n",st[Max]);
} return ;
}
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 -- SKViedoNode类
SKViedoNode类 继承自 SKNode:UIResponder:NSObject 符合 NSCoding(SKNode)NSCopying(SKNode)NSObject(NSObject) ...
- 客户端svn出现authorization failed异常
原文:https://blog.csdn.net/big1989wmf/article/details/70144470 发现,原来是 服务端上面 svnserve 这个进程没有启动起来 然后,再试一 ...
- 解决Gradle执行命令时报Could not determine the dependencies of task ':compileReleaseJava'.
Could not determine the dependencies of task ':compileReleaseJava'. > failed to find target andro ...
- NATSserver配置具体解释
NATSserver配置具体解释 作者:chszs,未经博主同意不得转载. 经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 虽然NATS能够无配置的执行,但也能 ...
- Solaris服务管理
远程登录协议 telnet \ssh 等.当然我们可以查看谁登录过我的系统,以及可以利用ftp记录日志. 一.SMF: 服务管理工具 优点:自动恢复意外终止的服务,支持服务的依赖关系,一个服务可以有多 ...
- python(39)- 网络编程socket练习
基于tcp的套接字实现远程执行命令的操作 #服务端 import socket import subprocess phone=socket.socket(socket.AF_INET,socket. ...
- mysql 分表的3种方法
http://blog.51yip.com/mysql/949.html CSDN - Mysql MERGE分表对大数据量的处理 实战经验: 要分表的表引擎必须是myisam类型 ...
- smartUpload注意过程
操作的过程中一定要注意的几个方面: 1.将smartUpload.jar拷贝到tomcat/lib以及项目的lib下面,最好是只多不少! 2.因为上传的文件一般都很大,所以应该 ...
- ssh 保持连接
ssh 保持连接 使用 ssh 登陆到云主机上,一段时间没有操作终端,会发现 ssh 连接断了,终端无响应. 配置 ssh 客户端,使其以一定间隔时间向 sshd 服务端发送心跳包,可解决此问题. / ...
- UltimateRecyclerView的用法具体解释
近期在用非常多第三方库的时候,发现有一些附带的demo写的不是非常全面或者样例的代码太多,凝视太少,要想使用还要去看下源代码什么的(.. .用第三方开源库不就是想节省时间嘛).所以决定每周两到三篇.写 ...