luogu P3357 最长k可重线段集问题

Posted gredcomet

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了luogu P3357 最长k可重线段集问题相关的知识,希望对你有一定的参考价值。

这题和3358一模一样,建模形式直接不用变,就两点不一样,一是len变化了,加入y后再更新即可,还有就是可能会出现x0=x1的情况,即一条开线段垂直x轴,如果我们依旧按照上一题的建图方法,就会出现负权环,无法跑出答案,我们就可以把一个点拆成入点和出点,这样无论是否是不是垂直都可以一样建,注意开long long,不开long long可能只有9分

技术图片
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
#define sqr(x) ((x)*(x))
typedef long long LL;

const int maxm = 1e5+5;
const LL INF = 0x3f3f3f3f3f3f3f3f;

struct edge{
    LL u, v, cap, flow, cost, nex;
} edges[maxm];

struct Points{
    LL l, r, len;
} point[505];

LL head[maxm], cur[maxm], cnt, fa[1024<<1], n, d[1024<<1], allx[1024];
bool inq[1024<<1];

void init() {
    memset(head, -1, sizeof(head));
}

void add(int u, int v, LL cap, LL cost) {
    edges[cnt] = edge{u, v, cap, 0, cost, head[u]};
    head[u] = cnt++;
}

void addedge(int u, int v, LL cap, LL cost) {
    add(u, v, cap, cost), add(v, u, 0, -cost);
}

bool spfa(int s, int t, int &flow, LL &cost) {
    for(int i = 0; i <= n+2; ++i) d[i] = INF; //init()
    memset(inq, false, sizeof(inq));
    d[s] = 0, inq[s] = true;
    fa[s] = -1, cur[s] = INF;
    queue<int> q;
    q.push(s);
    while(!q.empty()) {
        int u = q.front();
        q.pop();
        inq[u] = false;
        for(int i = head[u]; i != -1; i = edges[i].nex) {
            edge& now = edges[i];
            int v = now.v;
            if(now.cap > now.flow && d[v] > d[u] + now.cost) {
                d[v] = d[u] + now.cost;
                fa[v] = i;
                cur[v] = min(cur[u], now.cap - now.flow);
                if(!inq[v]) {q.push(v); inq[v] = true;}
            }
        }
    }
    if(d[t] == INF) return false;
    flow += cur[t];
    cost += 1LL*d[t]*cur[t];
    for(int u = t; u != s; u = edges[fa[u]].u) {
        edges[fa[u]].flow += cur[t];
        edges[fa[u]^1].flow -= cur[t];
    }
    return true;
}

int MincostMaxflow(int s, int t, LL &cost) {
    cost = 0;
    int flow = 0;
    while(spfa(s, t, flow, cost));
    return flow;
}

void run_case() {
    init();
    LL l, r, y1, y2;
    int k, xcnt = 0;
    cin >> n >> k;
    for(int i = 1; i <= n; ++i) {
        cin >> l >> y1 >> r >> y2;
        LL tmp = 1LL*floor(sqrt(sqr(r-l)+sqr(y2-y1)));
        if(l > r) swap(l, r);
        l <<= 1, r <<= 1;
        if(l == r) r|=1; else l|=1;
        allx[++xcnt] = l, allx[++xcnt] = r, point[i] = Points{l, r, tmp};
    }
    sort(allx+1,allx+1+xcnt);
    int len = unique(allx+1,allx+1+xcnt)-allx;
    for(int i = 1; i <= n; ++i) {
        point[i].l = lower_bound(allx+1,allx+len,point[i].l)-allx;
        point[i].r = lower_bound(allx+1,allx+len,point[i].r)-allx;
    }
    for(int i = 1; i < len-1; ++i)
        addedge(i, i+1, INF, 0);
    int s = 0, t = len;
    for(int i = 1; i <= n; ++i) {
        addedge(point[i].l, point[i].r, 1, -point[i].len);
    }
    addedge(s, 1, k, 0), addedge(len-1, t, k, 0);
    LL cost = 0;
    n = len;
    MincostMaxflow(s, t, cost);
    cout << -cost;
}

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    run_case();
    cout.flush();
    return 0;
}
View Code

 

以上是关于luogu P3357 最长k可重线段集问题的主要内容,如果未能解决你的问题,请参考以下文章

最长k可重区间集

最长k可重线段集问题

最长k可重线段集问题

网络流24题22最长k可重线段集问题

luogu P3358 最长k可重区间集问题

*LOJ#6227. 「网络流 24 题」最长k可重线段集问题