区间(模板)

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int M=9999999;
int n,m,flag;
int vis[M],d[M],dis[M],f[M];
int nex[M],tot,to[M],head[M],cos[M];
void add(int x,int y,int z)
{
    nex[++tot]=head[x];
    to[tot]=y;
    cos[tot]=z;
    head[x]=tot;
}
void spfa(int s)
{
    d[1]=s;
    vis[s]=1;
    memset(dis,128,sizeof(dis));
    dis[s]=0;
    int h=0,t=1;
    do{
        h++;
        int x=d[h];
        vis[x]=0;
        for(int i=head[x];i;i=nex[i])
        {
            int tmp=to[i];
            if(dis[tmp]<dis[x]+cos[i])
            {
            dis[tmp]=dis[x]+cos[i]; 
            if(!vis[tmp]) 
            {
            vis[tmp]=1;
            d[++t]=tmp;
            f[tmp]++;
            if(f[tmp]>n)
            {printf("NO");
            flag=1;
            return ;
                }

            }
            }
        }
    }
    while(h<t);
}
int main()
{
    int h1=999999,t1=-999999;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
    int a,b,t;
    scanf("%d%d%d",&a,&b,&t);
    add(a-1,b,t);
    h1=min(h1,a-1);
    t1=max(t1,b);
}
    for(int i=h1;i<=t1;i++)
    add(99999,i,0);
    for(int i=h1;i<=t1;i++)
    {
    add(i,i-1,-1);
    add(i-1,i,0);
    }
    spfa(99999);
    int max1=-9999999;
    for(int i=h1;i<=t1;i++)
    {
    max1=max(max1,dis[i]);  
    }
    if(flag==0)
    printf("%d",max1);

}
原文地址:https://www.cnblogs.com/wspl98765/p/6819877.html