POJ3680 Intervals

嘟嘟嘟


这题我没想出来……


刚开始我是想把连续的一段看成一个点,这样最多有\(2n - 1\)个点,复杂度可以接受,然后把离散化后的点都想汇点连边。从源点向每一个限制连边,每一个限制向对应的区间中的所有点连边。这个图看似挺好,但是他必须满足每一个限制的出边全流满。这显然是不可能的,因为网络流可以增广的条件是只要找到一条增广路就行。


这题正解还是很妙的。
我们先把端点离散化(不用管区间开闭的事)。然后首尾相连穿成一串,每条边都是流量为\(k\),费用为0,然后再把到源汇点的边补上。
对于每一个限制,从\(L\)\(R\)连一条流量为1,费用为\(w\)的边。然后跑最大费用流即可。(费用取负跑最小费用流)


这个方法确实很妙,而且完美的解决了开区间的问题,我估计没做过应该是想不出来吧……

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 205;
const int maxe = 1e5 + 5;
inline ll read()
{
    ll ans = 0;
    char ch = getchar(), last = ' ';
    while(!isdigit(ch)) last = ch, ch = getchar();
    while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
    if(last == '-') ans = -ans;
    return ans;
}
inline void write(ll x)
{
    if(x < 0) x = -x, putchar('-');
    if(x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}

int n, K, t;
int di[maxn << 1], cnt = 0;
struct Node
{
    int L, R, w;
}q[maxn];
struct Edge
{
    int nxt, from, to, cap, cos;
}e[maxe];
int head[maxn << 1], ecnt = -1;
In void addEdge(int x, int y, int w, int c)
{
    e[++ecnt] = (Edge){head[x], x, y, w, c};
    head[x] = ecnt;
    e[++ecnt] = (Edge){head[y], y, x, 0, -c};
    head[y] = ecnt;
}

bool in[maxn << 1];
int dis[maxn << 1], pre[maxn << 1], flow[maxn << 1];
In bool spfa()
{
    Mem(dis, 0x3f), Mem(in, 0);
    dis[0] = 0, flow[0] = INF;
    queue<int> q; q.push(0);
    while(!q.empty())
    {
        int now = q.front(); q.pop(); in[now] = 0;
        for(int i = head[now], v; ~i; i = e[i].nxt)
        {
            if(dis[v = e[i].to] > dis[now] + e[i].cos && e[i].cap > 0)
            {
                dis[v] = dis[now] + e[i].cos;
                pre[v] = i;
                flow[v] = min(flow[now], e[i].cap);
                if(!in[v]) q.push(v), in[v] = 1;
            }
        }
    }
    return dis[t] != INF;
}
int minCost = 0;
In void update()
{
    int x = t;
    while(x)
    {
        int i = pre[x];
        e[i].cap -= flow[t];
        e[i ^ 1].cap += flow[t];
        x = e[i].from;
    }
    minCost += dis[t] * flow[t];
}

In int MCMF()
{
    minCost = 0;
    while(spfa()) update();
    return minCost;
}

In void init()
{
    Mem(head, -1), ecnt = -1;
    cnt = 0;
}

int main()
{
    int T = read();
    while(T--)
    {
        init();
        n = read(), K = read(); t = n + n + 1;
        for(int i = 1; i <= n; ++i)
        {
            int L = read(), R = read(), w = read();
            q[i] = (Node){L, R, w};
            di[++cnt] = q[i].L, di[++cnt] = q[i].R;
        }
        sort(di + 1, di + cnt + 1);
        int _n = unique(di + 1, di + cnt + 1) - di - 1;
        for(int i = 1; i <= _n; ++i) addEdge(i - 1, i, K, 0);
        addEdge(_n, t, K, 0);
        for(int i = 1; i <= n; ++i)
        {
            int L = lower_bound(di + 1, di + _n + 1, q[i].L) - di;
            int R = lower_bound(di + 1, di + _n + 1, q[i].R) - di;
            addEdge(L, R, 1, -q[i].w);
        }
        write(-MCMF()), enter;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/mrclr/p/10801574.html
今日推荐