POJ1201:Intervals【差分约束】
题目大意:给出N个闭区间,每个区间给出一个ci值,让你找出最小的数集Z使得每个闭区间都有不少于ci个Z中的元素,求card(Z)
思路:06年集训队论文《浅析差分约束系统》有详细的解题,设Sn为[0,n]中Z中元素的个数,ai ,bi为区间的两个端点,则可列出以下不等式:
0<=Sn-S(n-1)<=1
S(bi+1)-S(ai)>=ci
然后就可以用差分约束做了,顺便提一下,如果要把0<=Sn-S(n-1)<=1这些边加进图中的话边集会非常的大,以至于一开始邻接表开50000时TLE 130000 RE 140000 WA 一直开到150000才AC
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string.h>
#include<iostream>
#define maxn 150010
using namespace std;
int head[maxn],point[maxn],next[maxn],value[maxn],dist[maxn];
int now=0,minx=19941117,maxx=-19941117;
queue<int> q;
void add(int x,int y,int c)
{
next[++now]=head[x];
head[x]=now;
point[now]=y;
value[now]=c;
}
void spfa(int s)
{
int u;
bool visit[maxn]={0};
for(int i=minx;i<=maxx;i++)dist[i]=-19941117;
q.push(s);
visit[s]=1;
dist[s]=0;
while(!q.empty())
{
u=q.front();
q.pop();
visit[u]=0;
for(int i=head[u];i!=0;i=next[i])
{
int k=point[i];
if (dist[u]+value[i]>dist[k])
{
dist[k]=dist[u]+value[i];
if (visit[k]==0)
{
visit[k]=1;
q.push(k);
}
}
}
}
}
int main()
{
int n,a,b,c;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&a,&b,&c);
if (b+1>maxx)maxx=b+1;
if (a<minx)minx=a;
add(a,b+1,c);
}
for(int i=minx;i<=maxx;i++)
{
add(i,i+1,0);
add(i+1,i,-1);
}
spfa(minx);
printf("%d\n",dist[maxx]);
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 ...
随机推荐
- CSS div 塌陷问题
嵌套塌陷 上下塌陷 overflow:hidden;
- 单页Html及Android App供小孩学习常用汉字
为了检验及帮助小孩学习常用汉字,简单开发本网页应用: 常用汉字是按使用频率排序的,来源于网上: 该简单应用 有Android APP下载 “学习常用汉字_20150910.apk” 单页Html 示例 ...
- 【前端】Html5浏览器缓存 sessionStorage 与 localStorage
一.sessionStorage: 浏览关闭会话结束就被清除:(不能跨页面) localStorage:永久保存: 二.使用 var storage = window.sessionStorage; ...
- RPC之远程过程调用
一. 简介 将一个函数运行在远程计算机上并且等待获取那里的结果,这个称作远程过程调用(Remote Procedure Call)或者 RPC. RPC是一个计算机通信协议. 1. 类比: 将计算机服 ...
- mac osx上为qt应用生成debug symbol
mac平台上,希望Qt编译的release程序也能包含debug symbol,这样出问题以后便于查找问题 开始按照http://doc.qt.io/qt-4.8/mac-differences.ht ...
- 【HEVC帧间预测论文】P1.8 Complexity Control of High Efficiency Video Encoders for Power-Constrained Devices
参考:Complexity Control of High Efficiency Video Encoders for Power-Constrained Devices <HEVC标准介绍.H ...
- feign 负载均衡熔断器
feign:和zuul配合进行负载均衡. 注解的含义: @EnableDiscoveryClient 声明它是一个资源服务端,即可以通过某些接口调用一些资源: @EnableFeignClients ...
- dockerfile 的最佳实践
Dockerfile 编写nginx容器 [root@mast nginx]# cat Dockerfile FROM centos MAINTAINER zhaoruidong RUN yum -y ...
- 获取指定点的RGB值
实现效果: 知识运用: Color对象的RGB属性 实现代码: private void button1_Click(object sender, EventArgs e) { OpenFileDia ...
- spring中注解的实现原理
@Autowired和@Resource的区别: 在Java中使用@Autowired和@Resource注解进行装配,这两个注解分别是:1.@Autowired按照默认类型(类名称)装配依赖对象,默 ...