HDU 1384 Intervals(差分约束)
Intervals
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4292 Accepted Submission(s):
1624
n integers c1, ..., cn.
Write a program that:
> reads the
number of intervals, their endpoints 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
<= n <= 50 000) - the number of intervals. The following n lines describe
the intervals. The i+1-th line of the input contains three integers ai, bi and
ci separated by single spaces and such that 0 <= ai <= bi <= 50 000 and
1 <= ci <= bi - ai + 1.
Process to the end of file.
minimal size of set Z sharing at least ci elements with interval [ai, bi], for
each i = 1, 2, ..., n.
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define MAXN 50100
using namespace std; struct Edge{
int to,nxt,w;
}e[MAXN<<];
int dis[MAXN],head[MAXN];
bool vis[MAXN];
int cnt,n,l,r;
queue<int>q; void init()
{
memset(head,,sizeof(head));
cnt = ;
l = ;
r = ;
}
void add(int u,int v,int w)
{
++cnt;
e[cnt].w = w;
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
void spfa()
{
memset(dis,0x3f,sizeof(dis));
memset(vis,false,sizeof(vis));
q.push(r);
vis[r] = true;
dis[r] = ;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i=head[u]; i; i=e[i].nxt)
{
int v = e[i].to;
int w = e[i].w;
if (dis[v]>dis[u]+w)
{
dis[v] = dis[u]+w;
if (!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
vis[u] = false ;
}
printf("%d\n",-dis[l-]);
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
init()
for (int a,b,c,i=; i<=n; ++i)
{
scanf("%d%d%d",&a,&b,&c);
l = min(l,a);
r = max(r,b);
add(b,a-,-c); //b-(a-1)>=c
}
for (int i=l; i<=r; ++i)
{
add(i,i-,); //i-(i-1)>=0
add(i-,i,); //i-(i-1)<=1
}
spfa();
}
return ;
}
HDU 1384 Intervals(差分约束)的更多相关文章
- hdu 1384 Intervals (差分约束)
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 1384 Intervals (差分约束)
/* 给你 n 个区间 [Ai, Bi],要求从每一个区间中至少选出 Ci 个数出来组成一个序列 问:满足上面条件的序列的最短长度是多少? 则对于 不等式 f(b)-f(a)>=c,建立 一条 ...
- Hdu 1384(差分约束)
题目链接 Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1384 Intervals (差分约束)
Problem - 1384 好歹用了一天,也算是看懂了差分约束的原理,做出第一条查分约束了. 题意是告诉你一些区间中最少有多少元素,最少需要多少个元素才能满足所有要求. 构图的方法是,(a)-> ...
- HDU 1384 Intervals【差分约束-SPFA】
类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...
- POJ1201 Intervals(差分约束)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 10966 Description You ...
- POJ 1364 / HDU 3666 【差分约束-SPFA】
POJ 1364 题解:最短路式子:d[v]<=d[u]+w 式子1:sum[a+b+1]−sum[a]>c — sum[a]<=sum[a+b+1]−c−1 ...
- 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 3169 Layout (HDU 3592) 差分约束
http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...
随机推荐
- Struts2_Struts标签大致内容
Struts-Tags1.通用标签 a) property b) set i.默认为 action scope,会将值放入 request 和 ActionContext中 ii. page.requ ...
- [bootstrap]模态框总结
<!--data-backdrop禁止点击空白处关闭模态框,也可以js设置,其他参数可参考官网模态框文档--> <div class="modal fade" i ...
- sql数据库常用操作
string connectionString = @"Data Source=(local)\sql2008r2;Initial Catalog=Movies;Uid=sa;Pwd=sql ...
- Linux命令之文件重定向2
linux中重定向用符号“>”表示,语法一般是 源文件 > 目标文件 1)创出.txt文件touch 1.txt 注意:创建文件夹用mkdir 2)向.txt文件中写入内容 注意:①cat ...
- day001-日期格式类、装拆箱
1.Object 1.1 String类型可以不用重写toString()方法 1.2 自定义类一般都去重写toString()方法 调用时机: a)对象名调用toString() b)打印输出时,间 ...
- 01、Spark安装与配置
01.Spark安装与配置 1.hadoop回顾 Hadoop是分布式计算引擎,含有四大模块,common.hdfs.mapreduce和yarn. 2.并发和并行 并发通常指针对单个节点的应对多个请 ...
- PHP : 封装Mysqli的连接、关闭和增改查(面向过程)
废话不多说,直接上图和案例:(看行数进行拼接) 注意:连接数据库中,因为用了$CONNECT全局变量,所以我们的连接数据库封装方法必须要执行了才能获取到此全局变量的值 所引入的数据库文件(databa ...
- Arduino-串口函数Serial
串口是Arduino与其它设备进行通信的接口,我们需要很好的掌握它的使用.Arduino串口使用相关的函数共有10个(随着版本的升级,新版本加入了更多,具体请参见官网:http://www.ardui ...
- github设置添加SSH(转载自:破男孩)
注:本文来源于 破男孩 博客(http://www.cnblogs.com/ayseeing/p/3572582.html)能切实解决问题. 很多朋友在用github管理项目的时候,都是直接使用htt ...
- Android(java)学习笔记59:类继承的 注意事项
1. 类继承的注意事项: /* 继承的注意事项: A:子类只能继承父类所有非私有的成员(成员方法和成员变量) B:子类不能继承父类的构造方法,但是可以通过super(马上讲)关键字去访问父类构造方法. ...