题目链接:

POJ:http://poj.org/problem?id=1201

HDU:http://acm.hdu.edu.cn/showproblem.php?

pid=1384

ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=508

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 end points 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 <= 50000) -- 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 <= 50000 and 1 <= ci <= bi - ai+1.

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

题意:(转)

[ai, bi]区间内和点集Z至少有ci个共同元素。那也就是说假设我用Si表示区间[0,i]区间内至少有多少个元素的话,那么Sbi - Sai >= ci,这样我们就构造出来了一系列边。权值为ci,可是这远远不够。由于有非常多点依旧没有相连接起来(也就是从起点可能根本就还没有到终点的路线),此时。我们再看看Si的定义。也不难写出0<=Si
- Si-1<=1的限制条件。尽管看上去是没有什么意义的条件,可是假设你也把它构造出一系列的边的话,这样从起点到终点的最短路也就顺理成章的出现了。

我们将上面的限制条件写为允许的形式:

Sbi - Sai >= ci

Si - Si-1 >= 0

Si-1 - Si >= -1

这样一来就构造出了三种权值的边。而最短路自然也就没问题了。

但要注意的是,因为查分约束系统里经常会有负权边,所以为了避免负权回路,往往用Bellman-Ford或是SPFA求解(存在负权回路则最短路不存在)。

PS:

由于求的是[ai,bi]区间,所以我们加入边的时候须要(u-1, v, w)!

把距离dis初始化为负无穷, if(dis[v] < dis[u] + w)就可以!

POJ 和ZOJ用队列和栈都能过,可是HDU用栈会超时,仅仅能用队列!

代码例如以下:(栈)

#include <cstdio>
#include <cstring>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define N 50017
#define M 50017
int n, m, k;
int Edgehead[N], dis[N];
struct Edge
{
int v,w,next;
} Edge[3*M];
bool vis[N];
//int cont[N];
int minn, maxx;
int MIN(int a, int b)
{
if(a < b)
return a;
return b;
}
int MAX(int a, int b)
{
if(a > b)
return a;
return b;
}
void Addedge(int u, int v, int w)
{
Edge[k].next = Edgehead[u];
Edge[k].w = w;
Edge[k].v = v;
Edgehead[u] = k++;
}
int SPFA( int start)//stack
{
int sta[N];
int top = 0;
//memset(cont,0,sizeof(cont);
for(int i = 1 ; i <= n ; i++ )
dis[i] = -INF;
dis[start] = 0;
//++cont[start];
memset(vis,false,sizeof(vis));
sta[++top] = start;
vis[start] = true;
while(top)
{
int u = sta[top--];
vis[u] = false;
for(int i = Edgehead[u]; i != -1; i = Edge[i].next)//注意
{
int v = Edge[i].v;
int w = Edge[i].w;
if(dis[v] < dis[u] + w)
{
dis[v] = dis[u]+w;
if( !vis[v] )//防止出现环
{
sta[++top] = v;
vis[v] = true;
}
// if(++cont[v] > n)//有负环
// return -1;
}
}
}
return dis[maxx];
}
int main()
{
int u, v, w;
while(~scanf("%d",&n))//n为目的地
{
k = 1;
memset(Edgehead,-1,sizeof(Edgehead));
minn = INF;
maxx = -1;
for(int i = 1 ; i <= n ; i++ )
{
scanf("%d%d%d",&u,&v,&w);
Addedge(u-1,v,w);
maxx = MAX(v,maxx);
minn = MIN(u-1,minn); }
for(int i = minn; i <= maxx; i++)//新边,保证图的连通性还必须加入每相邻两个整数点i,i+1的边
{
Addedge(i,i+1,0);
Addedge(i+1,i,-1);
}
int ans = SPFA(minn);//从点minn開始寻找最短路
printf("%d\n",ans);
}
return 0;
}

HDU队列:

