【差分约束系统】POJ 1201--Intervals

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.

题解

裸的差分约束系统,好久没写差分约束了手痒。p.s.水博客都水的有点不好意思了。
代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 50006
#define maxe 200006
using namespace std;
inline char nc(){
    static char buf[100000],*i=buf,*j=buf;
    return i==j&&(j=(i=buf)+fread(buf,1,100000,stdin),i==j)?EOF:*i++;
}
inline int _read(){
    int sum=0;char ch=nc();
    while(!(ch>='0'&&ch<='9'))ch=nc();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
    return sum;
}
int n,m,tot,hed,tal,dis[maxn],que[maxn],lnk[maxn],nxt[maxe],son[maxe],w[maxe];
bool vis[maxn];
void add(int x,int y,int z){
    nxt[++tot]=lnk[x];son[tot]=y;w[tot]=z;lnk[x]=tot;
}
void spfa(){
    memset(vis,0,sizeof(vis));memset(dis,192,sizeof(dis));
    hed=0;que[tal=1]=0;vis[0]=1;dis[0]=0;
    while(hed!=tal){
        vis[que[hed=(hed+1)%maxn]]=0;
        for(int j=lnk[que[hed]];j;j=nxt[j]) if(dis[son[j]]<dis[que[hed]]+w[j]){
            dis[son[j]]=dis[que[hed]]+w[j];
            if(!vis[son[j]]){
                vis[son[j]]=0;
                que[tal=(tal+1)%maxn]=son[j];
                if(dis[que[(hed+1)%maxn]]<dis[que[tal]]) swap(que[tal],que[(hed+1)%maxn]);
            }
        }
    }
}
int main(){
    freopen("interval.in","r",stdin);
    freopen("interval.out","w",stdout);
    n=_read();
    for(int i=1;i<=n;i++){
        int x=_read(),y=_read(),z=_read();
        add(x-1,y,z);
    }
    add(0,1,0);
    for(int i=1;i<=50000;i++)add(i,i+1,0),add(i,i-1,-1);
    spfa();
    printf("%d",dis[50000]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值