【网络流】Modular Production Line

焦作上的一道,网络流24题中的原题....

  • https://nanti.jisuanke.com/t/31715

    给出了1e5个点,但是因为最多200条边,也就是最多用到400个点,所用先离散化,然后建图。

    建图:

    1.对权值为w的区间[u,v],加边id(u)->id(v+1),容量为1,费用为-w;

    2.对所有相邻的点加边id(i)->id(i+1),容量为正无穷,费用为0;

    3.建立源点汇点,由源点s向最左侧的点加边,容量为K,费用为0,由最右侧的点向汇点加边,容量为K,费用为0

    4.跑出最大流后,最小费用取绝对值就是能获得的最大权

离散化那里不太熟

spfa的slf优化能快200ms

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=10010;
const int maxm=100010;
const int inf=0x3f3f3f3f;
struct edge{
int to,next,cap,flow,cost;
}e[maxm];
int head[maxn],tol,cost;
int pre[maxn],dis[maxn];
bool vis[maxn];
int N;
void init(int n){
N=n;tol=1;cost=0;
memset(head,0,sizeof(head));
}
void addedge(int u,int v,int cap,int cost){
++tol;
e[tol].to=v;e[tol].next=head[u];e[tol].cap=cap;e[tol].cost=cost;e[tol].flow=0;
head[u]=tol;
++tol;
e[tol].to=u;e[tol].next=head[v];e[tol].cap=0;e[tol].flow=0;e[tol].cost=-cost;
head[v]=tol;
}
bool spfa(int s,int t){
deque<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_front(s);
while (!q.empty()){
int u=q.front();
q.pop_front();
vis[u]=0;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(e[i].cap>e[i].flow&&dis[v]>dis[u]+e[i].cost){
dis[v]=dis[u]+e[i].cost;
pre[v]=i;
if(!vis[v]){
vis[v]=true;
if(!q.empty()){
if(dis[v]<dis[q.front()]) q.push_front(v);
else q.push_back(v);
}
else q.push_back(v);
}
}
}
}
if(pre[t]==-1) return false;
else return true;
}
int mcfc(int s,int t){
int flow=0;
while (spfa(s,t)){
int minn=inf;
for(int i=pre[t];i!=-1;i=pre[e[i^1].to]){
if(minn>e[i].cap-e[i].flow){
minn=e[i].cap-e[i].flow;
}
}
for(int i=pre[t];i!=-1;i=pre[e[i^1].to]){
e[i].flow+=minn;
e[i^1].flow-=minn;
cost+=e[i].cost*minn;
}
flow+=minn;
}
return flow;
}
struct node{
int a,b,c;
}f[300];
map<int,int>mp;
int main(){
int T;scanf("%d",&T);
int n,m,k,cnt;int a,b,w;
while (T--){
scanf("%d%d%d",&n,&m,&k);
cnt=0;mp.clear();
for (int i = 1; i <= k; ++i) {
scanf("%d%d%d",&a,&b,&w);
f[i]=node{a,b+1,w};
mp[a]=mp[b+1]=1;
}
map<int,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
int id=it->first;
mp[id]=++cnt;
}
init(cnt+5);
int s=0,t=cnt+1;
addedge(0,1,m,0);
addedge(cnt,t,m,0);
for (int i = 1; i <cnt ; ++i) {
addedge(i,i+1,inf,0);
}
for (int j = 1; j <=k ; ++j) {
addedge(mp[f[j].a],mp[f[j].b],1,-f[j].c);
}
mcfc(s,t);
printf("%d\n",-cost);
}
}

