P1250 Trees

Topic description

There are several houses on one side of a street. Residents want to plant some trees by the roadside for environmental reasons. Roadside areas are divided into blocks and numbered 1..N. Each section is one unit size and can plant up to one tree. Each resident wants to plant some trees in front of the door and assigns three numbers B, E, T. These three numbers indicate that the resident wants to plant at least T trees between B and E. Of course, B≤E, residents must remember that no more trees can be planted in the designated area than the number of plots in the area, so T≤E-B+l. Individual areas where residents want to plant trees can intersect. Your task is to find the minimum number of trees that satisfy all requirements.

Write a program to do the following:

Input and output format

Input format:

 

The first line contains data N, the number of regions (0<N≤30000);

The second line contains H, the number of houses (0<H≤5000);

The following H line describes the needs of the residents: BET, 0<B≤E≤30000, T≤E-B+1.

 

Output format:

 

The output file has only one line with the number of trees written

 

Input and output example

Input Example #1:  Copy
9
4
1 4 2
4 6 2
8 9 2
3 5 2
Output Sample #1:  Copy
5 

Differential Constraint Basic Topics

#include<bits/stdc++.h>

#define N 3000500
#define inf 0x7fffffff

using namespace std;

inline void read(int &x){
    x=0;register char ch=getchar();int flg=1;
    for(;ch<'0'||ch>'9';) {if(ch=='-')flg=-1;ch=getchar();}
    for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
    x *= flg;
}
int n,m,b,c,t,head[N],tot,d[N];
bool vis[N];
struct node{
    int to,next,dis;
} and [N];

void add(int u,int v,int w){
    e[++tot].to=v;e[tot].next=head[u];head[u]=tot;e[tot].dis=w;
}

void spfa(int s)
{
    for(int i=1;i<=n+1;i++) d[i]=1e9;
    d [s] = 0 ; queue < int > que;
    que.push(s);vis[s]=1;
    while(!que.empty())
    {
        int h = que.front ();
        that.pop();
        vis[h]=0;
        for(int i=head[h];i!=-1;i=e[i].next)
            if(d[e[i].to]>d[h]+e[i].dis)
            {
                d[e[i].to]=d[h]+e[i].dis;
                if(!vis[e[i].to])
                {
                    que.push(e[i].to);
                    vis [e [i] .to] = 1 ;
                }
            }
    }
}

intmain ()
{
    read(n);read(m);
    int s=n+1;
    memset(head,-1,sizeof(head));
    for(int i=0;i<=n;i++) add(s,i,0);
    for(int i=1;i<=m;i++){
        read(b);read(c);read(t);
        add(c,b-1,-t);
    }for(int i=1;i<=n;i++){
        add(i-1,i,1);
        add(i,i-1,0);
    }spfa(s);
    you are from = 2e9;
    for ( int i = 0 ; i <= n; i ++) minn = min (minn, d [i]);
    printf("%d\n",d[N]-minn);
    return 0;
}
View Code

 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325143530&siteId=291194637