【网络流】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. Spring学习(三)——@PropertySource,@ImportResource,@Bean注解

    @PropertySource注解是将配置文件中 的值赋值给POJO 项目结构如下 一.创建一个Person.Java文件: import org.springframework.boot.conte ...

  2. POJ 3126:Prime Path

    Prime Path Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit St ...

  3. python counter、闭包、generator、解数学方程、异常

    1.counter 2.闭包 3.generator 4.解数学方程 5.异常 1.python库——counter from collections import Counter breakfast ...

  4. 两个exe共享内存数据

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  5. 吴裕雄--天生自然MySQL学习笔记:MySQL 连接

    使用mysql二进制方式连接 您可以使用MySQL二进制方式进入到mysql命令提示符下来连接MySQL数据库. 实例 以下是从命令行中连接mysql服务器的简单实例: [root@host]# my ...

  6. postman批量接口测试注意事项

    1.使用cvs文件 导入文件后最后行出现\r符号 用文本打开 删除最后一行空白行 2.打印cvs文件中的接口调用的参数 Pre-request Script: var beginDate=data.b ...

  7. Java中的四种引用类型比较

    1.引用的概念 引用这个概念是与JAVA虚拟机的垃圾回收有关的,不同的引用类型对应不同的垃圾回收策略或时机. 垃圾收集可能是大家感到难于理解的较难的概念之一,因为它并不能总是毫无遗漏地解决Java运行 ...

  8. SEO初步学习之影响网站排名的因素

    本文介绍一些比较明显的因素,一些隐藏较深的原因还有待发掘: 1.采集网站内容,即抄袭其他网站的内容. 2.新站上传后建议不要有大的改动. 3.标题频繁修改. 4.大量投放垃圾外链. 5.不做友链,交友 ...

  9. zabbix使用短信猫实现报警

    因为公司运维的对象是政府单位,所以在实际的监控过程中无法连接到外网,所以最后报警选择的媒介是短信猫,下边就是具体的实施过程. 一.面临的问题 因为手头上的设备是串口的短信猫,但是zabbix serv ...

  10. share团队冲刺6

    团队冲刺第六天 昨天:进行各种原件的自定义样式,进行界面布局 登陆界面: 今天:进行后台的代码编写,实现各种按钮的功能 问题:在不同的型号手机上,界面会发生不兼容的问题.