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个星星的坐标.每个星星都有一个等级,数值等于坐标系内纵坐标和横坐标皆不大于它的星星的个数.星星的坐标按照纵坐标从小到大的顺序给出,纵坐标相同时 ...
随机推荐
- BootStrap Select2组件
想使用Select2组件必须引用:select2.min.css和select2.min.js两个文件:如下: 页面写法很简单: 在这里多选是没有搜索功能的,只有单选的时候才会有搜索功能. Selec ...
- CCF|打酱油|Java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = ...
- APP崩溃处理
以前经常遇到APP内部异常情况下的Exception,最初是通过try catch这样的方式处理:但是APP上线后,用户在特地的情况下触发 了某些Exception,当然这些Exception从理论和 ...
- Eureka 整理
服务治理:(该模块也可以使用集群) 该模块主要负责完成微服务架构中的服务治理功能. 1.构建服务注册中心. 每个服务单元需要向注册中心登记自己提供的服务. 2.服务注册与服务发现. 服务之间的调用不再 ...
- COPY - 在表和文件之间拷贝数据
SYNOPSIS COPY tablename [ ( column [, ...] ) ] FROM { 'filename' | STDIN } [ [ WITH ] [ BINARY ] [ O ...
- Java基础(十二)--clone()方法
Clone在Java中就是用来复制对象,通过分配一个和源对象相同大小的内存空间,然后创建一个新的对象,那么他和=的区别在哪? 通过=实现对象拷贝: @Data @NoArgsConstructor @ ...
- 【转】c语言中的定义和声明
1. 变量的定义.声明 变量的声明有两种情况: 一种是需要建立存储空间的.例如:int a.在声明的时候就已经建立了存储空间.这种声明是"定义性声明(defining declaratio ...
- Webstorm安装、破解、使用
Webstorm是专用于web开发的号称最好的的编辑器,界面美观大方,有黑.白和经典三大主题可选,使用起来整体感觉良好,破解也不是很麻烦. 一.安装 1.去官网下载正版安装包(建议所有软件都去官网下载 ...
- react-native 0.58版本打包图片问题 task ':app:mergeReleaseResources' Error: Duplicate resources
debug没问题,在生成正式apk的时候就如下: google了一下在github上找到了解决方案: github问题指向 在node_modules/react-native/react.gradl ...
- 根据Dockerfile创建hello docker镜像
一.编写hello可执行c文件: 1.安装:gcc glibc glibc-static yum install -y gcc glibc glibc-static 2.编写hello.c:vim h ...