http://acm.hdu.edu.cn/showproblem.php?pid=1384

Intervals

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

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

题目大意:给若干个区间,并且每个区间给定一个数C,表示集合S至少含有这个区间内C个整数,求满足要求的S的最小元素数

题目分析:典型的差分约束题目,即能根据题设条件列出若干不等式,并且所求问题中含有最小【或最大,最多,最少】字眼。都可以列出不等式,然后转化为最短路或者最长路。

     本题要求区间【LI,RI】内含有CI个整数,即可令S【I】表示最终集合S在【0,I】区间内含有的整数,则本题要求即可化为S【RI】-S【LI-1】>= CI【条件不等式一】

     又因为S【I】-S【I-1】>= 0 && S【I】-S【I-1】<= 1【条件不等式二、三】

       题目所求最小元素数ANS  即S【Rmax】-s【Lmin-1】>= ANS  【所求不等式】

      由所求不等式确定所求最长路还是最短路,从而据此再根据条件不等式可以建图,SPFA跑一遍即可。

【PS:边的结构体数组一定要开大一点,不然会TLE到怀疑人生...】

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int INF=;
struct edge{
int to;
int len;
int next;
}EDGE[];
queue<int>pq;
int edge_cnt=,dist[],stk[],head[],n;
void add(int x,int y,int z)
{
EDGE[edge_cnt].to=y;
EDGE[edge_cnt].next=head[x];
EDGE[edge_cnt].len=z;
head[x]=edge_cnt++;
}
void spfa()
{
while(!pq.empty())
{
int qwq=pq.front();pq.pop();
stk[qwq]=;
for(int i = head[qwq] ; i != - ; i = EDGE[i].next)
{
int v=EDGE[i].to;
if(dist[v]<dist[qwq]+EDGE[i].len)
{
dist[v]=dist[qwq]+EDGE[i].len;
if(!stk[v]){
stk[v]=;
pq.push(v);
}
}
}
}
}
int main()
{
while(scanf("%d",&n)==)
{
edge_cnt=;
int mmin=;
int mmax=-;
memset(dist,-,sizeof(dist));
memset(stk,,sizeof(stk));
memset(head,-,sizeof(head));
while(!pq.empty())pq.pop();
while(n--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a)
add(a-,b,c);
else
{
add(,b,c);
}
mmax=max(b,mmax);
mmin=min(a,mmin);
}
if(mmin==)
{
dist[]=;
add(,,);
add(,,-);
mmin=;
stk[]=;
pq.push();
}
else
{
stk[mmin-]=;
pq.push(mmin-);
dist[mmin-]=;
}
for(int i = mmin-;i<mmax;i++)
{
add(i+,i,-);
add(i,i+,);
}
spfa();
printf("%d\n",dist[mmax]);
}
return ;
}

【HDOJ1384】【差分约束+SPFA】的更多相关文章

  1. 【poj3169】【差分约束+spfa】

    题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...

  2. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

  3. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  4. 【BZOJ】2330: [SCOI2011]糖果(差分约束+spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束运用了最短路中的三角形不等式,即d[v]<=d[u]+w(u, v),当然,最长 ...

  5. (简单) POJ 3169 Layout,差分约束+SPFA。

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  6. poj Layout 差分约束+SPFA

    题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...

  7. BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)

    BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...

  8. POJ-3159.Candies.(差分约束 + Spfa)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 40407   Accepted: 11367 Descri ...

  9. 图论分支-差分约束-SPFA系统

    据说差分约束有很多种,但是我学过的只有SPFA求差分: 我们知道,例如 A-B<=C,那么这就是一个差分约束. 比如说,著名的三角形差分约束,这个大家都是知道的,什么两边之差小于第三边啦,等等等 ...

随机推荐

  1. linux网络操作 配置文件

    网络接口配置文件(网卡信息文件) '/etc/sysconfig/network-srcipts/ifcfg-*(eth0)' (注意区分大小写) DEVICE=eth0 网卡编号 HWADDR=08 ...

  2. NOIP2018复赛获奖分数线及名额分配办法

    中国计算机学会CCF NOI科学委员会.竞赛委员会召开会议,确定了CCF NOIP2018复赛获奖分数线及获奖名额分配方案. 提高组一等奖名额分配方案 提高组一等奖全国基准分数线: 245分 CCF ...

  3. JQuery button控制div或者section

    一.项目你需求 点击左边导航栏的某个按钮,右边内容栏显示出,相应的内容 效果如图   二.html与css.jQuery 1.div模式 <!DOCTYPE html PUBLIC " ...

  4. 图的关键路径,AOE,完整实现,C++描述

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  5. Android简易项目--傻瓜式阿拉伯语输入法(Dummy Arabic Input)

    一.应用名称 Android简易项目--傻瓜式阿拉伯语输入法(Dummy Arabic Input) 二.应用图标 三.应用说明 现在通行的阿拉伯语键盘布局并无规律可循,阿拉伯语使用者需要花费较多时间 ...

  6. 总结小bug

    1.下拉刷新问题 //不要用scroll-view 他会阻止刷新 //改用view <template name="movieGridTemplate"> <!- ...

  7. 2.6 C++通过引用来传递和返回类对象

    参考:http://www.weixueyuan.net/view/6338.html 总结: C++语言中,由类声明的对象,和其它类型声明的变量一样,同样可以通过传值.引用和指针的方式作为函数的参数 ...

  8. hibernate查询oracle数据库表报错SQL state [null]; error code [17027]; 流已被关闭; nested exception is java.sql.SQLException: 流已被关闭

    把表字段类型 long 修改为 number类型即可

  9. 在JS文件中,不需要<script>标签

    在JS文件中,不需要<script>标签\

  10. Zabbix4.0添加端口和进程监控

    一:Zabbix设置主动模式: vim /etc/zabbix/zabbix_agent.conf Server=192.168.1.10 #被动模式的serverip地址,如果设置纯被动模式,可以注 ...