zoukankan      html  css  js  c++  java
  • 最长k可重区间集问题(费用流,最大权不相交路径,离散化,网络流24题)

    题意

    给定实直线 (L)(n) 个开区间组成的集合 (I),和一个正整数 (k),试设计一个算法,从开区间集合 (I) 中选取出开区间集合 (S in I),使得在实直线 (L) 的任何一点 (x)(S) 中包含点 (x) 的开区间个数不超过 (k),且 (sum_{zin s}|z|) 达到最大。

    这样的集合 (S) 称为开区间集合 (I) 的最长 (k) 可重区间集。(sum_{zin s}|z|) 称为最长 (k) 可重区间集的长度。

    对于给定的开区间集合 (I) 和正整数 (k),计算开区间集合 (I) 的最长 (k) 可重区间集的长度。

    思路

    这道题建图挺神奇的。

    将所有区间的端点离散化处理,然后以它们为流网络的顶点。每个区间的两个端点(l, r)连容量是(1)(因为只能使用(1)次),费用为(r - l)。其实这一点还是挺容易想到的。

    然后相邻两个点(i, i + 1)之间连容量是(infty),费用是(0)的边。这一点我认为纯粹是为了流网络是连通的,也就是可以覆盖数轴上所有位置。

    为了保证不超过(k)的条件,设立虚拟源点(S),向第一个顶点连容量是(k),费用是(0)的边。设立虚拟汇点(T),最后一个顶点向其连容量是(k),费用是(0)的边。

    跑最大费用流即可。

    代码

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <vector>
    
    using namespace std;
    
    typedef pair<int, int> pii;
    
    const int N = 1010, M = 3010, inf = 1e8;
    
    int n, k, S, T;
    int h[N], e[M], ne[M], f[M], w[M], idx;
    int pre[N], d[N], incf[N];
    bool st[N];
    vector<int> nums;
    pii segs[N];
    
    void add(int a, int b, int c, int d)
    {
        e[idx] = b, f[idx] = c, w[idx] = d, ne[idx] = h[a], h[a] = idx ++;
        e[idx] = a, f[idx] = 0, w[idx] = -d, ne[idx] = h[b], h[b] = idx ++;
    }
    
    bool spfa()
    {
        memset(d, 0x3f, sizeof(d));
        memset(incf, 0, sizeof(incf));
        queue<int> que;
        que.push(S);
        d[S] = 0, incf[S] = inf;
        st[S] = true;
        while(que.size()) {
            int t = que.front();
            que.pop();
            st[t] = false;
            for(int i = h[t]; ~i; i = ne[i]) {
                int ver = e[i];
                if(d[ver] > d[t] + w[i] && f[i]) {
                    d[ver] = d[t] + w[i];
                    pre[ver] = i;
                    incf[ver] = min(incf[t], f[i]);
                    if(!st[ver]) {
                        st[ver] = true;
                        que.push(ver);
                    }
                }
            }
        }
        return incf[T] > 0;
    }
    
    int EK()
    {
        int cost = 0;
        while(spfa()) {
            int t = incf[T];
            cost += t * d[T];
            for(int i = T; i != S; i = e[pre[i] ^ 1]) {
                f[pre[i]] -= t;
                f[pre[i] ^ 1] += t;
            }
        }
        return cost;
    }
    
    int find(int x)
    {
        return lower_bound(nums.begin(), nums.end(), x) - nums.begin();
    }
    
    int main()
    {
        scanf("%d%d", &n, &k);
        memset(h, -1, sizeof(h));
        S = 0, T = 2 * n + 1;
        for(int i = 1; i <= n; i ++) {
            int a, b;
            scanf("%d%d", &a, &b);
            if(a > b) swap(a, b);
            segs[i] = {a, b};
            nums.push_back(a);
            nums.push_back(b);
        }
        nums.push_back(-2e9);
        sort(nums.begin(), nums.end());
        nums.erase(unique(nums.begin(), nums.end()), nums.end());
        for(int i = 1; i <= n; i ++) {
            int a = find(segs[i].first), b = find(segs[i].second);
            add(a, b, 1, segs[i].first - segs[i].second);
        }
        int len = nums.size();
        for(int i = 2; i < len; i ++) {
            add(i - 1, i, inf, 0);
        }
        add(S, 1, k, 0);
        add(len - 1, T, k, 0);
        printf("%d
    ", -EK());
        return 0;
    }
    
  • 相关阅读:
    SqlDataAdapter 类
    新博开
    gcc编译C++程序

    抖动
    css3 导航效果
    javascript判断IE浏览器的版本
    CSS样式表继承详解
    css选择器
    鼠标样式
  • 原文地址:https://www.cnblogs.com/miraclepbc/p/14417887.html
Copyright © 2011-2022 走看看