poj3680 Intervals

177 篇文章 0 订阅
125 篇文章 0 订阅

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.

看成进行 k 次选择,每次选取的区间不相交。
对端点离散化,s 1 m t 连容量为k的边, x x+1连容量为 的边,每个区间从起点到终点连容量为 1 、费用为w的边。每一条从 s t的容量为 1 <script type="math/tex" id="MathJax-Element-675">1</script>的路径代表一次选择。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int s=505,t=506,oo=0x3f3f3f3f,mod=503;
int a[210],b[210],v[210],ord[410],newa[210],newb[210],
fir[510],ne[50010],to[50010],w[50010],c[50010],fa[510],
que[510],in[510],len[510],minw[510],
n,m,num,k;
void add(int u,int v,int x,int y)
{
    num++;
    ne[num*2]=fir[u];
    fir[u]=num*2;
    to[num*2]=v;
    w[num*2]=x;
    c[num*2]=y;
    ne[num*2+1]=fir[v];
    fir[v]=num*2+1;
    to[num*2+1]=u;
    w[num*2+1]=0;
    c[num*2+1]=-y;
}
void init()
{
    scanf("%d%d",&n,&k);
    m=num=0;
    for (int i=1;i<=n;i++)
    {
        scanf("%d%d%d",&a[i],&b[i],&v[i]);
        ord[++m]=a[i];
        ord[++m]=b[i];
    }
    sort(ord+1,ord+m+1);
    m=unique(ord+1,ord+m+1)-ord-1;
    for (int i=1;i<=n;i++)
    {
        newa[i]=lower_bound(ord+1,ord+m+1,a[i])-ord;
        newb[i]=lower_bound(ord+1,ord+m+1,b[i])-ord;
    }
    memset(fir,0,sizeof(fir));
    for (int i=1;i<m;i++) add(i,i+1,oo,0);
    add(s,1,k,0);
    add(m,t,k,0);
    for (int i=1;i<=n;i++)
        add(newa[i],newb[i],1,-v[i]);
}
void check()
{
    for (int i=0;i<=t;i++)
        for (int j=fir[i];j;j=ne[j])
            if (w[j]) printf("%d->%d:%d,%d\n",i,to[j],w[j],c[j]);
}
bool spfa()
{
    /*check();
    printf("\n\n\n");*/
    int u,v,hd=0,tl=1;
    memset(len,0x3f,sizeof(len));
    len[s]=0;
    memset(minw,0,sizeof(minw));
    minw[s]=oo;
    que[0]=s;
    in[s]=1;
    while (hd!=tl)
    {
        u=que[hd++];
        hd%=mod;
        for (int i=fir[u];i;i=ne[i])
            if (w[i]&&len[v=to[i]]>len[u]+c[i])
            {
                len[v]=len[u]+c[i];
                minw[v]=min(minw[u],w[i]);
                fa[v]=i;
                if (!in[v])
                {
                    in[v]=1;
                    que[tl++]=v;
                    tl%=mod;
                }
            }
        in[u]=0;
    }
    return minw[t];
}
int find()
{
    for (int i=fa[t];i;i=fa[to[i^1]])
    {
        w[i]-=minw[t];
        w[i^1]+=minw[t];
    }
    return minw[t]*len[t];
}
int main()
{
    int T,ans;
    scanf("%d",&T);
    while (T--)
    {
        init();
        ans=0;
        while (spfa()) ans-=find();
        printf("%d\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值