poj1201

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

 

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define INF 0x3f3f3f3f
#define MAXN (50000+10)
using namespace std;
struct Edge
{
    int from, to, dd,next;
    Edge(){}
    Edge(int fro,int t,int d,int net):from(fro),to(t),dd(d),next(net){}
}edge[3*MAXN];
int head[MAXN], edgenum;
bool vis[MAXN];
int dist[MAXN];
int n;
void init()
{
    edgenum = 0;
    memset(head,-1,sizeof(head));
}
void addEdge(int u, int v,int dist)
{
    Edge E = Edge(u, v, dist,head[u]);
    edge[edgenum] = E;
    head[u] = edgenum++;
}
void SPFA(int s, int *d)
{
   queue<int> Q;
    memset(vis,false,sizeof(false));
    d[s] = 0;
    vis[s] = true;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        vis[u] = false;
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(d[E.to] < d[u] + E.dd)
            {
                d[E.to] = d[u] + E.dd;
                if(!vis[E.to])
                {
                    vis[E.to] = true;
                    Q.push(E.to);
                }
            }
        }
    }
}
int main()
{

while(~scanf("%d",&n))
{
    init();
 memset(dist,-INF,sizeof(dist));
 int minn,maxx;
 minn=INF;
 maxx=-INF;
    for(int i=1;i<=n;i++)
    {
        int u,v,d;
        scanf("%d%d%d",&u,&v,&d);
        addEdge(u-1,v,d);
        minn=min(minn,u-1);
        maxx=max(v,maxx);
    }
    for(int i=minn;i<=maxx;i++)
    {
        addEdge(i,i+1,0);
        addEdge(i+1,i,-1);
    }
      SPFA(minn,dist);
      cout<<dist[maxx]<<endl;
}
}

猜你喜欢

转载自blog.csdn.net/qq_40859951/article/details/88055212