Intervals

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4292    Accepted Submission(s):
1624

Problem Description
You are given n closed, integer intervals [ai, bi] and
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

 
Input
The first line of the input contains an integer n (1
<= 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.

 
Output
The output contains exactly one integer equal to the
minimal size of set Z sharing at least ci elements with interval [ai, bi], for
each i = 1, 2, ..., n.
 
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
 
Sample Output
6
 
Author
1384
 
需要求S[r]-S[l-1]>=ans,即S[l-1]-S[r]<=-ans。若以r为起点 ,而-ans就为r到l-1的最短路径,即-dist[Maxl-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(差分约束)的更多相关文章

  1. hdu 1384 Intervals (差分约束)

    Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. hdu 1384 Intervals (差分约束)

    /* 给你 n 个区间 [Ai, Bi],要求从每一个区间中至少选出 Ci 个数出来组成一个序列 问:满足上面条件的序列的最短长度是多少? 则对于 不等式 f(b)-f(a)>=c,建立 一条 ...

  3. Hdu 1384(差分约束)

    题目链接 Intervals Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. hdu 1384 Intervals (差分约束)

    Problem - 1384 好歹用了一天,也算是看懂了差分约束的原理,做出第一条查分约束了. 题意是告诉你一些区间中最少有多少元素,最少需要多少个元素才能满足所有要求. 构图的方法是,(a)-> ...

  5. HDU 1384 Intervals【差分约束-SPFA】

    类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b&l ...

  6. POJ1201 Intervals(差分约束)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28416   Accepted: 10966 Description You ...

  7. 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  ...

  8. poj 1716 Integer Intervals (差分约束 或 贪心)

    Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12192   Accepted: 514 ...

  9. zoj 1508 Intervals (差分约束)

    Intervals Time Limit: 10 Seconds      Memory Limit: 32768 KB You are given n closed, integer interva ...

  10. POJ 3169 Layout (HDU 3592) 差分约束

    http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...

随机推荐

  1. c\c++数据类型存储

    C四个 C++五个 一. 在c中分为这几个存储区 1.栈 - 由编译器自动分配释放 2.堆 - 一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收 3.全局区(静态区),全局变量和静态变量 ...

  2. Servlet是线程安全的吗?

    Servlet不是线程安全的. 要解释为什么Servlet为什么不是线程安全的,需要了解Servlet容器(即Tomcat)使如何响应HTTP请求的. 当Tomcat接收到Client的HTTP请求时 ...

  3. MongoDB 安装笔记

    一.MongoDB的安装 1.在MongoDB的官网下载对应的安装文件() 2.解压安装文件 #解压tgz文件 tar -zxvf mongodb-linux-x86_64-ubuntu1604-3. ...

  4. April 26 2017 Week 17 Wednesday

    We read the world wrong and say that it deceives us. 我们把世界看错了,反而说它欺骗了我们. It is not a cakewalk to see ...

  5. April 14 2017 Week 15 Friday

    Try to be a rainbow in someone's cloud. 当乌云萦绕心头,我愿意成为你的彩虹. Actually there are many rainbows in our l ...

  6. Uva 12657 双向链表

    题目链接:https://uva.onlinejudge.org/external/126/12657.pdf 题意: 给你一个从1~n的数,然后给你操作方案 • 1 X Y : move box X ...

  7. 【转】如何手动添加Android Dependencies包

    在ADT16 之前可以在工程里面做关联,eclipse会在工程上自动添加ReferenceLibrary.新版本的ADT修改了第三方jar的导入方式,只需要在工程目录下新建libs文件夹,注意是lib ...

  8. 20145238-荆玉茗 《Java程序设计》第四次实验报告

    20145238<Java程序设计>第四次实验报告 实验四 Android环境搭建 实验内容 1.搭建Android环境 2.运行Android 3.修改代码,能输出学号 实验步骤 搭建A ...

  9. Makefile 简述

    定义 Linux 环境下的程序员如果不会使用GNU make来构建和管理自己的工程,应该不能算是一个合格的专业程序员,至少不能称得上是 Unix程序员.在 Linux(unix )环境下使用GNU 的 ...

  10. ios数据持久化--CoreData框架的介绍和使用

    1.Core Data 是数据持久化存储的最佳方式 2.数据最终的存储类型可以是:SQLite数据库,XML,二进制,内存里,或自定义数据类型 在Mac OS X 10.5Leopard及以后的版本中 ...