给你若干个区间,每个区间有一个权值,你可以选出某些区间,使得在保证没有任何一段的覆盖次数超过k的前提下,总的权值最大。

这个建模真的十分神奇,赞一个。

对于给出的每一个区间,离散化,最终我们可以知道所有区间的端点的个数不会超过2n,然后我们加边,(i,i+1,k,0),对于每个区间我们加边(li,ri,1,-wi)。

这样我们跑出最小费用,就是答案了,仔细理解一下就知道了,这就是题目的等价模型,保证了流量的限制。

召唤代码君:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define maxn 422
#define maxm 84444
using namespace std; int to[maxm],cap[maxm],cost[maxm],next[maxm],first[maxn],edge;
int d[maxn],num[maxn],from[maxn],tag[maxn],TAG=;
int Q[maxm],bot,top;
int a[maxm],u[maxn],v[maxn],w[maxn];
int n,m,N,K,T,s,t,ans; void _init()
{
s=,t=n+,ans=,edge=-;
for (int i=s; i<=t; i++) first[i]=-;
} void addedge(int U,int V,int W,int C)
{
edge++;
to[edge]=V,cap[edge]=W,cost[edge]=C,next[edge]=first[U],first[U]=edge;
edge++;
to[edge]=U,cap[edge]=,cost[edge]=-C,next[edge]=first[V],first[V]=edge;
} bool bfs()
{
Q[bot=top=]=s,tag[s]=++TAG,d[s]=,num[s]=K,from[s]=-;
while (bot<=top){
int cur=Q[bot++];
for (int i=first[cur]; i!=-; i=next[i])
if (cap[i]> && (tag[to[i]]!=TAG || d[cur]+cost[i]<d[to[i]])){
tag[to[i]]=TAG;
num[to[i]]=min(num[cur],cap[i]);
d[to[i]]=d[cur]+cost[i];
from[to[i]]=i;
Q[++top]=to[i];
}
}
if (tag[t]!=TAG || d[t]>=) return false;
ans+=num[t]*d[t];
for (int i=t; from[i]!=-; i=to[from[i]^])
cap[from[i]]-=num[t],cap[from[i]^]+=num[t];
return true;
} int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&N,&K);
for (int i=; i<N; i++){
scanf("%d%d%d",&u[i],&v[i],&w[i]);
a[i]=u[i],a[i+N]=v[i];
}
sort(a,a+N+N);
n=unique(a,a+N+N)-a;
for (int i=; i<N; i++){
u[i]=lower_bound(a,a+n,u[i])-a+;
v[i]=lower_bound(a,a+n,v[i])-a+;
}
_init();
for (int i=; i<=n; i++) addedge(i,i+,K,);
for (int i=; i<N; i++) addedge(u[i],v[i],,-w[i]);
while (bfs()) ;
printf("%d\n",-ans);
}
return ;
}

POJ3680_Intervals的更多相关文章

随机推荐

  1. youtube不显示其他人头像

    我用的是ss , 右键ss选择pac->编辑本地pac 把ggpht.com加入pac列表即可

  2. c#深拷贝

    /// <summary> /// 对象拷贝 /// </summary> /// <param name="obj">被复制对象</pa ...

  3. python 简单爬虫diy

    简单爬虫直接diy, 复杂的用scrapy import urllib2 import re from bs4 import BeautifulSoap req = urllib2.Request(u ...

  4. [HTML] CSS3 文本效果

    CSS3 文本效果 CSS3中包含几个新的文本特征. 在本章中您将了解以下文本属性: text-shadow word-wrap 浏览器支持

  5. sql查询指定范围内的所有月份

    ),) FROM master..spt_values WHERE type='P' AND DATEADD(MONTH,number,'2016-01-01')<='2017-01-01'

  6. Flex帮助文档ASDoc

    首先,我们一般会对类文件的类和成员以及成员函数做一些解析性说明.那么这个解析性说明应该怎么写呢?如果想给指定的类.成员属性.成员函数加上注释,可以在这些声明的顶部按照下面的格式属性注释: (在flas ...

  7. 24. Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  8. mysql 不是主键不能删除的保护问题解决办法?

       select * from t_answerexams;      delete from  t_answerexams  where  selectid = 'c4582502-8b27-44 ...

  9. [电脑常见问题] win8 ie浏览器打不开

    我安装的是win8专业版,正版的已经激活了,突然IE浏览器就打不开了,在桌面里面点IE没反应,在Metro界面点IE就回到开始界面 解决办法: 1.Win+R呼出运行窗口,键入Regedit,回车,打 ...

  10. PIC32MZ tutorial -- Hello World

    Today I implement "Hello World" on PIC32MZ EC starter kit. The application of "Hello ...