【网络流】Modular Production Line的更多相关文章

  1. Modular Production Line

     Modular Production Line 时空限制: 1000ms /65536K   An automobile factory has a car production line. Now ...

  2. Modular Production Line (MCMF)

    Modular Production Line \[ Time Limit: 1000ms\quad Memory Limit: 65536kB \] 题意 给出 \(N\) 种零件,现在你可以用连续 ...

  3. 焦作F Modular Production Line 费用流

    题目链接 题解:这道题比赛的时候,学弟说是网络流,当时看N这么大,觉得网络流没法做,实际本题通过巧妙的建图,然后离散化. 先说下建图方式,首先每个覆盖区域,只有左右端点,如果我们只用左右端点的话,最多 ...

  4. ACM-ICPC 2018 焦作赛区网络预赛 F. Modular Production Line (区间K覆盖-最小费用流)

    很明显的区间K覆盖模型,用费用流求解.只是这题N可达1e5,需要将点离散化. 建模方式步骤: 1.对权值为w的区间[u,v],加边id(u)->id(v+1),容量为1,费用为-w; 2.对所有 ...

  5. ACM-ICPC 2018 焦作赛区网络预赛

    这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...

  6. ACM-ICPC 2018 焦作网络赛

    题目顺序:A F G H I K L 做题链接 A. Magic Mirror 题意:判断 给出的 字符串 是否等于"jessie",需要判断大小写 题解:1.用stl库 tolo ...

  7. 2018 ACM 网络选拔赛 焦作赛区

    A. Magic Mirror #include <cstdio> #include <cstdlib> #include <cmath> #include < ...

  8. ACM-ICPC 2018 焦作赛区网络预赛 Solution

    A. Magic Mirror 水. #include <bits/stdc++.h> using namespace std; int t; ]; inline bool work() ...

  9. 计算机视觉code与软件

    Research Code A rational methodology for lossy compression - REWIC is a software-based implementatio ...

随机推荐

  1. 全面掌握Nginx配置+快速搭建高可用架构 一 Nginx请求限制

    三次握手细节 语法: key为分配空间的关键字,以及分配空间的大小 示例: 压力测试工具ab

  2. Swift轮播控件快速入门——FSPagerView

    2018年03月01日 19:17:42 https://blog.csdn.net/sinat_21886795/article/details/79416068 今天介绍一个IOS的轮播控件FSP ...

  3. LIS是什么?【标本分拣】

    接之前[LIS是什么?]中,提到几点需要补充描述的部分. Ⅰ.标本分管处理 标本的分管处理,在医院和第三方实验室有多种叫法,例如:分拣.合管等等.这里我称之为分拣,分拣实际上分为两个部分:系统中标本分 ...

  4. VS.NET中的常用控件和类型的命名规范

    表1  命名规范 VS名称 简写 VS名称 简写 数据类型 Array arr Boolean bln Byte byt Char Chr Date Time dtm Decimal dtm Doub ...

  5. django-替代为自定义的User model

    https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model Subs ...

  6. 从内存上限说起 VMware内存分配初探

    原文链接:http://blog.51cto.com/cxpbt/463777 [IT168 应用技巧]为方便识别虚拟的资源和物理(或叫真实的)资源,本人文章中以小写字母v前缀标识虚拟资源,小写字母p ...

  7. POJ - 3660 Cow Contest(flod)

    题意:有N头牛,M个关系,每个关系A B表示编号为A的牛比编号为B的牛强,问若想将N头牛按能力排名,有多少头牛的名次是确定的. 分析: 1.a[u][v]=1表示牛u比牛v强,flod扫一遍,可以将所 ...

  8. freeswitch初识

    一.参考内容 1.<FreeSWITCH权威指南>杜金房.张令考著 2.FreeSwitch 国内源码快速下载 3.Ubuntu下freeSwitch安裝指导(推荐) 4.Freeswit ...

  9. Cracking Digital VLSI Verification Interview 第一章

    目录 Digital Logic Design Number Systems, Arithmetic and Codes Basic Gates Combinational Logic Circuit ...

  10. Springboot数据校验

    SpringBoot中使用了Hibernate-validate校验框架 1.在实体类中添加校验规则 校验规则: @NotBlank: 判断字符串是否为null或者是空串(去掉首尾空格).@NotEm ...