CF-164C. Machine Programming(最小费用最大流)
题意:
给你n个任务,k个机器,n个任务的起始时间,持续时间,完成任务的获利
每个机器可以完成任何一项任务,但是同一时刻只能完成一项任务,一旦某台机器在完成某项任务时,直到任务结束,这台机器都不能去做其他任务
最后问你当获利最大时,应该安排那些机器工作,即输出方案
分析:
要求的是最大费用,因此将费用取负就可以用最小费用最大流算法了
建图很重要。如果图建的复杂的话,可能就会超时了的!
新建源汇S T‘
对任务按照起始时间s按升序排序
拆点:
u 向 u'连一条边 容量为 1 费用为 -c,
u' 向 T连一条边 容量为 inf 费用为 0;
如果任务u完成后接下来最先开始的是任务v
则从u' 向 v连一条边,容量inf 费用 0.
另外,任务从前往后具有传递性,所以必须是第i个任务向第i+1个任务建边,容量为inf
// File Name: 164-C.cpp
// Author: Zlbing
// Created Time: 2013年08月13日 星期二 14时57分55秒 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
const int MAXN=2e3+;
struct Edge{
int from,to,cap,flow,cost;
};
struct MCMF{
int n,m,s,t;
vector<Edge>edges;
vector<int> G[MAXN];
int inq[MAXN];
int d[MAXN];
int p[MAXN];
int a[MAXN];
void init(int n){
this->n=n;
for(int i=;i<=n;i++)G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap,int cost){
edges.push_back((Edge){from,to,cap,,cost});
edges.push_back((Edge){to,from,,,-cost});
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool BellmanFord(int s,int t,int& flow,int& cost){
for(int i=;i<=n;i++)d[i]=INF;
CL(inq,);
d[s]=;inq[s]=;p[s]=;a[s]=INF; queue<int>Q;
Q.push(s);
while(!Q.empty()){
int u=Q.front();Q.pop();
inq[u]=;
for(int i=;i<(int)G[u].size();i++){
Edge& e=edges[G[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
d[e.to]=d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to]){
Q.push(e.to);
inq[e.to]=;
}
}
}
}
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]^].flow-=a[t];
u=edges[p[u]].from;
}
return true;
}
int Mincost(int s,int t){
int flow=,cost=;
while(BellmanFord(s,t,flow,cost));
return cost;
}
};
struct node{
int u, v,cost,id ;
bool operator <(const node &rsh)const
{
return u<rsh.u;
}
}pos[MAXN];
MCMF solver;
int ans[MAXN];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int a,b,c;
solver.init(*n+);
REP(i,,n-)
{
scanf("%d%d%d",&a,&b,&c);
pos[i]=(node){
a,a+b-,c,i
};
}
sort(pos,pos+n);
int s=n*,t=n*+;
REP(i,,n-)
{
solver.AddEdge(i,i+n,,-pos[i].cost);
solver.AddEdge(i+n,t,INF,);
if(i<n-)solver.AddEdge(i,i+,INF,);
for(int j=i+;j<n;j++)
{
if(pos[i].v<pos[j].u)
{
solver.AddEdge(i+n,j,INF,);
break;
}
}
}
solver.AddEdge(s,,m,);
solver.AddEdge(n-,t,m,);
solver.Mincost(s,t);
//printf("cost=%d\n",-tmp);
CL(ans,);
for(int i=;i<(int)solver.edges.size();i++)
{
Edge e=solver.edges[i];
if(e.cap)
{
int u=e.from;
if(u!=s&&u!=t&&u<n&&e.flow==e.cap)
{
ans[pos[u].id]=;
}
}
}
for(int i=;i<n;i++)
{
if(i)printf(" ");
printf("%d",ans[i]);
}
printf("\n");
}
return ;
}
CF-164C. Machine Programming(最小费用最大流)的更多相关文章
- 【CF708D】Incorrect Flow 最小费用可行流
[CF708D]Incorrect Flow 题意:给你一个点数为n,边数为m的流网络,每条边有一个容量c和流量f,这个网络可能是不合法的.你可以花费1的代价使c或f减少或增加1,可以修改无限次.你不 ...
- hdu 2686&&hdu 3376(拆点+构图+最小费用最大流)
Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdu 1853 Cyclic Tour (二分匹配KM最小权值 或 最小费用最大流)
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
- POJ - 2195 最小费用最大流
题意:每个人到每个房子一一对应,费用为曼哈顿距离,求最小的费用 题解:单源点汇点最小费用最大流,每个人和房子对于建边 #include<map> #include<set> # ...
- [板子]最小费用最大流(Dijkstra增广)
最小费用最大流板子,没有压行.利用重标号让边权非负,用Dijkstra进行增广,在理论和实际上都比SPFA增广快得多.教程略去.转载请随意. #include <cstdio> #incl ...
- bzoj1927最小费用最大流
其实本来打算做最小费用最大流的题目前先来点模板题的,,,结果看到这道题二话不说(之前打太多了)敲了一个dinic,快写完了发现不对 我当时就这表情→ =_=你TM逗我 刚要删突然感觉dinic的模 ...
- ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)
将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- P3381 【模板】最小费用最大流
P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...
随机推荐
- 关于echarts绘图,主题的更换
echarts主题进行更换步骤: 模块化单文件引入(推荐) 1.查看自己想要的主题 echarts官网 http://echarts.baidu.com/echarts2/doc/example/t ...
- spring下载dist.zip
http://repo.springsource.org/libs-release-local/org/springframework/spring/ 选择对应版本下载即可
- StructureMap Exception Code: 207 Internal exception while creating Instance '06fc8bd7-76db-47c1-8d71-31090a074f5e' of PluginType QIMS.Repository.IComStaffRepository. Check the inner exception for more
标题翻译: StructureMap异常代码:207内部异常,同时创造PluginType QIMS.Repository.IComStaffRepository的实例“06fc8bd7-76db-4 ...
- listView中的button控件获取item的索引
在listview中的listitem设置事件响应,如果listitem中有button控件,这时候listitem就不会捕获到点击事件,而默认的是listitem中的button会捕获点击事件.那么 ...
- Android 控件 之 Menu 菜单
http://www.cnblogs.com/Mrs-cc/archive/2012/07/21/2603042.html 1.OptionsMenu (选项菜单)用法总结 使用方法: 方法一:添 ...
- OpenGL ES 3.0 点,线,三角形绘制形式总结
OpenGL ES 3.0 顶点 -1, 1, 0, -0.5f, 0, 0, 0, -1, 0, -1, 0, 0, 0.5f, 0, 0, 1, -1, ...
- C# DateTime
//c datetime 格式化DateTime dt = DateTime.Now;Label1.Text = dt.ToString();//2005-11-5 13:21:25Label2.Te ...
- 实现Bootstrap Carousel Fade Transition 淡入淡出效果
html代码: <div id="carousel" class="carousel slide carousel-fade" data-ride=&qu ...
- Spring在代码中获取bean的几种方式(转:http://www.dexcoder.com/selfly/article/326)
方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObj ...
- phpcms V9 数据模型基类(转)
转自:http://www.cnblogs.com/Braveliu/p/5100421.html 在学习<phpcms V9首页模板文件解析>的第七步,我们看到content_model ...