Intervals
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5762   Accepted: 2288

Description

You are given N weighted open intervals. The ith interval covers (aibi) 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 aibiwi(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.

Sample Input

4

3 1
1 2 2
2 3 4
3 4 8 3 1
1 3 2
2 3 4
3 4 8 3 1
1 100000 100000
1 2 3
100 200 300 3 2
1 100000 100000
1 150 301
100 200 300

Sample Output

14
12
100000
100301

Source

一条线段看成两个点。

#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <string>
#include <queue>
using namespace std; const int MAXN = ;
const int MAXM = ;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow,cost;
}edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1
void init(int n)
{
N = n;
tol = ;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int cap,int cost)
{
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = ;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = ;
edge[tol].cost = -cost;
edge[tol].flow = ;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa(int s,int t)
{
queue<int>q;
for(int i = ;i < N;i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -;
}
dis[s] = ;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -;i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap > edge[i].flow &&
dis[v] > dis[u] + edge[i].cost )
{
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t] == -)return false;
else return true;
}
//返回的是最大流,cost存的是最小费用
int minCostMaxflow(int s,int t,int &cost)
{
int flow = ;
cost = ;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t];i != -;i = pre[edge[i^].to])
{
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i = pre[t];i != -;i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
cost += edge[i].cost * Min;
}
flow += Min;
}
return flow;
} pair<int,int>p[];
int main()
{
int T;
int n,k;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&k);
init(*n+);
int a,b,w;
for(int i = ;i <= n;i++)
{
scanf("%d%d%d",&a,&b,&w);
p[i] = make_pair(a,b);
addedge(*i-,*i,,-w);
addedge(*i-,*i,INF,);
addedge(,*i-,,);
addedge(*i,*n+,,);
}
addedge(*n+,,k,);
for(int i = ;i <= n;i++)
for(int j = ;j <= n;j++)
if(p[j].first >= p[i].second )
addedge(*i,*j-,INF,);
int cost = ;
minCostMaxflow(*n+,*n+,cost);
printf("%d\n",-cost); }
return ;
}

POJ 3680 Intervals(费用流)的更多相关文章

  1. poj 3680 Intervals(费用流)

    http://poj.org/problem?id=3680 巧妙的构图. 题目:给定N个区间(ai,bi)权值wi,求最大权和且每个点最多覆盖K次. 构图:将区间端点离散化,将第i个点连第i+1个点 ...

  2. POJ 3680: Intervals【最小费用最大流】

    题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...

  3. poj 3680(最小费用最大流)

    题目链接:http://poj.org/problem?id=3680 思路:因为N<=200,而区间范围为[1,100000],因此需要离散化,去重,然后就是建图了相连两点连边,容量为k,费用 ...

  4. POJ 2516 基础费用流

    题意       有n个顾客,m个供应商,k种货物,给你顾客对于每种货物的要求个数,和供应商对于每种货物的现有量,以及供应每种货物的时候供应商和顾客之间的运输单价,问你满足所有顾客的前提下的最小运输费 ...

  5. POJ 2135 简单费用流

    题意:       题意是一个人他要从牧场1走到牧场n然后在走回来,每条路径只走一次,问全程的最短路径是多少. 思路:        这个题目挺简单的吧,首先要保证每条边只能走一次,然后还要要求费用最 ...

  6. 网络流(最大费用最大流) :POJ 3680 Intervals

    Intervals Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7218   Accepted: 3011 Descrip ...

  7. POJ 3680 Intervals(费用流+负权优化)

    [题目链接] http://poj.org/problem?id=3680 [题目大意] 有N个带权重的区间,现在要从中选取一些区间, 要求任意点都不被超过K个区间所覆盖,请最大化总的区间权重. [题 ...

  8. POJ 3680 Intervals 最小费用最大流(MCMF算法)

    题意:给出 n ,k 表示接下来给你 n 段开区间,每段区间都有它的权值,问选出一些区间,使它的权值最大,并且在实轴上的每个点,不得超过 k次被覆盖. 思路:首先要理解建图思路,首先有一个基图,相邻点 ...

  9. poj 3680 Intervals

    给定N个带权的开区间,第i个区间覆盖区间(ai,bi),权值为wi.现在要求挑出一些区间使得总权值最大,并且满足实轴上任意一个点被覆盖不超过K次. 1<=K<=N<=200.1< ...

随机推荐

  1. nginx源码分析--使用GDB调试(strace、 pstack )

    nginx源码分析--使用GDB调试(strace.  pstack ) http://blog.csdn.net/scdxmoe/article/details/49070577

  2. C++中delete和delete[]的区别(转)

    原文链接:http://www.cnblogs.com/charley_yang/archive/2010/12/08/1899982.html 一直对C++中的delete和delete[]的区别不 ...

  3. hibernate连接Oracle rac

    连接方式与普通的数据库不一样.connection.url 中使用了LOAD-BALANCE = yes 要不然会报错 <hibernate-configuration> <sess ...

  4. linux命令行任务管理

    今天看到了linux命令行的任务管理命令感觉很实用: 1.ctrl+z  将当前前台执行的任务放到后台并暂停 2.fg恢复上次放入后台的任务 这两个命令组合起来很实用,比如在linux命令行中写pyt ...

  5. python标准库之【socket】

    socket通常也称作”套接字“.网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket.socket 是网络连接端点.例如当你的Web浏览器请求www.fishc. ...

  6. C#进行Socket通信编程之一

    关于Socket编程的相关资料(含实例)在网上多如牛毛,而我写这篇文章的初衷仅仅是为了记录自己的一些心得体会. Socket提供了这样一个接口,可以方便地使程序员通过其来发送和接收网络上的数据.在利用 ...

  7. mysql-表完整性约束

    阅读目录 一 介绍 二 not null与default 三 unique 四 primary key 五 auto_increment 六 foreign key 七 总结     一 介绍 回到顶 ...

  8. zookeeper编程入门系列之zookeeper实现分布式进程监控和分布式共享锁(图文详解)

    本博文的主要内容有 一.zookeeper编程入门系列之利用zookeeper的临时节点的特性来监控程序是否还在运行   二.zookeeper编程入门系列之zookeeper实现分布式进程监控 三. ...

  9. 原生DOM选择器querySelector和querySelectorAll

    在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id ...

  10. AC日记——[SDOI2017]相关分析 洛谷 P3707

    [SDOI2017]相关分析 思路: 裸线段树: (玄学ac): 代码: #include <bits/stdc++.h> using namespace std; #define max ...