POJ1201 Intervals (差分约束)
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
Output
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
题意:给定X轴上一些区间[ai,bi],其间至少有ci个点,求X轴上至少多少个点。
思路:记得高中是用bellman-ford优化到了NKOJ的第一名。。。。但是差不多搞忘差分约束了。所以现在再来整理总结一下。
此处求最小,显然需要构造T>=S+dist,然后用最长路求。 由于是点段,处理区间最好半开半闭。
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const int inf=0x7fffffff;
int cnt,n,Max,Min;
int Laxt[maxn],Next[maxn],To[maxn],Len[maxn];
int times[maxn],st[maxn],vis[maxn],inq[maxn];
void update()
{
cnt=;Min=inf;Max=-inf;
memset(Laxt,,sizeof(Laxt));
memset(vis,,sizeof(vis));
memset(inq,,sizeof(inq));
}
void add(int u,int v,int d)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
Len[cnt]=d;
}
bool spfa()
{
for(int i=Min-;i<=Max+;i++) st[i]=-inf;
queue<int>q;
q.push(Min); st[Min]=; inq[Min]=;
while(!q.empty()){
int u=q.front(); q.pop(); inq[u]=;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(st[v]<st[u]+Len[i]){
st[v]=st[u]+Len[i];
if(!inq[v]){
inq[v]=; vis[v]++; q.push(v);
//if(vis[v]>Max-Min) return false;
}
}
}
} return true;
}
int main()
{
int a,b,c;
while(~scanf("%d",&n)){
update();
for(int i=;i<=n;i++) {
scanf("%d%d%d",&a,&b,&c);
a++;b++;
if(b>Max) Max=b;
if(a-<Min) Min=a-1;
add(a-,b,c);
}
for(int i=Min;i<Max;i++) add(i+,i,-),add(i,i+,);
spfa();
printf("%d\n",st[Max]);
} return ;
}
POJ1201 Intervals (差分约束)的更多相关文章
- POJ1201 Intervals(差分约束)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 28416 Accepted: 10966 Description You ...
- poj1201 Intervals——差分约束
题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[a ...
- hdu 1384 Intervals (差分约束)
Intervals Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
- zoj 1508 Intervals (差分约束)
Intervals Time Limit: 10 Seconds Memory Limit: 32768 KB You are given n closed, integer interva ...
- 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 都 ...
- POJ1201基础差分约束
题意: 有一条直线,直线上做多有50000个点,然后给你组关系 a b c表明a-b之间最少有c个点,问直线上最少多少个点. 思路: a-b最少有c个点可以想象a到b+1的距 ...
- poj1201/zoj1508/hdu1384 Intervals(差分约束)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Intervals Time Limit: 10 Seconds Mem ...
- POJ1201 Intervals差分约束系统(最短路)
Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...
随机推荐
- python得到最长递增子串长度及起始位置【转】
def incSeq(seq): start = 0 for i in xrange(1, len(seq)): if seq[i] < seq[i-1]: yield start, i - s ...
- 邻接表的使用及和vector的比較
这几天碰到一些对建边要求挺高的题目.而vector不好建边,所以学习了邻接表.. 以下是我对邻接表的一些看法. 邻接表的储存方式 邻接表就是就是每一个节点的一个链表,而且是头插法建的链表,这里我们首先 ...
- python(36)- 测试题
1.8<<2等于? 32 “<<”位运算 264 132 64 32 16 8 4 2 1 原始位置 0 0 0 0 0 1 0 0 0 想左位移2位 0 0 0 1 0 0 ...
- Linux中du和df
Linux运维过程中,常常发现du和df返回值不一样,偶尔会发现区别非常大. 特定情况下,可能df看到磁盘已满,可是du推断磁盘剩余空间非常大. 文件系统分配当中的一些磁盘块用来记录它自身的一些数据. ...
- kubernetes调度之pod优先级和资源抢占
系列目录 Pod可以拥有优先级.优先意味着相对于其它pod某个pod更为重要.如果重要的pod不能被调度,则kubernetes调度器会优先于(驱离)低优先级的pod来让处于pending状态的高优先 ...
- javaScript中innerHTML,innerText,outerHTML,outerText的区别
开头说下innerText和outerText只在chrome浏览器中有效 定义和用法 innerHTML 属性设置或返回表格行的开始和结束标签之间的 HTML,包括标签. 来看代码 <!DOC ...
- MFC学习之对话框---对话框之间的domodle 数据传递
if(IDOK==data.DoModal()){tx->m_gao=data.m_he;tx->m_kuan=data.m_wi;tx->m_jiao=data.m_jiaoju; ...
- Win10升级.NET Framework 3.5或2.0遇到错误0x800f081f
具体方法如下: 1.将WIN10安装光盘ISO文件加载到虚拟光驱中. 2.WIN键+R键一起按,输入CMD后回车. 3.在CMD的命令行窗口里输入: cd C:Windowssystem32 跳转到s ...
- scrollify 全屏滚动插件
使用 1.引入文件 <script src="js/jquery.min.js"> </script> <script src="js/jq ...
- 2.alert() 函数
①alert() 函数在 JavaScript 中并不常用,但它对于代码测试非常方便. <!DOCTYPE html><html><body> <h1> ...