# 差分约束

## 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.

## Sample Output

``````6
``````

# Intervals

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4292    Accepted Submission(s): 1624

Problem 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 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

Author

1384

`````` 1 #include<cstdio>
2 #include<cstring>
3 #include<queue>
4 #include<algorithm>
5 #define MAXN 50100
6 using namespace std;
7
8 struct Edge{
9     int to,nxt,w;
10 }e[MAXN<<2];
12 bool vis[MAXN];
13 int cnt,n,l,r;
14 queue<int>q;
15
16 void init()
17 {
19     cnt = 0;
20     l = 50100;
21     r = 0;
22 }
23 void add(int u,int v,int w)
24 {
25       cnt;
26     e[cnt].w = w;
27     e[cnt].to = v;
30 }
31 void spfa()
32 {
33     memset(dis,0x3f,sizeof(dis));
34     memset(vis,false,sizeof(vis));
35     q.push(r);
36     vis[r] = true;
37     dis[r] = 0;
38     while (!q.empty())
39     {
40         int u = q.front();
41         q.pop();
42         for (int i=head[u]; i; i=e[i].nxt)
43         {
44             int v = e[i].to;
45             int w = e[i].w;
46             if (dis[v]>dis[u] w)
47             {
48                 dis[v] = dis[u] w;
49                 if (!vis[v])
50                 {
51                     vis[v] = true;
52                     q.push(v);
53                 }
54             }
55         }
56         vis[u] = false ;
57     }
58     printf("%dn",-dis[l-1]);
59 }
60 int main()
61 {
62     while (scanf("%d",&n)!=EOF)
63     {
64         init()
65         for (int a,b,c,i=1; i<=n;   i)
66         {
67             scanf("%d%d%d",&a,&b,&c);
68             l = min(l,a);
69             r = max(r,b);
71         }
72         for (int i=l; i<=r;   i)
73         {
76         }
77         spfa();
78     }
79     return 0;
80 }
``````
``````5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
``````

Process to the end of file.

## POJ1201 Intervals(差分约束)，poj1201intervals

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

## 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 -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.

Input

5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1

## Sample Output

``````6
``````
 Time Limit:2000MS Memory Limit:65536K Total Submissions:28416 Accepted:10966

Sample Output

Output

TAG标签： 全球彩票平台
Ctrl+D 将本页面保存为书签，全面了解最新资讯，方便快捷。