POJ 3762 The Bonus Salary!(最小K覆盖)
POJ 3762 The Bonus Salary!
题意:给定一些任务。每一个任务有一个时间,有k天。一个时间仅仅能运行一个任务,每一个任务有一个价值。问怎么安排能得到最多价值
思路:典型的区间k覆盖问题
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
using namespace std; const int MAXNODE = 4505;
const int MAXEDGE = 100005;
typedef int Type;
const Type INF = 0x3f3f3f3f; struct Edge {
int u, v;
Type cap, flow, cost;
Edge() {}
Edge(int u, int v, Type cap, Type flow, Type cost) {
this->u = u;
this->v = v;
this->cap = cap;
this->flow = flow;
this->cost = cost;
}
}; struct MCFC {
int n, m, s, t;
Edge edges[MAXEDGE];
int first[MAXNODE];
int next[MAXEDGE];
int inq[MAXNODE];
Type d[MAXNODE];
int p[MAXNODE];
Type a[MAXNODE]; void init(int n) {
this->n = n;
memset(first, -1, sizeof(first));
m = 0;
} void add_Edge(int u, int v, Type cap, Type cost) {
edges[m] = Edge(u, v, cap, 0, cost);
next[m] = first[u];
first[u] = m++;
edges[m] = Edge(v, u, 0, 0, -cost);
next[m] = first[v];
first[v] = m++;
} bool bellmanford(int s, int t, Type &flow, Type &cost) { for (int i = 0; i < n; i++) d[i] = INF;
memset(inq, false, sizeof(inq));
d[s] = 0; inq[s] = true; p[s] = s; a[s] = INF;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = false;
for (int i = first[u]; i != -1; i = next[i]) {
Edge& e = edges[i];
if (e.cap > e.flow && d[e.v] > d[u] + e.cost) {
d[e.v] = d[u] + e.cost;
p[e.v] = i;
a[e.v] = min(a[u], e.cap - e.flow);
if (!inq[e.v]) {Q.push(e.v); inq[e.v] = true;}
}
}
}
if (d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].u;
}
return true;
} Type Mincost(int s, int t) {
Type flow = 0, cost = 0;
while (bellmanford(s, t, flow, cost));
return cost;
}
} gao; const int N = 2005; map<int, int> hash;
int n, k, hn; int get(int x) {
if (!hash.count(x)) hash[x] = hn++;
return hash[x];
} struct Task {
int l, r, w;
} task[N]; int main() {
while (~scanf("%d%d", &n, &k)) {
hash.clear();
hn = 1;
int h1, m1, s1, h2, m2, s2, w;
for (int i = 0; i < n; i++) {
scanf("%d:%d:%d %d:%d:%d %d", &h1, &m1, &s1, &h2, &m2, &s2, &w);
int t1 = h1 * 3600 + m1 * 60 + s1;
int t2 = h2 * 3600 + m2 * 60 + s2;
task[i].l = get(t1); task[i].r = get(t2); task[i].w = w;
}
gao.init(hn + 1);
for (int i = 0; i < n; i++)
gao.add_Edge(task[i].l, task[i].r, 1, -task[i].w);
int pre = 0;
for (map<int, int>::iterator it = hash.begin(); it != hash.end(); it++) {
gao.add_Edge(pre, it->second, k, 0);
pre = it->second;
}
gao.add_Edge(pre, hn, k, 0);
printf("%d\n", -gao.Mincost(0, hn));
}
return 0;
}
POJ 3762 The Bonus Salary!(最小K覆盖)的更多相关文章
- POJ 3762 The Bonus Salary!
The Bonus Salary! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Origi ...
- poj 3020 Antenna Placement(最小路径覆盖 + 构图)
http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- POJ 2594 Treasure Exploration(最小路径覆盖变形)
POJ 2594 Treasure Exploration 题目链接 题意:有向无环图,求最少多少条路径能够覆盖整个图,点能够反复走 思路:和普通的最小路径覆盖不同的是,点能够反复走,那么事实上仅仅要 ...
- POJ 3216 Repairing Company(最小路径覆盖)
POJ 3216 Repairing Company id=3216">题目链接 题意:有m项任务,每项任务的起始时间,持续时间,和它所在的block已知,且往返每对相邻block之间 ...
- POJ 2594 —— Treasure Exploration——————【最小路径覆盖、可重点、floyd传递闭包】
Treasure Exploration Time Limit:6000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64 ...
- poj 2060 Taxi Cab Scheme (最小路径覆盖)
http://poj.org/problem?id=2060 Taxi Cab Scheme Time Limit: 1000MS Memory Limit: 30000K Total Submi ...
- poj 2594 Treasure Exploration(最小路径覆盖,可重点)
题意:选出最小路径覆盖图中所有点,路径可以交叉,也就是允许路径有重复的点. 分析:这个题的难点在于如何解决有重复点的问题-方法就是使用Floyd求闭包,就是把间接相连的点直接连上边,然后就是求最小路径 ...
- POJ 2594 Treasure Exploration (Floyd+最小路径覆盖)
<题目链接> 题目大意: 机器人探索宝藏,有N个点,M条边.问你要几个机器人才能遍历所有的点. 解题分析: 刚开始还以为是最小路径覆盖的模板题,但是后面才知道,本题允许一个点经过多次,这与 ...
- poj 2594(可相交的最小路径覆盖)
题目链接:http://poj.org/problem?id=2594 思路:本来求最小路径覆盖是不能相交的,那么对于那些本来就可达的点怎么处理,我们可以求一次传递闭包,相当于是加边,这样我们就可以来 ...
随机推荐
- python学习之老男孩python全栈第九期_day022作业
1. 写一个求正方形周长和面积的类 class Square: def __init__(self, length): self.length = length def area(self): ret ...
- Thymeleaf学习记录(8)--表达式基本对象
基础对象 #ctx:上下文对象 /* * ====================================================================== * See ja ...
- HTML是什么与基础格式
html 又称 超文本标记语言. 网页的本质其实就是html代码,通过浏览器,将html转换翻译成用户可以看得懂的展现丰富的页面. 所以制作网站的本质,其实就是编写html代码. HTML基础格式 & ...
- LOJ6066:「2017 山东一轮集训 Day3」第二题
传送门 二分答案 \(k\),考虑如何 \(hash\) 使得做起来方便 把每个点挂在 \(k+1\) 级祖先上,考虑在祖先上删除 这道题巧妙在于其可以对于 \(dfs\) 序/括号序列 \(hash ...
- 鼠标事件-拖拽2(不能拖出指定对象的div)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- Ubuntu install TensorFlow 1.10 + CUDA 9.2 + cuDNN 7.2
为了装TensorFlow 1.10 下面升级一下系统的软件环境 NVIDIA DRIVER 去官网下载最新的linux驱动 http://www.nvidia.com/Download/in ...
- 哪个HTML5内建对象用于在画布上绘制?()
哪个HTML5内建对象用于在画布上绘制?() getContent getContext getGraphics getCanvas 我的理解: A.C.D不存在HTML5,,js方法中 HTML 5 ...
- 《Java并发编程实战》文摘
更新时间:2017-06-03 <Java并发编程实战>文摘,有兴趣的朋友可以买本纸质书仔细研究下. 一 线程安全性 1.1 什么是线程安全性 当多个线程访问某个类时,不管运行时环境采用何 ...
- Python学习---Model拾遗[2]180318
Model的字段及字段参数: Model字段: 数字 字符串(带正则的字段) 时间 文件 特殊字段:(一对一,一对多,多对多) Models.py ...
- 使用TryUpdateModel进行数据更新
在控制器中可以使用TryUpdateModel或者UpdateModel方法来对指定的数据Model进行更新,如图所示的更新操作. POST请求数据如下所示 使用如下代码就可以对指定的字段进行更新 使 ...