POJ 3680 Intervals(区间覆盖建图)

87 篇文章 0 订阅

Intervals
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 8549 Accepted: 3641
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 ≤ K ≤ N ≤ 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
Source

POJ Founder Monthly Contest – 2008.07.27, windy7926778

题目大意:

  有 N 个带权开区间,让你从中选择一些使得权值最大,实数轴上任意一点被覆盖不超过K次,输出最大权值和。

解题思路:

  经典的在区间上建图题目。首先把所有端点离散化编号从1 n ,建立源点0,汇点 n+1 。对于除汇点之外的点建立从 i i+1的边,容量为K,费用为0。对于每个区间 (l,r) ,建立从 l r 的边,容量为 1 ,费用为wi。跑一遍最小费用最大流,结果的相反数即为答案。

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define fi first
#define se second
#define sqr(x) ((x)*(x))

const int MAXN=200+3;
const int MAXV=MAXN*2;
const int MAXE=MAXV*4;

struct Edge
{
    int to,next,cap,cost;
    Edge(int t=0,int n=0,int ca=0,int f=0,int co=0):to(t),next(n),cap(ca),cost(co){}
}edge[MAXE];

int N, K, l[MAXN], r[MAXN], w[MAXN];
vector<int> save;
int head[MAXV],tol;
int pre[MAXV],dis[MAXV];
bool vis[MAXV];
int V;//节点总个数,节点编号从0~V-1

void add_edge(int u,int v,int cap,int cost)
{
    edge[tol]=Edge(v,head[u],cap,0,cost);
    head[u]=tol++;
    edge[tol]=Edge(u,head[v],0,0,-cost);
    head[v]=tol++;
}

bool spfa(int s,int t)
{
    queue<int> que;
    for(int i=0;i<V;++i)
    {
        dis[i]=INF;
        vis[i]=false;
        pre[i]=-1;
    }
    dis[s]=0;
    vis[s]=true;
    que.push(s);
    while(!que.empty())
    {
        int u=que.front(); que.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap>0&&dis[v]>dis[u]+edge[i].cost)
            {
                dis[v]=dis[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v])
                {
                    vis[v]=true;
                    que.push(v);
                }
            }
        }
    }
    return pre[t]!=-1;
}

int min_cost_flow(int s,int t,int &cost)//返回的是最大流,cost存的是最小费用
{
    int flow=0;
    cost=0;
    while(spfa(s,t))
    {
        int the_min=INF;
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
            the_min=min(the_min,edge[i].cap);
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
        {
            edge[i].cap-=the_min;
            edge[i^1].cap+=the_min;
            cost+=edge[i].cost*the_min;
        }
        flow+=the_min;
    }
    return flow;
}


void init()
{
    save.clear();
    tol=0;
}

int main()
{
    int T_T;
    scanf("%d", &T_T);
    while(T_T--)
    {
        scanf("%d%d", &N, &K);
        init();
        for(int i=0;i<N;++i)
        {
            scanf("%d%d%d", &l[i], &r[i], &w[i]);
            save.push_back(l[i]);
            save.push_back(r[i]);
        }
        sort(save.begin(), save.end());
        save.erase(unique(save.begin(), save.end()), save.end());
        for(int i=0;i<N;++i)
        {
            l[i]=lower_bound(save.begin(), save.end(), l[i])-save.begin()+1;
            r[i]=lower_bound(save.begin(), save.end(), r[i])-save.begin()+1;
        }
        V=save.size()+2;
        for(int i=0;i<V;++i)
            head[i]=-1;
        int s=0, t=V-1;
        for(int i=0;i<V-1;++i)
            add_edge(i, i+1, K, 0);
        for(int i=0;i<N;++i)
        {
            add_edge(l[i], r[i], 1, -w[i]);
        }
        int ans;
        min_cost_flow(s, t, ans);
        printf("%d\n", -ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值