Intervals poj 1201 差分约束系统

Intervals

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22503   Accepted: 8506

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

Southwestern Europe 2002

设   s[i]表示集合中小于等于i的元素个数,  u   v   w      s[v]-s[u-1]>=w;

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <string>
  7 #include <vector>
  8 #include <set>
  9 #include <map>
 10 #include <queue>
 11 #include <stack>
 12 #include <sstream>
 13 #include <iomanip>
 14 using namespace std;
 15 const int INF=0x4fffffff;
 16 const int EXP=1e-6;
 17 const int MS=50005;
 18
 19 struct edge
 20 {
 21       int u,v,w;
 22 }edges[10*MS];
 23
 24 int maxv,minv;
 25 int dis[MS];
 26 int esize,n;
 27
 28 bool bellman()
 29 {
 30       memset(dis,0,sizeof(dis));
 31       bool flag=true;
 32       int cnt=0;
 33       while(flag)
 34       {
 35             flag=false;   //表示没有更新了
 36
 37             if(cnt++>n)    //更新次数大于n-1,
 38                   return false;
 39             for(int i=0;i<esize;i++)
 40             {
 41                   if(dis[edges[i].u]+edges[i].w<dis[edges[i].v])
 42                   {
 43                         dis[edges[i].v]=dis[edges[i].u]+edges[i].w;
 44                         flag=true;
 45                   }
 46             }
 47
 48             //0<=s[i]-s[i-1]<=1      这些边可以不用存储
 49
 50             //  i-1   -->i  1
 51
 52             for(int i=minv;i<=maxv;i++)
 53             {
 54                   if(dis[i-1]+1<dis[i])
 55                   {
 56                         dis[i]=dis[i-1]+1;
 57                         flag=true;
 58                   }
 59             }
 60
 61             //   i--> i-1   0
 62
 63             for(int i=maxv;i>=minv;i--)
 64             {
 65                   if(dis[i]<dis[i-1])
 66                   {
 67                         dis[i-1]=dis[i];
 68                         flag=true;
 69                   }
 70             }
 71       }
 72       return true;
 73 }
 74
 75
 76
 77 int main()
 78 {
 79       while(scanf("%d",&n)!=EOF)
 80       {
 81             int u,v,w;
 82             esize=0;
 83             maxv=0;
 84             minv=INF;
 85             for(int i=0;i<n;i++)
 86             {
 87                   scanf("%d%d%d",&u,&v,&w);
 88                   //   s[i]表示集合中小于等于i的元素个数
 89                   //s[v]-s[u-1]>=w;    v->u-1   -w;
 90                   edges[esize].u=v;
 91                   edges[esize].v=u-1;
 92                   edges[esize++].w=-w;
 93                   if(v>maxv)
 94                         maxv=v;
 95                   if(u<minv)
 96                         minv=u;
 97             }
 98             bellman();
 99             printf("%d\n",dis[maxv]-dis[minv-1]);
100       }
101       return 0;
102 }
时间: 2024-11-10 22:04:10

Intervals poj 1201 差分约束系统的相关文章

poj 1201 差分约束+spfa

非常经典的差分约束系统的建模.求最小值需要转化为求最长路. 1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int INF = 99999999; 8 const int N = 50002; 9 const int M = 200000; 10 int head[N];

poj 1201(差分约束)

Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24948   Accepted: 9491 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 po

POJ 3169-Layout(差分约束系统)

题目地址:id=3169">POJ 3169 题意:N头牛排队吃饭 排编号顺序排.大的永远在小的前面.但牛之间有的关系好.有的差,所以有的牛想离某些牛的距离最远不超过D 有的必须大于D 给出它们的关系 求第n头牛跟第一头的最远距离. 思路:非常easy的查分约束,公式非常好看出来.求最大值 约束条件转化为 < : 所以有S大-S小 <= D1,S大-S小>=D2 把这个条件转化一下--> S小-S大<=-D2. #include <stdio.h>

POJ 1364-King(差分约束系统)

题目地址:POJ 1364 题意:n个数的一个序列,m个约数,si, ni, oi, ki, 代表了序列中第si个数到第si+ni个数的和大于或小于ki,gt 为大于,lt 为 小于.问是否存在相悖的约束. 思路: 因为这个题目是单纯的大于或者小于,所以要变成大于等于或者小于等于,这样的话就在k值的基础上+1或者-1.所以就有了以下的约束. 设sum[i]为前i个数的和,那么就可以得到约束:si, ni, oi, ki sum[0] = 0 oi为gt时:sum[si-1] - sum[si+n

【POJ 1201】 Intervals(差分约束系统)

[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23817   Accepted: 9023 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p

poj 1201 Intervals(差分约束系统)(困难)

Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23205   Accepted: 8764 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 po

POJ 1201 &amp;&amp; HDU 1384 Intervals(差分约束系统)

题目地址:POJ 1201   HDU 1384 根据题目意思,可以列出不等式如下: Sj-Si>=c; Si-S(i-1)>=0; S(i-1)-Si>=-1; 然后用最短路spfa来解决这个不等式.用max来当源点,0为终点.最终的-d[0]就是答案. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <

POJ 1201 Intervals(图论-差分约束)

Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20779   Accepted: 7863 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 po

POJ 1201 Intervals(差分约束)

[题目链接] http://poj.org/problem?id=1201 [题目大意] 告诉你一个区间至少要选定的数字的个数,给出n个区间的需求 问最少选取几个数字可以满足所有的需求 [题解] 对于区间[a,b]建立不等式Sb+1-Sa>=c,最后要求最小化Smax, 结合基础条件Si+1-Si>=0,Si-Si+1>=-1,构造差分约束系统求最长路就是答案. [代码] #include <cstdio> #include <algorithm> #includ