码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1201 Intervals

时间:2016-08-05 21:25:36      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

 

 

 Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25125   Accepted: 9580

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

 

差分约束

 

 1 /**/
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<queue>
 8 using namespace std;
 9 const int INF=0x3f3f3f3f;
10 const int mxn=60000;
11 struct edge{
12     int v,dis;
13     int next;
14 }e[mxn*10];
15 int hd[mxn],cnt;
16 int dis[mxn];
17 int vis[mxn];
18 int inq[mxn];
19 int n;
20 void add_edge(int u,int v,int w){
21     e[++cnt].next=hd[u];e[cnt].v=v;e[cnt].dis=w;hd[u]=cnt;
22 }
23 bool SPFA(){
24     memset(dis,-1,sizeof dis);
25     queue<int>q;
26     q.push(0);
27     inq[0]=1;
28     vis[0]=1;
29     dis[0]=0;
30     while(!q.empty()){
31         int u=q.front();
32         for(int i=hd[u];i;i=e[i].next){
33             int v=e[i].v;
34             if(dis[u]+e[i].dis>dis[v]){
35                 dis[v]=dis[u]+e[i].dis;
36                 vis[v]++;
37                 if(vis[v]>n)return 0;
38                 if(!inq[v]){
39                     inq[v]=1;
40                     q.push(v);
41                 }
42             }
43         }
44         q.pop();
45         inq[u]=0;
46     }
47     return 1;
48 }
49 int main(){
50     scanf("%d",&n);
51     int i,j;
52     int mx=-1,a,b,c;
53     for(i=1;i<=n;i++){
54         scanf("%d%d%d",&a,&b,&c);
55         add_edge(a,b+1,c);
56         mx=max(mx,b+1);
57     }
58     for(i=0;i<mx;i++){
59         add_edge(i,i+1,0);
60         add_edge(i+1,i,-1);
61     }
62     SPFA();
63     printf("%d\n",dis[mx]);
64     return 0;
65 }

 

POJ1201 Intervals

标签:

原文地址:http://www.cnblogs.com/SilverNebula/p/5742599.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!