Machine Programming

题目连接:

http://codeforces.com/problemset/problem/164/B

Descriptionww.co

One remarkable day company "X" received k machines. And they were not simple machines, they were mechanical programmers! This was the last unsuccessful step before switching to android programmers, but that's another story.

The company has now n tasks, for each of them we know the start time of its execution si, the duration of its execution ti, and the company profit from its completion ci. Any machine can perform any task, exactly one at a time. If a machine has started to perform the task, it is busy at all moments of time from si to si + ti - 1, inclusive, and it cannot switch to another task.

You are required to select a set of tasks which can be done with these k machines, and which will bring the maximum total profit.

Input

The first line contains two integer numbers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ 50) — the numbers of tasks and machines, correspondingly.

The next n lines contain space-separated groups of three integers si, ti, ci (1 ≤ si, ti ≤ 109, 1 ≤ ci ≤ 106), si is the time where they start executing the i-th task, ti is the duration of the i-th task and ci is the profit of its execution.

Output

Print n integers x1, x2, ..., xn. Number xi should equal 1, if task i should be completed and otherwise it should equal 0.

If there are several optimal solutions, print any of them.

Sample Input

3 1

2 7 5

1 3 3

4 1 3

Sample Output

0 1 1

Hint

题意

有n个任务,m个机器,每个机器同一时间只能处理一个任务

每个任务开始时间为s,持续时间为t,做完可以赚c元

问你做哪几个任务可以拿到最多的钱

输出方案

题解:

费用流

离散化每个任务的开始时间和结束时间,然后建图跑一遍就好了

把所有时间扔到一个队列里面排序

然后建立源点到最开始任务的起始时间-第二个时间-第三个时间-....-最后一个时间点-汇点,期间流量都是m,花费为0

然后对于每一个任务,连一条开始时间到结束时间+1的边,花费为-c,流量为1的

然后这样跑费用流一定就是答案了

代码

#include<bits/stdc++.h>
using namespace std;const int MAXN = 10000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
struct Edge
{int to, next, cap, flow, cost;int id;int x, y;
} edge[MAXM],HH[MAXN],MM[MAXN];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N, M;
void init()
{N = MAXN;tol = 0;memset(head, -1, sizeof(head));
}
int addedge(int u, int v, int cap, int cost, int id)//左端点,右端点,容量,花费
{edge[tol]. to = v;edge[tol]. cap = cap;edge[tol]. cost = cost;edge[tol]. flow = 0;edge[tol]. next = head[u];edge[tol]. id = id;int t = tol;head[u] = tol++;edge[tol]. to = u;edge[tol]. cap = 0;edge[tol]. cost = -cost;edge[tol]. flow = 0;edge[tol]. next = head[v];edge[tol]. id = id;head[v] = tol++;return tol;
}
bool spfa(int s, int t)
{queue<int>q;for(int i = 0; i < N; i++){dis[i] = INF;vis[i] = false;pre[i] = -1;}dis[s] = 0;vis[s] = true;q.push(s);while(!q.empty()){int u = q.front();q.pop();vis[u] = false;for(int i = head[u]; i != -1; 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] == -1) return false;else return true;
}
//返回的是最大流, cost存的是最小费用
int minCostMaxflow(int s, int t, int &cost)
{int flow = 0;cost = 0;while(spfa(s,t)){int Min = INF;for(int i = pre[t]; i != -1; i = pre[edge[i^1]. to]){if(Min > edge[i]. cap - edge[i]. flow)Min = edge[i]. cap - edge[i]. flow;}for(int i = pre[t]; i != -1; i = pre[edge[i^1]. to]){edge[i]. flow += Min;edge[i^1]. flow -= Min;cost += edge[i]. cost * Min;}flow += Min;}return flow;
}
struct node
{int st,et,ct;int id;
}task[MAXN];
bool cmp(node A,node B)
{if(A.st==B.st)return A.et<B.et;return A.st<B.st;
}
map<int,int> H;
vector<int> V;
int id[3000];
int main()
{int n, m;scanf("%d%d",&n,&m);init();for(int i=1;i<=n;i++){scanf("%d%d%d",&task[i].st,&task[i].et,&task[i].ct);task[i].et+=task[i].st-1;task[i].id=i;V.push_back(task[i].st);V.push_back(task[i].et);}sort(V.begin(),V.end());V.erase(unique(V.begin(),V.end()),V.end());for(int i=0;i<V.size();i++)H[V[i]]=i+1;for(int i=1;i<=V.size();i++)addedge(i-1,i,m,0,0);addedge(V.size(),V.size()+1,m,0,0);addedge(V.size()+1,V.size()+2,m,0,0);for(int i=1;i<=n;i++)id[i]=addedge(H[task[i].st],H[task[i].et]+1,1,-task[i].ct,i);int ans1=0,ans2=0;ans1=minCostMaxflow(0,V.size()+2,ans2);//printf("%d\n",ans2);for(int i=1;i<=n;i++)printf("%d ",edge[id[i]-2].flow);return 0;
}

