给你一些区间,每个区间都有些价值。取一个区间就能获得对应的价值,并且一个点不能覆盖超过k次,问你最大的价值是多少。

我们可以把这些区间放到一维的轴上去,然后我们可以把它看成一个需要从左到右的过程,然后这个过程中保证每个点不超过k次,并且获得的价值最大。

因为一个点不超过k次,只需要控制流入的最大流量是k,就可以保证每个点的流量都不会超过k。

建图过程:

1.超源到整个区间最小的点, 流量k, 费用0。

2.整个区间内每个点(除了最后一个点)到自己的下一个点,流量inf,费用0。

3.每个区间的左端到右端,流量1,费用就是-价值。

4.整个区间的最后一个点到超汇,流量k, 费用0.

这样其实就是如果我极端情况,所有区间都经过了某一点,但是我的流量只有k,所以我不会让这个点被覆盖k次以上。

如果每个区间都流完了还有多余的流量,我就会从第2条路流下去,然后保证他不会多多余的费用出现。

#include<map>
#include<set>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lowbit(x) (x & (-x)) typedef unsigned long long int ull;
typedef long long int ll;
const double pi = 4.0*atan(1.0);
const int inf = 0x3f3f3f3f;
const int maxn = ;
const int maxm = ;
const int mod = ;
using namespace std; int n, m, tol, T;
struct Node {
int u;
int v;
int w;
int val;
int next;
};
Node node[maxn];
struct Edge{
int l;
int r;
int w;
bool operator< (Edge a) const {
return l < a.l;
}
};
Edge edge[maxn];
int head[maxn];
int cap[maxn];
int dis[maxn];
int pre[maxn];
bool vis[maxn];
int a[maxn]; void init() {
tol = ;
memset(a, , sizeof a);
memset(node, , sizeof node);
memset(head, -, sizeof head);
} void addnode(int u, int v, int w, int val) {
node[tol].u = u;
node[tol].v = v;
node[tol].w = w;
node[tol].val = val;
node[tol].next = head[u];
head[u] = tol++;
} bool spfa(int src, int des, int &flow, int &cost) {
memset(pre, , sizeof pre);
memset(dis, inf, sizeof dis);
memset(cap, inf, sizeof cap);
memset(vis, false, sizeof vis);
queue<int > q;
pre[src] = src;
vis[src] = true;
dis[src] = ;
cap[src] = inf;
q.push(src);
while(!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for(int i=head[u]; ~i; i=node[i].next) {
int v = node[i].v;
if(node[i].w && dis[v] > dis[u] + node[i].val) {
dis[v] = dis[u] + node[i].val;
cap[v] = min(cap[u], node[i].w);
pre[v] = i;
if(!vis[v]) {
vis[v] = true;
q.push(v);
}
}
}
}
if(dis[des] == inf) return false;
flow += cap[des];
cost += cap[des] * dis[des];
int u = des;
while(u != src) {
node[pre[u]].w -= cap[des];
node[pre[u]^].w += cap[des];
u = node[pre[u]].u;
}
return true;
} int MCMF(int src, int des) {
int flow = ;
int cost = ;
while(spfa(src, des, flow, cost));
return cost;
} int main() {
scanf("%d", &T);
while(T--) {
init();
scanf("%d%d", &n, &m);
int nn = ;
for(int i=; i<=n; i++) {
scanf("%d%d%d", &edge[i].l, &edge[i].r, &edge[i].w);
a[++nn] = edge[i].l;
a[++nn] = edge[i].r;
}
sort(edge+, edge++n);
sort(a+, a++nn);
nn = unique(a+, a++nn) - (a+);
int mi = inf, ma = -inf;
for(int i=; i<=n; i++) {
edge[i].l = lower_bound(a+, a++nn, edge[i].l) - a;
mi = min(mi, edge[i].l);
ma = max(ma, edge[i].l);
edge[i].r = lower_bound(a+, a++nn, edge[i].r) - a;
mi = min(mi, edge[i].r);
ma = max(ma, edge[i].r);
}
int src = mi-;
int des = ma+;
addnode(src, mi, m, );
addnode(mi, src, , );
addnode(ma, des, m, );
addnode(des, ma, , );
for(int i=mi; i<ma; i++) {
addnode(i, i+, inf, );
addnode(i+, i, , );
}
for(int i=; i<=n; i++) {
addnode(edge[i].l, edge[i].r, , -edge[i].w);
addnode(edge[i].r, edge[i].l, , edge[i].w);
}
/*
for(int i=0; i<tol; i++) {
printf("%d %d %d %d\n", node[i].u, node[i].v, node[i].w, node[i].val);
}
*/
int ans = -MCMF(src, des);
printf("%d\n", ans);
}
return ;
}

