Intervals


Time Limit: 10 Seconds      Memory Limit: 32768 KB

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


Source: Southwestern Europe 2002

 //2013-11-25 09:45:31     Accepted     1508    C++    2120    2124
/* 题意:
有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该
序列中处于[ai,bi]这个区间的整数至少有ci个。如果存在这样的序列,请求出满足
题目要求的最短的序列长度是多少。如果不存在则输出 -1。 差分约束:
第一题差分约束,感觉还好,基本看过资料的应该都可以做。
难点在于构图部分,构好图后直接求其单源最短路径。
构图就是要满足
1)S(bi) - S(a(i-1))>=Ci
2)Si - S(i-1)>=0
3)S(i-1) - Si>=-1 即 map[bi][a(i-1)]=-ci
map[i][i-1]=0
map[i-1][i]=1 然后在采用最短路算法求点n到点0的最短路,值再取反即为解
存在负权环时不存在 */
#include<stdio.h>
#include<string.h>
#define N 50005
#define inf 0x7ffffff
using namespace std;
struct node{
int u,v,w;
}edge[*N];
int n,edgenum,m;
int d[N];
int Max(int a,int b)
{
return a>b?a:b;
}
void addedge(int u,int v,int w)
{
edge[edgenum].u=u;
edge[edgenum].v=v;
edge[edgenum++].w=w;
}
bool bellman_ford()
{
bool flag;
for(int i=;i<=m;i++) d[i]=inf;
d[m]=;
for(int i=;i<m;i++){
flag=false;
for(int j=;j<edgenum;j++){
if(d[edge[j].u]!=inf && d[edge[j].v]>d[edge[j].u]+edge[j].w){
flag=true;
d[edge[j].v]=d[edge[j].u]+edge[j].w;
}
}
if(!flag) break;
}
for(int i=;i<edgenum;i++)
if(d[edge[i].u]!=inf && d[edge[i].v]>d[edge[i].u]+edge[i].w)
return false;
return true;
}
int main(void)
{
int a,b,c;
while(scanf("%d",&n)!=EOF)
{
edgenum=;
m=;
for(int i=;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
addedge(b,a-,-c);
m=Max(m,Max(a,b));
}
for(int i=;i<m;i++){
addedge(i,i+,);
addedge(i+,i,);
}
if(!bellman_ford()) puts("-1");
else printf("%d\n",-d[]);
}
return ;
}

贴一下SPFA的解法:

 //2013-11-25 22:05:24     Accepted     1508    C++    130    5444
#include<iostream>
#include<vector>
#include<queue>
#include<stdio.h>
#include<string.h>
#define N 50005
#define inf 0x7ffffff
using namespace std;
struct node{
int v,w;
node(int a,int b){
v=a;w=b;
}
};
vector<node>V[N];
int d[N],vis[N],in[N];
int n,m;
int spfa()
{
memset(vis,,sizeof(vis));
memset(in,,sizeof(in));
for(int i=;i<=m;i++) d[i]=-inf;
queue<int>Q;
Q.push(m);
d[m]=;
while(!Q.empty()){
int u=Q.front();
Q.pop();
if(in[u]>m) return ;
vis[u]=;
int n0=V[u].size();
for(int i=;i<n0;i++){
int v=V[u][i].v;
int w=V[u][i].w;
if(d[v]<d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
in[v]++;
Q.push(v);
vis[v]=;
}
}
}
}
return ;
}
int main(void)
{
int a,b,c;
while(scanf("%d",&n)!=EOF)
{
m=;
for(int i=;i<N;i++) V[i].clear();
for(int i=;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
m=max(m,max(a,b));
V[b].push_back(node(a-,c));
}
for(int i=;i<m;i++){
V[i].push_back(node(i+,-));
V[i+].push_back(node(i,));
}
if(!spfa()) puts("impossible");
else printf("%d\n",d[]);
}
return ;
}

zoj 1508 Intervals (差分约束)的更多相关文章

  1. poj 1201/zoj 1508 intervals 差分约束系统

      // 思路 : // 图建好后 剩下的就和上一篇的 火烧连营那题一样了 求得解都是一样的 // 所以稍微改了就过了 // 最下面还有更快的算法 速度是这个算法的2倍#include <ios ...

  2. POJ1201 Intervals(差分约束)

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

  3. hdu 1384 Intervals (差分约束)

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

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

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

  5. poj 1201 Intervals(差分约束)

    题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...

  6. poj 1201 Intervals——差分约束裸题

    题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...

  7. poj1201 Intervals——差分约束

    题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...

  8. POJ 1201 &amp; HDU1384 &amp; ZOJ 1508 Intervals(差分约束+spfa 求最长路径)

    题目链接: POJ:http://poj.org/problem?id=1201 HDU:http://acm.hdu.edu.cn/showproblem.php? pid=1384 ZOJ:htt ...

  9. POJ 2101 Intervals 差分约束

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27746   Accepted: 10687 Description You ...

随机推荐

  1. DB总结1

    DBA  重构 data  new york   committee   cobol codasyl  journal DDL  DML    关系演算  域关系演算语言(QBE)  元祖关系演算语言 ...

  2. RDVTabBarController--可自由定制的iOS底部导航控件

    RDVTabBarController:一个十分完善的tabBarController,可以自定义角标个数,爽的停不下来. RDVTabBarController地址:RDVTabBarControl ...

  3. DFS练习一---HDU 1342

    参考文章来源:http://blog.csdn.net/pengwill97/article/details/54850852 题目在这里:HDU.1342 最近在练习DFS,就找了一些题来做,力求自 ...

  4. JS - CommonJS、AMD、CMD

    CommonJS CommonJS是一种JS模块定义规范,它出现之初是为了解决JS天生没有模块管理的缺陷,它的终极目标是提供一个类似Python.Ruby和Java标准库.NodeJS的模块系统就是参 ...

  5. IDEA怎么生成UML类图

    说之前先说一下Diagram这个单词,意思是图表; 示意图; 图解; [数] 线图的意思. 打开设置 File->Setting或windows下按Ctrl+Alt+S 在搜索框中输入Diagr ...

  6. VM虚拟机里的Ubuntu系统怎么设置屏幕分辨率

    说白了就是安装VMWare tools工具,步骤如下: 1)在VMWare中启动ubuntu虚拟机 2)在VMWare中:右键单击启动虚拟机,点击[安装vmware tools] 3)在ubuntu中 ...

  7. Java web--过滤器

    本文引自:https://www.cnblogs.com/dudududu/p/8505177.html 参考博客:http://www.cnblogs.com/coderland/p/5902878 ...

  8. PHP自动生成分页链接

    page.class.php <?php class Page { // 分页栏每页显示的页数 public $rollPage = 5; // 页数跳转时要带的参数 public $param ...

  9. vue数据绑定html

    html标签的纯文本显示/被当做html标签处理: 1)使用两个大括号时,假如字符串内容是html标签,那么不会被转义: 2)使用三个大括号时,字符串内的html标签会被直接转义 a.两个大括号: & ...

  10. requests--etree--xpath

    # -*- coding: cp936 -*- import requests from lxml import etree url = 'https://weibo.cn/pub/' html = ...