poj 3680 Intervals(最大费用流+离散化)

46 篇文章 0 订阅
Intervals
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 6049 Accepted: 2434

Description

You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task is to pick some of the intervals to maximize the total weights under the limit that no point in the real axis is covered more than k times.

Input

The first line of input is the number of test case.
The first line of each test case contains two integers, N and K (1 ≤ KN ≤ 200).
The next N line each contain three integers ai, bi, wi(1 ≤ ai < bi ≤ 100,000, 1 ≤ wi ≤ 100,000) describing the intervals.
There is a blank line before each test case.

Output

For each test case output the maximum total weights in a separate line.

Sample Input

4

3 1
1 2 2
2 3 4
3 4 8

3 1
1 3 2
2 3 4
3 4 8

3 1
1 100000 100000
1 2 3
100 200 300

3 2
1 100000 100000
1 150 301
100 200 300

Sample Output

14
12
100000
100301
 
 
题意:给出n个区间[ai,bi],wi,表示如果取这个区间i,则能获得权值wi,现在有一个限制,就是取若干个区间后,所有取到的点的重复次数不能超过k,且要使最后获得的权值最大,求最后获得的权值。
思路:先说说如何构图:
先将区间的端点离散化(无视区间内的点),设离散化后共得到cnt个点,设超级源s=0,超级汇t=cnt+1,从s到cnt,每个点按顺序向后一个点连边,费用为0,容量为k。
对于每个区间端点ai、bi,取离散化后的标号hash[ai]、hash[bi],连一条边,费用为该区间权值,流量为1。
最后求最大费用流即可。
为什么这样构图可以求解呢?
这样构图以后,若两个区间有重叠的部分,那么如果两个区间都取,就肯定需要两条增广路,这样的话重叠部分的点重复次数就是2,一开始设置源点到点1的流量为k,就限制了点重复次数最多为k了。

AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <stack>
#include <ctime>
#include <algorithm>
#define ll __int64

using namespace std;

const int INF = 1000000000;
const int maxn = 200000;

struct node
{
    int a, b, w;
}inter[205];
struct Edge{
    int u, v, cost, cap, flow, next;
}et[maxn * 10];
int low[maxn], pre[maxn], dis[maxn], eh[maxn], hash[maxn];
bool vis[maxn];
int s, t, num, anscost, n, k;
void init(){
    memset(eh, -1, sizeof(eh));
    num = 0;
}
void add(int u, int v, int cost, int cap, int flow){
    Edge e = {u, v, cost, cap, flow, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, int cost, int cap){
    add(u, v, cost, cap, 0);
    add(v, u, -cost, 0, 0);
}
bool spfa(){
    int Q[maxn], first = 0, tail = 1;
    for(int i = s; i <= t; i++)
    {
        pre[i] = -1;
        low[i] = 0;
        dis[i] = -INF;
        vis[i] = false;
    }
    dis[s] = 0, low[s] = INF, vis[s] = true;
    Q[0] = s;
    while(first != tail)
    {
        int u = Q[first++];
        vis[u] = false;
        for(int i = eh[u]; i != -1; i = et[i].next)
        {
            int v = et[i].v, cost = et[i].cost, cap = et[i].cap, flow = et[i].flow;
            if(cap - flow && dis[v] < dis[u] + cost)
            {
                dis[v] = dis[u] + cost;
                pre[v] = i;
                low[v] = min(low[u], cap - flow);
                if(!vis[v])
                {
                    vis[v] = true;
                    Q[tail++] = v;
                }
            }
        }
    }
    return dis[t] != -INF;
}
void costflow(){
    anscost = 0;
    while(spfa())
    {
        int x = pre[t];
        anscost += low[t] * dis[t];
        while(x != -1)
        {
            et[x].flow += low[t];
            et[x^1].flow -= low[t];
            x = pre[et[x].u];
        }
    }
}

int main()
{
    int tt;
    scanf("%d", &tt);
    while(tt--)
    {
        scanf("%d%d", &n, &k);
        memset(hash, 0, sizeof(hash));
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d%d", &inter[i].a, &inter[i].b, &inter[i].w);
            hash[inter[i].a] = hash[inter[i].b] = 1;
        }
        int cnt = 0;
        for(int i = 1; i <= 100000; i++)
        if(hash[i] > 0) hash[i] = ++cnt;
        s = 0, t = cnt + 1;
        init();
        for(int i = 0; i <= cnt; i++) addedge(i, i + 1, 0, k);
        for(int i = 0; i < n; i++) addedge(hash[inter[i].a], hash[inter[i].b], inter[i].w, 1);
        costflow();
        printf("%d\n", anscost);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值