POJ 3762 The Bonus Salary!

The Bonus Salary!

Time Limit: 2000ms

Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 3762
64-bit integer IO format: %lld      Java class name: Main

In order to encourage employees‘ productivity, ACM Company has made a new policy. At the beginning of a period, they give a list of tasks to each employee. In this list, each task is assigned a "productivity score". After the first K days, the employee who gets the highest score will be awarded bonus salary.

Due to the difficulty of tasks, for task i-th:

  • It must be done from hh_Li mm_Li : ss_Li to hh_Ri : mm_Ri : ss_Ri.
  • This range of time is estimated very strictly so that anyone must use all of this time to finish the task.

Moreover, at a moment, each employee can only do at most one task. And as soon as he finishes a task, he can start doing another one immediately.

XYY is very hard-working. Unfortunately, he‘s never got the award. Thus, he asks you for some optimal strategy. That means, with a given list of tasks, which tasks he should do in the first K days to maximize the total productivity score. Notice that one task can be done at most once.

Input

The first line contains 2 integers N and K (1 ≤ N ≤ 2000, 0 ≤ K ≤ 100), indicating the number of tasks and days respectively. This is followed by N lines; each line has the following format:

hh_Li:mm_Li:ss_Li hh_Ri:mm_Ri:ss_Ri w

Which means, the i-th task must be done from hh_Li mm_Li : ss_Li to hh_Ri : mm_Ri : ss_Ri and its productivity score is w. (0 ≤hh_Lihh_Ri ≤ 23, 0 ≤mm_Limm_Riss_Liss_Ri≤ 59, 1 ≤ w ≤ 10000). We use exactly 2 digits (possibly with a leading zero) to represent hhmm and ss. It is guaranteed that the moment hh_Ri : mm_Ri : ss_Ri is strictly later thanhh_Li mm_Li : ss_Li. 

Output

The output only contains a nonnegative integer --- the maximum total productivity score.

Sample Input

5 2
09:00:00 09:30:00 2
09:40:00 10:00:00 3
09:29:00 09:59:00 10
09:30:00 23:59:59 4
07:00:00 09:31:00 3

Sample Output

16

Hint

The optimal strategy is:
Day1: Task1, Task 4
Day2: Task 3
The total productivity score is 2 + 4 + 10 = 16.

解题:最小费用最大流。离散化时间点。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 5010;
 18 struct arc{
 19     int v,w,f,next;
 20     arc(int x = 0,int y = 0,int z = 0,int nxt = 0){
 21         v = x;
 22         w = y;
 23         f = z;
 24         next = nxt;
 25     }
 26 };
 27 arc e[1000000];
 28 int head[maxn],d[maxn],p[maxn],tot,S,T;
 29 bool in[maxn];
 30 int n,m,lisan[maxn<<4],cnt,x[maxn],y[maxn],sc[maxn];
 31 void add(int u,int v,int w,int f){
 32     e[tot] = arc(v,w,f,head[u]);
 33     head[u] = tot++;
 34     e[tot] = arc(u,-w,0,head[v]);
 35     head[v] = tot++;
 36 }
 37 queue<int>q;
 38 bool spfa(){
 39     for(int i = S; i <= T; i++){
 40         d[i] = INF;
 41         p[i] = -1;
 42         in[i] = false;
 43     }
 44     while(!q.empty()) q.pop();
 45     d[S] = 0;
 46     in[S] = true;
 47     q.push(S);
 48     while(!q.empty()){
 49         int u = q.front();
 50         q.pop();
 51         in[u] = false;
 52         for(int i = head[u]; ~i; i = e[i].next){
 53             if(e[i].f > 0 && d[e[i].v] > d[u] + e[i].w){
 54                 d[e[i].v] = d[u] + e[i].w;
 55                 p[e[i].v] = i;
 56                 if(!in[e[i].v]){
 57                     in[e[i].v] = true;
 58                     q.push(e[i].v);
 59                 }
 60             }
 61         }
 62     }
 63     return p[T] > -1;
 64 }
 65 int solve(){
 66     int tmp = 0,minV;
 67     while(spfa()){
 68         minV = INF;
 69         for(int i = p[T]; ~i; i = p[e[i^1].v])
 70             minV = min(minV,e[i].f);
 71         for(int i = p[T]; ~i; i = p[e[i^1].v]){
 72             tmp += minV*e[i].w;
 73             e[i].f -= minV;
 74             e[i^1].f += minV;
 75         }
 76     }
 77     return tmp;
 78 }
 79 int main(){
 80     int hh,mm,ss;
 81     while(~scanf("%d %d",&n,&m)){
 82         tot = cnt = 0;
 83         memset(head,-1,sizeof(head));
 84         for(int i = 0; i < n; i++){
 85             scanf("%d:%d:%d",&hh,&mm,&ss);
 86             x[i] = hh*3600 + mm*60 + ss;
 87             lisan[cnt++] = x[i];
 88             scanf("%d:%d:%d",&hh,&mm,&ss);
 89             y[i] = hh*3600 + mm*60 + ss;
 90             lisan[cnt++] = y[i];
 91             scanf("%d",sc+i);
 92         }
 93         sort(lisan,lisan+cnt);
 94         int cnt1 = 1;
 95         for(int i = 1; i < cnt; i++)
 96         if(lisan[i] != lisan[cnt1-1]) lisan[cnt1++] = lisan[i];
 97         cnt = cnt1;
 98         S = 0;
 99         T = cnt;
100         for(int i = 0; i < cnt; i++) add(i,i+1,0,m);
101         for(int i = 0; i < n; i++){
102             int tx = lower_bound(lisan,lisan+cnt,x[i]) - lisan;
103             int ty = lower_bound(lisan,lisan+cnt,y[i]) - lisan;
104             add(tx,ty,-sc[i],1);
105         }
106         printf("%d\n",-solve());
107     }
108     return 0;
109 }
110 /*
111 5 2
112 09:00:00 09:30:00 2
113 09:40:00 10:00:00 3
114 09:29:00 09:59:00 10
115 09:30:00 23:59:59 4
116 07:00:00 09:31:00 3
117 */

时间: 2024-10-16 17:26:44

POJ 3762 The Bonus Salary!的相关文章

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> u

POJ 3762 The Bonus Salary! 最小费用最大流

题意最后可以简化为 给出若干个区间,每个区间由左端点,右端点, 权值构成. 挑出若干个区间,使得权值和最大,但必须满足区间任意部分重叠次数不超过k 这题跟POJ3680一样一样的 构图是这样 先把这些区间都给hash了. hash完必然这些区间端点都落在1,2,3..., cnt   这些数中, cnt是hash完 不同数的个数 然后建边, 源点为S,汇点为T S到1  建边  流量为k  费用为0 1到2,2到3,3到4....cnt-1到cnt   分别建边 流量为k 费用为0 cnt到T建

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

【图论】网络流总结

[图论]网络流总结 最大流部分 网络流题目的关键:看出是网络流而且确定正确的模型 最大流算法:用来解决从源点s到汇点t,整个网络最多能输送多少流量的题目 模板: #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const int MAXNODE = 105 * 2; const int MAXEDGE = 10000

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?       

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123