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;
}

CodeForces 164C Machine Programming 费用流的更多相关文章

  1. Codeforces 708D 上下界费用流

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

  2. codeforces gym 100357 I (费用流)

    题目大意 给出一个或与表达式,每个正变量和反变量最多出现一次,询问是否存在一种方案使得每个或式中有且仅有一个变量的值为1. 解题分析 将每个变量拆成三个点x,y,z. y表示对应的正变量,z表示对应的 ...

  3. CodeForces 1187G Gang Up 费用流

    题解: 先按时间轴将一个点拆成100个点. 第一个点相当于第一秒, 第二个点相当于第二秒. 在这些点之间连边, 每1流量的费用为c. 再将图上的边也拆开. 将 u_i 向 v_i+1 建边. 将 v_ ...

  4. Codeforces 最大流 费用流

    这套题目做完后,一定要反复的看! 代码经常出现的几个问题: 本机测试超时: 1.init函数忘记写. 2.addedge函数写成add函数. 3.边连错了. 代码TLE: 1.前向星边数组开小. 2. ...

  5. Codeforces Gym 100002 E "Evacuation Plan" 费用流

    "Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  6. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  7. Codeforces 280D k-Maximum Subsequence Sum [模拟费用流,线段树]

    洛谷 Codeforces bzoj1,bzoj2 这可真是一道n倍经验题呢-- 思路 我首先想到了DP,然后矩阵,然后线段树,然后T飞-- 搜了题解之后发现是模拟费用流. 直接维护选k个子段时的最优 ...

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

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

  9. Codeforces 730I [费用流]

    /* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给两行n个数,要求从第一行选取a个数,第二行选取b个数使得这些数加起来和最大. 限制条件是第一行选取了某个数的条件下,第二行不能选取对应位置的数. ...

随机推荐

  1. Hanoi塔问题

    说明:河内之塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时北越的首都,即现在的胡志明市:1883年法国数学家 Edouard Luc ...

  2. Range类中的三个方法及简单打印

    package pkgFirst; import org.junit.Test; public class Range{ /** * FunName: println * Description : ...

  3. LoadRunner error -27498

    URL=http://172.18.20.70:7001/workflow/bjtel/leasedline/ querystat/ subOrderQuery.do错误分析:这种错误常常是因为并发压 ...

  4. [转载]我读过最好的Epoll模型讲解

    转载来自:http://blog.csdn.net/mango_song/article/details/42643971 首先我们来定义流的概念,一个流可以是文件,socket,pipe等等可以进行 ...

  5. 我的新计划 《2Dof Racing Simulator》2014/3/9 20:30:00

    最近好久都没来网站上了,也没心思和时间去弄VellLock和升级V&View了.一直在蕴量这做一件大玩意. 最近一直都很忙,忙着做数电课设,还有各种实验,可是我的心思不在这些东西上,当然除了数 ...

  6. Mapreduce执行过程分析(基于Hadoop2.4)——(一)

    1 概述 该瞅瞅MapReduce的内部运行原理了,以前只知道个皮毛,再不搞搞,不然怎么死的都不晓得.下文会以2.4版本中的WordCount这个经典例子作为分析的切入点,一步步来看里面到底是个什么情 ...

  7. DD_belatedPNG,解决 IE6 不支持 PNG-24 绝佳解决方案

    png24在ie下支持透明.终于找到下面的可行办法: 我们知道 IE6 是不支持透明的 PNG-24 的,这无疑限制了网页设计的发挥空间. 然而整个互联网上解决这个 IE6 的透明 PNG-24 的方 ...

  8. 记录一个 关于 python 普通方法,静态方法和类方法 的介绍。@classmethod @staticmethod

    上班时间 只贴看到最厉害的答案 回头总结 http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod ...

  9. 在IT网站上少花些时间

    我自己关注的IT网站还是蛮多的,经常去的有CSDN,博客园,51CTO,InfoQ,还有微博,微信上关注了IT程序猿,IT技术博客大学习,程序员之家, 开发者头条,还有还有,我还通过邮件订阅了码家周刊 ...

  10. Quora的技术探索(转)

    原文:http://www.cnblogs.com/xiekeli/archive/2012/04/27/2473808.html 关于问答类的应用,最早接触的是stackoverflow和知乎 ,而 ...