POJ 1201 Intervals(图论-差分约束)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 20779 | Accepted: 7863 |
Description
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
ai <= bi <= 50000 and 1 <= ci <= bi - ai+1.
Output
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
Source
题目大意:
n行。每行a,b,c,表示在区间a,b内要找c个数,问你总共至少要找多少个数?
解题思路:
差分约束系统。
在本题中。假设[a,b]中要找c个元素。那么:s[b]-s[a-1]>=c。我们能够推得:s[a-1] - s[b] <= -c
同一时候。因为每个值上最多仅仅能含有一个元素。那么:s[i] - s[i-1]<=1 ,又因为s[i] - s[i-1]>=0 推得:s[i-1] - s[i] <=0
这样:我们有了三个约束不等式:
s[a-1] - s[b] <= -c
s[i] - s[i-1]<=1
s[i-1] - s[i] <=0于是:假设起点为from,终点为to。我们仅仅要求出:s[to] -s[from-1] >= M就能够了,
因此求出 s[from-1]-s[to]<=-M,即求to 到 from-1 的最短路径,
注意:因为i<0,所以i-1可能小于0,因此所有向右平移1位。
解题代码:
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std; const int maxn=50000;
const int inf=0x3f3f3f3f; struct edge{
int u,v,w,next;
}e[maxn*10]; int head[maxn*2+10],dist[maxn*2+10],cnt;
int n,from,to; void initial(){
cnt=0;
from=inf,to=0;
for(int i=0;i<=maxn;i++) head[i]=-1;
} void adde(int u,int v,int w){
u++;v++;
e[cnt].u=u,e[cnt].v=v,e[cnt].w=w,e[cnt].next=head[u],head[u]=cnt++;
} void input(){
int u,v,w;
//[v]-[u-1]>=w [u-1]-[v]<=-w
for(int i=0;i<n;i++){
scanf("%d%d%d",&u,&v,&w);
adde(v,u-1,-w);
if(u<from) from=u;
if(v>to) to=v;
}
//0<=[i]-[i-1]<=1
for(int i=from;i<=to;i++){
adde(i-1,i,1);
adde(i,i-1,0);
}
} bool spfa(int from){
int s=from,num[maxn];
bool visited[maxn];
for(int i=0;i<=maxn;i++){
num[i]=0;
dist[i]=inf;
visited[i]=false;
}
queue <int> q;
q.push(s);
visited[s]=true;
dist[s]=0;
while(!q.empty()){
s=q.front();
q.pop();
for(int i=head[s];i!=-1;i=e[i].next){
int d=e[i].v;
if(dist[d]>dist[s]+e[i].w){
dist[d]=dist[s]+e[i].w;
if(!visited[d]){
visited[d]=true;
q.push(d);
num[d]++;
if(num[d]>n) return false;
}
}
}
visited[s]=false;
}
return true;
} void solve(){
//get [to]-[from-1]>=M; [from-1]-[to]<=-M
spfa(to+1);
cout<<-dist[from]<<endl;
} int main(){
while(scanf("%d",&n)!=EOF){
initial();
input();
solve();
}
return 0;
}
POJ 1201 Intervals(图论-差分约束)的更多相关文章
- POJ 1201 Intervals【差分约束】
传送门:http://poj.org/problem?id=1201 题意: 有n个如下形式的条件:,表示在区间[, ]内至少要选择个整数点.问你满足以上所有条件,最少需要选多少个点? 思路:第一道差 ...
- poj 1201 Intervals(差分约束)
做的第一道差分约束的题目,思考了一天,终于把差分约束弄懂了O(∩_∩)O哈哈~ 题意(略坑):三元组{ai,bi,ci},表示区间[ai,bi]上至少要有ci个数字相同,其实就是说,在区间[0,500 ...
- POJ 1201 Intervals (经典) (差分约束)
<题目链接> 题目大意:给你$n$段区间,$a_i,b_i,c_i$ 表示在 $[a_i,b_i]$ 区间内至少要选择$c_i$个点.现在问你在满足这n个条件的情况下,最少要选多少个点? ...
- 【题解】 POJ 1201 Intervals(差分约束)
懒得复制,戳我戳我 Solution: 这道题就是一个板子题 抽象成第\(a\)至第\(b\)间选择数的个数为\(c\),我们就可以用前缀和来表示,这样就可以得到不等式\(s[b]-s[a-1]> ...
- POJ 1201 Intervals(差分约束 区间约束模版)
关于差分约束详情可阅读:http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 题意: 给定n个区间[L,R], 每个区间至 ...
- poj 1201 Intervals【差分约束+spfa】
设s为前缀和,首先显然的条件是\[ s_{bi}-s_{ai-1}>=c \],然后隐含的是\[ s_i-s_{i-1}>=0 s_i-s_{i-1}<=1 \] 然后根据差分约束, ...
- POJ 1201 Intervals (差分约束,最短路)
题意: 有一个集合Z,其元素都是整整数,但是数量未知.现有n个约束,形如 [a,b]=c 表示整数区间[a,b]中有c个元素在Z中出现.问集合Z最小可能含多少个元素? 思路: 对于所给的区间 cnt[ ...
- poj 1201 Intervals(差分约束)
题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...
- poj 1201 Intervals——差分约束裸题
题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...
- POJ 1201 Intervals || POJ 1716 Integer Intervals 差分约束
POJ 1201 http://poj.org/problem?id=1201 题目大意: 有一个序列,题目用n个整数组合 [ai,bi,ci]来描述它,[ai,bi,ci]表示在该序列中处于[ai, ...
随机推荐
- Sublime Text 插件 autoprefixer
Sublime Text 早就有插件(Sublime Prefixr)使用 prefixr 的 API 来自动完成 CSS 前缀,但是 autoprefixer 更牛,这款可使用 Can I Use ...
- C#中的几个线程同步对象方法
在编写多线程程序时无可避免会遇到线程的同步问题.什么是线程的同步呢? 举个例子:如果在一个公司里面有一个变量记录某人T的工资count=100,有两个主管A和B(即工作线程)在早一些时候拿了这个变量的 ...
- 【转】Xcode常用快捷键与技巧分享
原文网址:http://www.jianshu.com/p/039954b0cbe0 工欲善其事必先利其器. 虽然Xcode编写objective-c or swift很完美, 但了解其工具的常用快捷 ...
- bzoj 1951 [Sdoi2010]古代猪文(数论知识)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1951 [思路] 一道优(e)秀(xin)的数论题. 首先我们要求的是(G^sigma{ ...
- 洛谷P1117 棋盘游戏
洛谷1117 棋盘游戏 题目描述 在一个4*4的棋盘上有8个黑棋和8个白棋,当且仅当两个格子有公共边,这两个格子上的棋是相邻的.移动棋子的规则是交换相邻两个棋子.现在给出一个初始棋盘和一个最终棋盘,要 ...
- cloudstack安装篇2-主机名配置
CloudStack要求正确配置主机名.在默认情况下,安装完操作系统后,主机名是localhost.localdomain.运行下面的命令来测试: hostname --fqdn 在此处将返回这样的结 ...
- Linux中_ALIGN宏背后的原理——内存对齐
转载自: http://englishman2008.blog.163.com/blog/static/2801290720114210254690/ 1. 原理 int a; int ...
- normalization归一化
简单的举个例子:一张表有两个变量,一个是体重kg,一个是身高cm.假设一般情况下体重这个变量均值为60(kg),身高均值为170(cm).1,这两个变量对应的单位不一样,同样是100,对于身高来说很矮 ...
- 从源码剖析一个Spark WordCount Job执行的全过程
原文地址:http://mzorro.me/post/55c85d06e40daa9d022f3cbd WordCount可以说是分布式数据处理框架的”Hello World”,我们可以以它为 ...
- Windows Service中使用Threading.Timer需注意回收
在Windows Service中使用Threading.Timer时需要注意回收池问题 Threading.Timer是基于线程池的,系统会对其进行垃圾回收. 当Threading.Timer定义在 ...