转载于:https://www.cnblogs.com/qscqesze/p/5150107.html

CodeForces 164C Machine Programming 费用流相关推荐

  1. Codeforces 708D 上下界费用流

    给你一个网络流的图 图中可能会有流量不平衡和流量>容量的情况存在 每调整一单位的流量/容量 需要一个单位的花费 问最少需要多少花费使得原图调整为正确(可行)的网络流 设当前边信息为(u,v,f, ...

  2. BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...

  3. Codeforces 362E Petya and Pipes 费用流建图

    题意: 给一个网络中某些边增加容量,增加的总和最大为K,使得最大流最大. 费用流:在某条边增加单位流量的费用. 那么就可以2个点之间建2条边,第一条给定边(u,v,x,0)这条边费用为0 同时另一条边 ...

  4. Codeforces 884F Anti-Palindromize 费用流

    题意 定义"反回文串"为一个字符串 S[1..|S|] S [ 1.. | S | ] S[1..|S|],对于任意 1<=i<=|S| 1 <= i <= ...

  5. Gym 101190D BZOJ 4842 Luogu P6967 LOJ #6071 [NEERC2016]Delight for a Cat (费用流)

    题目链接 (BZOJ) 大人,时代变了 (Gym) https://codeforces.com/gym/101190 (Luogu) https://www.luogu.com.cn/problem ...

  6. 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem I. Plugs and Sockets 费用流

    Problem I. Plugs and Sockets 题目连接: http://www.codeforces.com/gym/100253 Description The Berland Regi ...

  7. C2. Tidying Up(费用流 二分图)

    http://codeforces.com/problemset/problem/316/C2 题意: 有n*m格子,[1,n*m/2]每个数都出现两次,现在可以选择一些点,被选择的点之间可以任意交换 ...

  8. [Neerc2016]Mole Tunnels (模拟费用流)

    题目链接:http://codeforces.com/gym/101190 SOLUTION: 模拟费用流 这题看完之后很容易想到费用流,但是n太大了不能直接跑. 我们考虑模拟这个费用流的增广过程,每 ...

  9. [BZOJ 1221][HNOI2001]软件开发(费用流)

    Description 某软件公司正在规划一项n天的软件开发计划,根据开发计划第i天需要ni个软件开发人员,为了提高软件开发人员的效率,公司给软件人员提供了很多的服务,其中一项服务就是要为每个开发人员 ...

最新文章

  1. 权限组件(10):三级菜单的展示和增删改查
  2. 前沿视频教室——《C#图解教程》是本好书,强烈推荐!
  3. Spring Security源码分析八:Spring Security 退出
  4. js获取当前页面的参数,带完善~~~
  5. 产品经理是种病,我竟已晚期
  6. mysql为什么要分库_我们为什么要分库分表
  7. TreeSet()详解
  8. Oracle中函数/过程返回结果集的几种方式
  9. 打印九九乘法表(跳转语句)
  10. [560]python简单验证文本的Zipf分布
  11. matlab神经网络预测数据,Matlab神经网络预测复数
  12. 两个音轨合并_怎样将两个音频合并在一起?超详细教程!
  13. WordPress外贸网站速度优化的四个层次
  14. 新建android模拟器无法拨号 真机可以拨号,Android模拟器相关操作设置
  15. U盘做成Mac启动盘之后怎么恢复成原来的U盘(U盘变成efi怎么恢复)
  16. 科达制造和盐湖股份的事儿
  17. 良精商城网店购物系统
  18. 苹果错误分析报告preferreuserinterface_双十一性能报告第二弹今年,你还在为双十一奋斗吗?...
  19. Error:java: Annotation processing is not supported for module cycles.项目启动报错 异常解决
  20. fastlane 入门使用

热门文章

  1. 提取voc数据集中特定的类
  2. signature=0d9b3a8f96c5f64e92cd85aaf7e70ac0,Scale controller
  3. oracle 段空间收缩,Oracle10g用Shrink Space收缩Oracle数据段
  4. python 字典排序成绩_集体备课第四章 python基础与顺序结构
  5. 山师计算机学硕分数线,2020山东师范大学考研复试分数线已公布
  6. MyBatis 如何传递参数(全)
  7. mongodb mysql json数据类型_mongodb 数据格式补充
  8. Web前端基础---JavaScript函数事件及其绑定DOM模型BOM模型
  9. sql 数据检索后的替换格式化
  10. 【BZOJ 1801】【AHOI 2009】中国象棋(递推DP)