Intervals POJ - 3680 (MCMF)
给你一些区间,每个区间都有些价值。取一个区间就能获得对应的价值,并且一个点不能覆盖超过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)的更多相关文章
- 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)
[LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
- 2014湘潭全国邀请赛I题 Intervals /POJ 3680 / 在限制次数下取有权区间使权最大/小问题(费用流)
先说POJ3680:给n个有权(权<10w)开区间(n<200),(区间最多数到10w)保证数轴上所有数最多被覆盖k次的情况下要求总权最大,输出最大权. 思路: 限制的处理:s ...
- POJ题目(转)
http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法: (1)枚举. (poj1753,poj29 ...
- Repeater POJ - 3768 (分形)
Repeater POJ - 3768 Harmony is indispensible in our daily life and no one can live without it----may ...
- Booksort POJ - 3460 (IDA*)
Description The Leiden University Library has millions of books. When a student wants to borrow a ce ...
- Radar Installation POJ - 1328(贪心)
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- Best Cow Fences POJ - 2018 (二分)
Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains ...
- E - The Balance POJ - 2142 (欧几里德)
题意:有两种砝码m1, m2和一个物体G,m1的个数x1, m2的个数为x2, 问令x1+x2最小,并且将天平保持平衡 !输出 x1 和 x2 题解:这是欧几里德拓展的一个应用,欧几里德求不定方程 ...
- poj 1220(短除法)
http://poj.org/problem?id=1220 题意:进制转换,把a进制转换为b进制. 如果数据不大的话,那么这个题还是很简单的,但这个题就是数据范围太大,所以这里可以采用短除法来做. ...
随机推荐
- 接口工具之postman
在我们日常开发中,经常会对功能接口进行相应的测试.那么postman是一款不错的测试工具,因为平常使用的比较多,因此在这里简单记录一下,经常使用到的一些地方 简单的使用就不错介绍了, 基本流程: 新建 ...
- C#中闭包的陷阱
我们在使用lambda的时候会遇到闭包,在闭包中有一个陷阱是在for循环中产生的,先上代码: class Program { static void Main(string[] args) { Act ...
- 2 Modals of necessity
1 情况动词must 和词组have to, need to ,have got to 都表示必须做某事或者要求做某事. need to have to must have got to When ...
- maven eclipse 第3方包
C:\Users\3510\.m2\repository\myjar install:install-file -Dfile=C:\Users\3510\.m2\repository\myjar\al ...
- Azure系列2.1.3 —— BlobEncryptionPolicy
(小弟自学Azure,文中有不正确之处,请路过各位大神指正.) 网上azure的资料较少,尤其是API,全是英文的,中文资料更是少之又少.这次由于公司项目需要使用Azure,所以对Azure的一些学习 ...
- [转帖]Linux 下如何知道是否有人在使坏?
Linux 下如何知道是否有人在使坏? 学到了两个最简单的命令 usermod -L username 锁定账户 passwd -s username 查看用户状态. 自己linux 知道的还是少 需 ...
- js实现input的赋值
input框赋值如下所示,是一个文本框的html代码,实际开发中,要涉及到将数据库中的数据取出然后放入input框中. <input id="name1" name=&quo ...
- java连接CentOS7上的redis
这篇博客写得挺全的: https://blog.csdn.net/achenyuan/article/details/78521831?locationNum=3&fps=1 我也是跟着这篇博 ...
- unsupported time zone specified undefined
unsupported time zone specified undefined 出现了这个问题,莫名其妙的,上次被我下回去了,真的是下回去的,我一去查看,坐在电脑前面问题就不见了…… 具体出现 ...
- springmvc配置文件
1 springMVC的配置文件路径问题 https://www.cnblogs.com/ysloong/p/6071450.html