HDU 1384 Intervals(差分约束)

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
 
需要求S[r]-S[l-1]>=ans,即S[l-1]-S[r]<=-ans。若以r为起点 ,而-ans就为r到l-1的最短路径,即-dist[Maxl-1]。
 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];
11 int dis[MAXN],head[MAXN];
12 bool vis[MAXN];
13 int cnt,n,l,r;
14 queue<int>q;
15 
16 void init()
17 {
18     memset(head,0,sizeof(head));
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;
28     e[cnt].nxt = head[u];
29     head[u] = cnt;
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("%d\n",-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);
70             add(b,a-1,-c);    //b-(a-1)>=c
71         }
72         for (int i=l; i<=r; ++i)
73         {
74             add(i,i-1,0);    //i-(i-1)>=0
75             add(i-1,i,1);    //i-(i-1)<=1
76         }
77         spfa(); 
78     } 
79     return 0;
80 } 
posted @ 2017-07-06 16:22  MJT12044  阅读(178)  评论(0编辑  收藏  举报