POJ 1201 Intervals (差分约束+SPFA)

Intervals
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18595 Accepted: 6976

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

 

题意: 在区间[0,50000]上有一些整点,并且满足n个约束条件(u, v, w),即在区间[u, v]上至少有x个整点,问区间[0, 50000]上至少有几个整点。

 

假设不等式 x1-x2<=d  => x1<=x2+d,这种形式是不是很像最短路径中的更新操作,当x1>x2+d 的时候就必须更新 即d[u]>d[v]+w的时候就要松弛时一样的道理。

这道题的题意很好理解我们只要找到两个约束条件:

Sbi+1-Sai>=ci;

1>= Si+1-Si >=0;(Si+1-Si>=0; Si-Si+1>=-1)

注意这里是>=也就是如果是<就要更新,那么是不是最长路呢。

其中Si,是表示,[0,i-1]这个区间和我们要求的序列的共同值的个数,为什么要加1,是避免0的情况。

 

从不等式中易想到差分限制系统
  即若a-b>=c,建立从b指向a的边,边的权重为c,

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>

using namespace std;

const int VM=50010;
const int INF=0x3f3f3f3f;

struct Edge{
    int to,nxt;
    int cap;
}edge[VM<<2];   //注意这里的范围不能太小,否则WA

int n,cnt,head[VM],src,des;
int dis[VM],vis[VM];

void addedge(int cu,int cv,int cw){
    edge[cnt].to=cv;    edge[cnt].cap=cw;   edge[cnt].nxt=head[cu];
    head[cu]=cnt++;
}

int SPFA(){
    queue<int> q;
    while(!q.empty())
        q.pop();
    memset(vis,0,sizeof(vis));
    for(int i=src;i<=des;i++)
        dis[i]=-INF;
    dis[src]=0;
    vis[src]=1;
    q.push(src);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].nxt){
            int v=edge[i].to;
            if(dis[v]<dis[u]+edge[i].cap){
                dis[v]=dis[u]+edge[i].cap;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    return dis[des];
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d",&n)){
        cnt=0;
        memset(head,-1,sizeof(head));
        src=VM, des=0;
        int u,v,w;
        while(n--){
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v+1,w);     //  一般为(u - 1, v, w),但这道题为了防止溢出用v + 1。
            src=min(src,u);
            des=max(des,v+1);
        }
        for(int i=src;i<=des;i++){  //两个隐含的约束条件,即1 >= dis[i] - dis[i-1] >= 0,
            addedge(i,i+1,0);   // dis[i] >= dis[i-1] + 0(正向边
            addedge(i+1,i,-1);  // dis[i-1] - dis[i] >= -1 (反向边)
        }
        printf("%d\n",SPFA());
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/jackge/archive/2013/04/12/3017063.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值