POJ 3680_Intervals
题意:
给定区间和该区间对应的权值,挑选一些区间,求使得每个数都不被K个区间覆盖的最大权值和。
分析:
如果K=1,即为区间图的最大权独立集问题。可以对区间所有端点排序后利用动态规划的方法,设dp[i]为只考虑区间右端点小于等于xi的区间所得到的最大总权重。
dp[i] = max(dp[i - 1], max{dp[j] + w[k])|a[k] = x[j]且b[k] = x[i]}
K>1,既然求权重最大值,利用最小费用流,很容易想到从a[i]到b[i]连一条容量为1,费用为−w[i]的边,但是如何限制每个数不被超过K个区间覆盖呢?从i到i+1连一条容量为K,费用为0的边,这样便限制了流经每个端点的流量不超过K,也就满足每个数不被超过K个区间覆盖啦~注意区间端点的离散化~~
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int maxn = 505, maxm = 1000;
const int INF = 0x3f3f3f3f;
int s, t, tot;
int dist[maxm], prevv[maxm], preve[maxm], head[maxm];
int a[maxn], b[maxn], w[maxn], tt[maxm];
bool in[maxn];
struct Edge{ int from, to, next, cap, cost;}edge[maxm * 3];
void add_edge(int from, int to, int cap, int cost)
{
edge[tot].to = to;
edge[tot].from = from;
edge[tot].cap = cap;
edge[tot].cost = cost;
edge[tot].next = head[from];
head[from] = tot++;
edge[tot].to = from;
edge[tot].from = to;
edge[tot].cap = 0;
edge[tot].cost = -cost;
edge[tot].next = head[to];
head[to] = tot++;
}
int mincost()
{
int flow=0, cost=0;
for(;;){
memset(dist, 0x3f, sizeof(dist));
memset(in, false, sizeof(in));
queue<int>q;
q.push(s);
in[s] = true;
dist[s]=0;
while(!q.empty()){
int u = q.front();q.pop();
in[u] = false;
for(int i = head[u]; i != -1; i = edge[i].next){
Edge e = edge[i];
if(e.cap>0 && dist[e.to] > dist[u] + e.cost){
dist[e.to] = dist[u] + e.cost;
prevv[e.to] = u, preve[e.to] = i;
if(!in[e.to]){
in[e.to] = true;
q.push(e.to);
}
}
}
}
if(dist[t] == INF) return cost;
int d = INF;
for(int i = t; i != s; i = prevv[i])
d = min(d, edge[preve[i]].cap);
flow += d;
cost += dist[t] * d;
for(int i = t; i != s; i = prevv[i]){
edge[preve[i]].cap -= d;
edge[preve[i]^1].cap += d;
}
}
}
int main()
{
int c;scanf("%d",&c);
while(c--){
int N, K;
memset(head,-1,sizeof(head));
tot = 0;
int n = 0;
scanf("%d%d",&N, &K);
for(int i = 0; i < N; i++){
scanf("%d%d%d", &a[i], &b[i], &w[i]);
tt[n++] = a[i];
tt[n++] = b[i];
}
sort(tt, tt + n);
int nn = unique(tt, tt +n) - tt;
int na, nb;
for(int i = 0; i < N; i++){
na = lower_bound(tt, tt + nn, a[i]) - tt;
nb = lower_bound(tt, tt + nn, b[i]) - tt;
add_edge(na + 1, nb + 1, 1, -w[i]);
}
s = 0, t = nn + 1;
add_edge(s, 1, K, 0);
for(int i = 1; i <= nn; i++)
add_edge(i, i + 1, K, 0);
printf("%d\n",-mincost());
}
return 0;
}
其实这题也可以是从i+1向i连一条容量为1,权值为w[i]的边,用求出的最小费用流减去所有区间权值和,再取负数就好啦~实际上是取最小费用流对应的区间之外的区间,因为建图保证每个点都不被超过K个区间覆盖,所以不用担心与题目不符啦~~
tle了一整天。。。。
很巧妙的构图~~~
POJ 3680_Intervals的更多相关文章
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- POJ 2752 Seek the Name, Seek the Fame [kmp]
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17898 Ac ...
- poj 2352 Stars 数星星 详解
题目: poj 2352 Stars 数星星 题意:已知n个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...
随机推荐
- Oracle 用到的服务
1.Oracle ORCL VSS Writer Service Oracle卷映射拷贝写入服务,VSS(Volume ShadowCopy Service)能够让存储基础设备(比如磁盘,阵列等)创建 ...
- C#_JDBC连接数据库
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 一个net程序猿必备工具
自古以来,人类的进步都是依赖于工具的进步,从刀耕火种,到使用青铜器,再到现在的科技,每一次都使我们的工作效率提高了无数倍,所以一个好的工具能使我们提高无数倍的工作效率,下面,我就根据自己简单的总结一下 ...
- Activiti工作流和shiro权限管理关系图
- ReactJS-0-React介绍
React介绍: React是一个库而不是一个MVC框架,因为React只负责解决MVC框架中V(View)层面的问题,React致力于创建可重用的UI组件.(React is a library f ...
- 田字格布局html div
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- zookeeper、consul 实现注册中心
1.Zookeeper 分布式协调工具,可以实现注册中心 所有实现方式基本一致,只需要先开启zookeeper的服务端,然后再打开客户端jar包即可. Zookeeper一开始连接失败,后面又可以了, ...
- Spring全局异常捕获
package org.xxx.ac.zpk.exception; import java.io.IOException; import javax.servlet.http.HttpServletR ...
- scws分词配置
1.下载安装包 wget -q -O - http://www.xunsearch.com/scws/down/scws-1.2.3.tar.bz2 | tar xjf - 2.进入源码目录配置和编译 ...
- Git中文书籍
Git中文书籍: http://git-scm.com/book/zh/v1