题意:给出 n ,k 表示接下来给你 n 段开区间,每段区间都有它的权值,问选出一些区间,使它的权值最大,并且在实轴上的每个点,不得超过 k次被覆盖。

思路:首先要理解建图思路,首先有一个基图,相邻点之间都有边,花费为0,容量为 k,然后再根据所给数据 u,v, w 建图,u,v,加一条容量为 1,花费为 - w的边,求最大费用最大流,只需将权值变为负数即可求出最短路,相反数即为最大值。

最小费用最大流,就是用SPFA寻找最短路(增广路容量是否大于流量,花费最短路),然后用最大流方法去计算最大流量(容量限制)

理解一下最后画的图应该就懂了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define N 220
int num[N],inf=0x3f3f3f3f;
struct node
{
int to,nextt;//下一个点 邻接表
int cap,flow,cost;//容量 流量 花费
} e[N*N]; //邻接表的next
bool book[N*2];
int a[N*2+5],tot,n,k,s,t,id[100010],l[N],r[N],w[N],dis[N*2],way[N*4],head[N*4];//最短距离 记录路径
/*邻接表存图*/
void add(int u,int v,int cap,int cost)
{
e[tot].to=v;
e[tot].cap=cap;
e[tot].cost=cost;
e[tot].flow=0;
e[tot].nextt=head[u];
head[u]=tot++; e[tot].to=u;
e[tot].cost=-1*cost;
e[tot].flow=0;
e[tot].cap=0;
e[tot].nextt=head[v];//正反建图
head[v]=tot++;
}
bool spfa()
{
// 寻找0~t的最短路
for(int i=0;i<=t;i++)
{
dis[i]=inf;
book[i]=0;
way[i]=-1;
}
book[s]=1;
dis[s]=0;
queue<int>q;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
book[u]=0;
for(int j=head[u];j!=-1;j=e[j].nextt)
{
int v=e[j].to;
if(e[j].cap>e[j].flow&&dis[v]>dis[u]+e[j].cost)//附加条件:当容量大于流量
{
dis[v]=dis[u]+e[j].cost;
way[v]=j;//记录边?
if(!book[v])
{
book[v]=1;
q.push(v);
}
}
}
}
if(dis[t]!=inf)//存在增广路可以到达 t
return 1;
return 0;
}
int MCMF()
{
int flow=0,cost=0;
while(spfa())
{
int minn=inf;
for(int i=way[t];i!=-1;i=way[e[i^1].to])
minn=min(minn,e[i].cap-e[i].flow);//找出最小花费
for(int i=way[t];i!=-1;i=way[e[i^1].to])
{
e[i].flow+=minn;
e[i^1].flow-=minn;//异或小技巧;1->2 2>1正反边
cost+=e[i].cost*minn;
}
flow+=minn;//计算最大流量?好像没用
}
return cost;
}
int main()
{
int z;
scanf("%d",&z);
while(z--)
{
scanf("%d%d",&n,&k);
int p=0;
memset(head,-1,sizeof(head));
for(int i=1; i<=n; i++)
{
scanf("%d%d%d",&l[i],&r[i],&w[i]);
a[++p]=l[i];
a[++p]=r[i];
}
sort(a+1,a+p+1);
tot=1;
/*重新编号 去重貌似有函数*/
id[a[1]]=tot++;
for(int i=2; i<=p; i++)
{
if(a[i]!=a[i-1])
id[a[i]]=tot++;
}
t=tot;
s=0;//起始点
// printf("%d %d\n",s,t);
// for(int i=1;i<=n;i++)
// printf("%d %d\n",id[l[i]],id[r[i]]);
tot=0;
for(int i=0;i<=t;i++)
add(i,i+1,k,0);
for(int i=1;i<=n;i++)
add(id[l[i]],id[r[i]],1,-w[i]);//为什么是负权
printf("%d\n",MCMF()*-1);
}
return 0;
}

最新文章

  1. JavaScript学习笔记(1))——————call,apply方法
  2. JS 生成GUID 方法
  3. HttpClient 与 HtmlParser 简介 转载
  4. NSOperation实现线程间通信
  5. Aspen 安装
  6. PHP中cookie和Session
  7. 解决MySQL中【Cannot load from mysql.proc. The table is probably corrupted
  8. 好记心不如烂笔头,ssh登录 The authenticity of host 192.168.0.xxx can&#39;t be established. 的问题
  9. RGB Bayer Color分析
  10. Hibernate(七)多对一单向关联映射
  11. curl 网页抓取
  12. 预处理指令中#Pragma
  13. jQuery插件Flot实战Demo
  14. cut命令及参数企业案列讲解及awk对比
  15. macbook配置xdebug+vscode
  16. Django创建项目基本步骤
  17. what&#39;s the 回撤
  18. MySQL可以通过phpmyadmin连接,但是无法通过SqlYog(Windows)或Sequel Pro(Mac)下进行远程连接
  19. 【Python】插入sqlite数据库
  20. ConcurrentHashMap内存溢出问题

热门文章

  1. java单链表的实现自己动手写一个单链表
  2. C#开发BIMFACE系列30 服务端API之模型对比1:发起模型对比
  3. 状压dp 持续更新
  4. Fetch API与POST请求那些事
  5. JavaScript对象(一)
  6. 添加bash命令
  7. MATLAB神经网络(5) 基于BP_Adaboost的强分类器设计——公司财务预警建模
  8. Redis集群搭建及选举原理
  9. echart图表中legend不显示问题
  10. 5分钟使用NetModular 完成通讯录 App 开发