码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2101 Intervals 差分约束

时间:2017-09-19 18:00:54      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:坐标   矩阵   esc   sed   mis   stack   follow   注意   ber   

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);
}

 

POJ 2101 Intervals 差分约束

标签:坐标   矩阵   esc   sed   mis   stack   follow   注意   ber   

原文地址:http://www.cnblogs.com/joeylee97/p/7552648.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!