【网络流】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. js 月份选择器(只选择到月)

    需要如下js https://pan.baidu.com/s/1c1T9wY0 在html中添加如下代码 <input onclick="setmonth(this)" /& ...

  2. 从架构师视角看是否该用Kotlin做服务端开发?

    前言 自从Oracle收购Sun之后,对Java收费或加强控制的尝试从未间断,谷歌与Oracle围绕Java API的官司也跌宕起伏.虽然Oracle只是针对Oracle JDK8的升级收费,并释放了 ...

  3. 美素数(HDU 4548)(打表,简化时间复杂度)

    相信大家都喜欢美的东西,让我们一起来看看美素数吧. 问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为"美素数",如29,本身是素数,而且2+9 = 11 ...

  4. POJ 1011:Sticks 经典搜索

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 128734   Accepted: 30173 Descrip ...

  5. 高性能集群软件keepalived

     Keepalived介绍 以下是keepalive官网上的介绍.官方站点为http://www.keepalived.org.         Keepalived is a routing sof ...

  6. Centos7 死循环登录问题

    问题:用户名和密码输入正确,登录后屏幕闪一下又回到初始的登录界面.不知道具体什么原因引起的,先记录下不知道是否正确的解决方案,网上找了些相关的方案有的也实现不了,可能这个问题跟装的虚拟机的版本也有关系 ...

  7. 不使用.h .lib文件使用DLL内的函数

    #include <windows.h> typedef int (*Func)(const char *fmt, ...); //这里声明一个函数指针,typedef 关键字是必须的,好 ...

  8. String 字符串,heredoc,nowdoc

    一个字符串可以用 4 种方式表达: 单引号 双引号 heredoc 语法结构 nowdoc 语法结构(自 PHP 5.3.0 起) 单引号 定义一个字符串的最简单的方法是用单引号把它包围起来(字符 ' ...

  9. 使用log4cxx

    在java中有log4j日志模块,使用起来非常方便,在C++中也是有的,log4cxx就是log4j的c++移植版,机缘巧合之下今天想要使用一下这个日志模块,所以记录下自己从一开始下载安装到成功使用的 ...

  10. python3编码问题个人理解

    #coding=utf-8 a = "你" # 这个字符串是Unicode和 a = u“你”等价b = b'\\u4f60' #这个表示b是字节串(如果需要显示b的值则 prin ...