Intervals POJ - 3680 (MCMF)的更多相关文章

  1. 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

  2. 2014湘潭全国邀请赛I题 Intervals /POJ 3680 / 在限制次数下取有权区间使权最大/小问题(费用流)

    先说POJ3680:给n个有权(权<10w)开区间(n<200),(区间最多数到10w)保证数轴上所有数最多被覆盖k次的情况下要求总权最大,输出最大权. 思路:       限制的处理:s ...

  3. POJ题目(转)

    http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj29 ...

  4. Repeater POJ - 3768 (分形)

    Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...

  5. Booksort POJ - 3460 (IDA*)

    Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...

  6. Radar Installation POJ - 1328(贪心)

    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...

  7. Best Cow Fences POJ - 2018 (二分)

    Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains ...

  8. E - The Balance POJ - 2142 (欧几里德)

    题意:有两种砝码m1, m2和一个物体G,m1的个数x1,  m2的个数为x2, 问令x1+x2最小,并且将天平保持平衡 !输出  x1 和 x2 题解:这是欧几里德拓展的一个应用,欧几里德求不定方程 ...

  9. poj 1220(短除法)

    http://poj.org/problem?id=1220 题意:进制转换,把a进制转换为b进制. 如果数据不大的话,那么这个题还是很简单的,但这个题就是数据范围太大,所以这里可以采用短除法来做. ...

随机推荐

  1. #Leetcode# 1016. Binary String With Substrings Representing 1 To N

    https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/ Given a binary stri ...

  2. python文件封装成*.exe文件(单文件和多文件)

    环境:win10 64位  python3.7 单*.py文件打包Python GUI:程序打包为exe 一.安装Pyinstaller,命令pip install Pyinstaller,(大写的P ...

  3. css引入的两种方法link和@import的区别和用法

    link和@import都是HTML中引入CSS的语法单词. 两者的基本语法 link语法结构 <link href="外部CSS文件的URL路径" rel="st ...

  4. vue页面传参和接参

    https://blog.csdn.net/zhouzuoluo/article/details/81259298(copy) js** this.$router.push({ name: 'Flow ...

  5. mysql数据库在linux上的不同登录方式和权限

    在我的上两篇博文里,一篇是安装,一篇是配置远程登录, 提君博客原创 >>提君博客原创  http://www.cnblogs.com/tijun/  << 所以我的mysql的 ...

  6. vue.js 添加 fastclick的支持

    fastclick:处理移动端click事件300毫秒延迟 1.兼容性 iOS 3及更高版本的移动Safari iOS 5及更高版本的Chrome Android上的Chrome(ICS) Opera ...

  7. 如何在TypeScript中使用第三方JavaScript框架

    一.安装typings 使用npm全局安装typings :npm install -g typings 安装成功. 二,搜索资源,支持模糊搜索:typings search base64 三.安装t ...

  8. Artifact project04:war :Error during artifact deployment. See server log for details

    困扰了我好长时间,我的错误是 先 Run clean  再package就成功了.

  9. MyBatis的demo

    把以前写的关于mybatis的demo放在这边,以便查看. 目录结构: package com.test.mybatis.util; import java.io.IOException; impor ...

  10. 运维常用mysql语句

    1..select @@version; ##查询当前mysql的版本. 2. show variables like 'port';##查看mysql实例的端口. 3.show variables ...