#include <cstdio>
#include <cstring>
#include <stack>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define N 50017
#define M 50017
int n, m, k;
int Edgehead[N], dis[N];
struct Edge
{
int v,w,next;
} Edge[3*M];
bool vis[N];
//int cont[N];
int minn, maxx;
int MIN(int a, int b)
{
if(a < b)
return a;
return b;
}
int MAX(int a, int b)
{
if(a > b)
return a;
return b;
}
void Addedge(int u, int v, int w)
{
Edge[k].next = Edgehead[u];
Edge[k].w = w;
Edge[k].v = v;
Edgehead[u] = k++;
}
int SPFA( int start)//stack
{
queue<int>q;
//int sta[N];
//memset(cont,0,sizeof(cont);
int top = 0;
for(int i = minn ; i <= maxx ; i++ )
dis[i] = -INF;
dis[start] = 0;
//++cont[start];
memset(vis,false,sizeof(vis));
//sta[++top] = start;
q.push(start);
vis[start] = true;
while(!q.empty())
{
//int u = sta[top--];
int u = q.front();
q.pop();
vis[u] = false;
for(int i = Edgehead[u]; i != -1; i = Edge[i].next)//注意
{
int v = Edge[i].v;
int w = Edge[i].w;
if(dis[v] < dis[u] + w)
{
dis[v] = dis[u]+w;
if( !vis[v] )//防止出现环
{
//sta[++top] = v;
q.push(v);
vis[v] = true;
}
// if(++cont[v] > n)//有负环
// return -1;
}
}
}
return dis[maxx];
}
int main()
{
int u, v, w;
while(~scanf("%d",&n))//n为目的地
{ k = 1;
memset(Edgehead,-1,sizeof(Edgehead));
minn = INF;
maxx = -1;
for(int i = 1 ; i <= n ; i++ )
{
scanf("%d%d%d",&u,&v,&w);
Addedge(u-1,v,w);
maxx = MAX(v,maxx);
minn = MIN(u-1,minn); }
for(int i = minn; i <= maxx; i++)//新边,保证图的连通性还必须加入每相邻两个整数点i,i+1的边
{
Addedge(i,i+1,0);
Addedge(i+1,i,-1);
}
int ans = SPFA(minn);//从点minn開始寻找最短路
printf("%d\n",ans);
}
return 0;
}

POJ 1201 &amp; HDU1384 &amp; ZOJ 1508 Intervals(差分约束+spfa 求最长路径)的更多相关文章

  1. zoj 1508 Intervals (差分约束)

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

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

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

  3. poj Layout 差分约束+SPFA

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

  4. poj 1201 Intervals(差分约束)

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

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

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

  6. POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束

    POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...

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

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

  8. poj1201 Intervals【差分约束+SPFA】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4303365.html   ---by 墨染之樱花 题目链接:http://poj.org/pr ...

  9. poj1201 Intervals——差分约束

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

随机推荐

  1. [BZOJ 2100] Apple Delivery

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2100 [算法] Answer = min{ dist(PB,PA1) + dist( ...

  2. [BZOJ 3387] Fence Obstacle Course

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3387 [算法] f[i][0]表示从第i个栅栏的左端点走到原点的最少移动步数 f[i ...

  3. 【POJ 2096】 Collecting Bugs

    [题目链接] http://poj.org/problem?id=2096 [算法] 概率DP [代码] #include <algorithm> #include <bitset& ...

  4. sublime界面主题

    一直以来都是使用的SUBLIME,真的很强大. 最近刚转到linux来学习C,把它重新配置了一遍,默认的字体颜色的搭配已经很不错了.不过界面的样子还是不太习惯.重新安装了下soda这个主题包,惭愧!即 ...

  5. [专辑] 也晒晒我的RBAC系统 ——行一山人的博客

    也晒晒我的RBAC系统(一):概述 也晒晒我的RBAC系统(二):系统实现原理简介 也晒晒我的RBAC系统(三):后台管理程序源码及使用演示 也晒晒我的RBAC系统(四):框架源代码(超值奉献,请勿拍 ...

  6. [Plugin] 文件上传利器SWFUpload使用指南

    SWFUpload是 一个flash和js相结合而成的文件上传插件,其功能非常强大.以前在项目中用过几次,但它的配置参数太多了,用过后就忘记怎么用了,到以后要用时又得 到官网上看它的文档,真是太烦了. ...

  7. RabbitMQ消息队列服务

    MQ 全称为 Message Queue, 消息队列( MQ ) 是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们. 一个软件它 ...

  8. java加密解密算法位运算

    一.实例说明 本实例通过位运算的异或运算符 “ ^ ” 把字符串与一个指定的值进行异或运算,从而改变每个字符串中字符的值,这样就可以得到一个加密后的字符串.当把加密后的字符串作为程序输入内容,异或运算 ...

  9. 【Python3】POP3协议收邮件

    初学Python3,做一个email的例子,虽然知道做的很渣渣,还是分享一下吧 POP3协议 POP3全称Post Official Protocol3,即邮局协议的第三个版本,它规定了怎样将个人计算 ...

  10. ML:自然语言处理NLP面试题

    自然语言处理的三个里程碑: http://blog.csdn.net/sddamoke/article/details/1419973 两个事实分别为: 一.短语结构语法不能有效地描写自然语言. 二. ...