POJ3680 Intervals

Intervals
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 8343 Accepted: 3562

Description

You are given N weighted open intervals. The ith interval covers (aibi) 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 ≤ K ≤ N ≤ 200).
The next N line each contain three integers aibiwi(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

Source


—————————————————————————————————

题目大意:给你n个区间(ai,bi),对应区间的权重为ci。现在让你挑选一些区间,使得任一点出现的次数不超过k,问你满足条件的方案的最大权重和为多少?

思路:最小费用最大流,先拿源点和每个左端点连边,流量为1.费用为0,汇点和右端点连边,流量为1,费用为0.区间左端点和右端点连边费用为-价值,流量为1,然后n2判区间是否重叠,不重叠就连一条费用0,流量无穷的边

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 100100
#define MAXM 1000100

int vis[MAXN],d[MAXN],pre[MAXN],aa[MAXN];

struct Edge
{
    int u, v, c, cost, next;
} edge[MAXM];
int s[MAXN], cnt;

void init()
{
    cnt = 0;
    memset(s, -1, sizeof(s));
}

void add(int u, int v, int c, int cost)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].cost = cost;
    edge[cnt].c = c;
    edge[cnt].next = s[u];
    s[u] = cnt++;
    edge[cnt].u = v;
    edge[cnt].v = u;
    edge[cnt].cost = -cost;
    edge[cnt].c = 0;
    edge[cnt].next = s[v];
    s[v] = cnt++;
}

bool spfa(int ss, int ee,int &flow,int &cost)
{
    queue<int> q;
    memset(d, INF, sizeof d);
    memset(vis, 0, sizeof vis);
    d[ss] = 0, vis[ss] = 1, pre[ss] = 0, aa[ss] = INF;
    q.push(ss);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for (int i = s[u]; ~i; i = edge[i].next)
        {
            int v = edge[i].v;
            if (edge[i].c>0&& d[v]>d[u] + edge[i].cost)
            {
                d[v] = d[u] + edge[i].cost;
                pre[v] = i;
               aa[v] = min(aa[u], edge[i].c);
                if (!vis[v])
                {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    if (d[ee] == INF) return 0;
    flow += aa[ee];
    cost += d[ee]*aa[ee];
    int u = ee;
    while (u != ss)
    {
        edge[pre[u]].c -= aa[ee];
        edge[pre[u] ^ 1].c += aa[ee];
        u = edge[pre[u]].u;
    }
    return 1;
}

int MCMF(int ss, int ee)
{
    int cost = 0, flow=0;
    while (spfa(ss, ee, flow, cost));

    return cost;
}


int a[300],b[300];
int main()
{
    int T,m,n,k,u,v,c;
    for(scanf("%d",&T);T--;)
    {
        init();
        scanf("%d%d",&n,&k);
        add(100001,100002,k,0);
        add(100003,100004,k,0);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&a[i],&b[i],&c);
            add(a[i],b[i],1,-c);
            add(100002,a[i],1,0);
            add(b[i],100003,1,0);
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
        {
            if(b[i]<a[j])
                add(b[i],a[j],1,0);
        }
        printf("%d\n",-MCMF(100001,100004));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值