POJ 2101 Intervals 差分约束

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 27746 Accepted: 10687

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

差分约束系统是线性规划中的一种,在一个差分约束系统中,可以看成一个矩阵乘以一个向量小于另一个向量,求其中向量两个坐标的距离关系,约束条件对的不等式和单元最短路的松弛操作十分类似!

抽象出节点,根据节点性质和题目信息建边,最短路即可。

注意一定<=建边!

如果出现负权回路说明无解!

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<functional>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
//[a,b]区间内至少有c个数在集合内,问集合最少包含多少点
//a,b 可以取0 再读入的时候手动 a++,b++ 
//定义ti 为[0,i]内至少有多少个数字,那么由ta-1 - tb <= -c 
//由ti的定义可以推出它的性质1.ti-ti+1<=0 ti+1-ti<=1

const int MAXM = 3*50000 + 6;
const int MAXN = 50000 + 6;

struct edge
{
    LL to, next, dis;
}E[MAXM];
LL head[MAXN],tot;
LL dist[MAXN];
bool vis[MAXN];
void init()
{
    tot = 0;
    memset(head, -1, sizeof(head));
}
void spfa(LL ed)
{
    memset(dist, 0x3f3f3f3f, sizeof(dist));
    memset(vis, false, sizeof(vis));
    queue<LL> q;
    q.push(0);
    vis[0] = true;
    dist[0] = 0;
    while (!q.empty())
    {
        LL f = q.front();
        q.pop();
        vis[f] = false;
        for (LL i = head[f]; i != -1; i = E[i].next)
        {
            LL v = E[i].to, d = E[i].dis;
            if (dist[v] > dist[f] + d)
            {
                dist[v] = dist[f] + d;
                if (!vis[v])
                {
                    vis[v] = true;
                    q.push(v);
                }
            }
        }
    }
    cout << -dist[ed] << endl;
}
void addedge(LL u, LL v, LL d)
{
    E[tot].to = v;
    E[tot].dis = d;
    E[tot].next = head[u];
    head[u] = tot++;
}
int main()
{
    ios::sync_with_stdio(0);
    init();
    LL f, t, d, ed;
    LL n;
    cin >> n;
    while (n--)
    {
        cin >> f >> t >> d;
        f++, t++;
        addedge(f - 1, t, -d);
        ed = max(t, ed);
    }
    for (int i = 0; i < ed; i++)
    {
        addedge(i, i + 1, 0);
        addedge(i + 1, i, 1);
    }
    spfa(ed);
}

 

转载于:https://www.cnblogs.com/joeylee97/p/7552648.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值