Modular Production Line

时空限制: 1000ms /65536K

 

An automobile factory has a car production line. Now the market is oversupply and the production line is often shut down. To make full use of resources, the manager divides the entire production line into N parts(1...N). Some continuous parts can produce sub-products. And each of sub-products has their own value. The manager will use spare time to produce sub-products to make money. Because of the limited spare time, each part of the production line could only work at most K times. And Because of the limited materials, each of the sub-products could be produced only once. The manager wants to know the maximum value could he make by produce sub-products.

Input

The first line of input is T, the number of test case.

The first line of each test case contains three integers,N,K and M. (M is the number of different sub-product).

The next MM lines each contain three integers Ai​,Bi​,Wi​ describing a sub-product. The sub-product has value Wi​. Only Ai​ to Bi​ parts work simultaneously will the sub-product be produced (include Ai​ to Bi​).

1≤T≤100

1≤K≤M≤200

1≤N≤10^5

1≤Ai​≤Bi​≤N

1≤Wi​≤10^5

Output

For each test case output the maximum value in a separate line.

样例输入

4
10 1 3
1 2 2
2 3 4
3 4 8
10 1 3
1 3 2
2 3 4
3 4 8
100000 1 3
1 100000 100000
1 2 3
100 200 300
100000 2 3
1 100000 100000
1 150 301
100 200 300

样例输出

10
8
100000
100301

题目来源

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


最大权不相交路径问题.与最长K可重区间问题几乎是一样的。

有一点不同的是在最长K可重区间问题中每个区间端点是不算在区间交集里的,而在这个问题中区间端点是算在区间交集里的。把每个区间右端点加一,就可以转化为最长K可重区间问题了。

还有一点要注意的是,在最长K可重区间问题中,参考博客给了两种解法,而这里的数据量比较大,因此只能用第二种的离散化方法来写。

#include<bits/stdc++.h>
#define INF INT_MAX/2
#define N 800
using namespace std; typedef struct
{
int u,v,next;
int flow,cost;
}ss; ss edg[*N];
int head[N];
int now_edge=; void addedge(int u,int v,int flow,int cost)
{
edg[now_edge]=(ss){u,v,head[u],flow,cost};
head[u]=now_edge++;
edg[now_edge]=(ss){v,u,head[v],,-cost};
head[v]=now_edge++;
} bool spfa(int s,int t,int &flow,int &cost)
{
int dis[N];
for(int i=;i<N;i++)dis[i]=INF;
dis[s]=; int vis[N]={};
vis[s]=; queue<int>q;
q.push(s); int addflow[N]={};
addflow[s]=INF; int pre[N]={}; while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=; for(int i=head[now];i!=-;i=edg[i].next)
{
ss e=edg[i]; if(e.flow>&&dis[e.v]>dis[now]+e.cost)
{
dis[e.v]=dis[now]+e.cost;
addflow[e.v]=min(addflow[now],e.flow);
pre[e.v]=i; if(!vis[e.v])
{
q.push(e.v);
vis[e.v]=;
}
}
}
} if(dis[t]==INF)return false; flow+=addflow[t];
cost+=addflow[t]*dis[t]; int now=t;
while(now!=s)
{
edg[pre[now]].flow-=addflow[t];
edg[pre[now]^].flow+=addflow[t];
now=edg[pre[now]].u;
} return true;
} void MCMF(int s,int t,int &flow,int &cost)
{
while(spfa(s,t,flow,cost));
} struct
{
int l,r,value;
}arr[N]; int lsh[N],len_lsh;
int f(int x)
{
return lower_bound(lsh,lsh+len_lsh,x)-lsh+;
} void init()
{
for(int i=;i<N;i++)head[i]=-;
now_edge=;
len_lsh=;
} int main()
{
int t=;
scanf("%d",&t);
while(t--)
{
init();
int n,k,m;
scanf("%d %d %d",&n,&k,&m); for(int i=;i<m;i++)
{
scanf("%d %d %d",&arr[i].l,&arr[i].r,&arr[i].value);
arr[i].r++;
lsh[len_lsh++]=arr[i].l;
lsh[len_lsh++]=arr[i].r;
} sort(lsh,lsh+len_lsh);
len_lsh=unique(lsh,lsh+len_lsh)-lsh; int s=len_lsh+,t=s+;
addedge(s,,k,);
addedge(len_lsh,t,INF,);
for(int i=;i<len_lsh;i++)addedge(i,i+,INF,); for(int i=;i<m;i++)addedge(f(arr[i].l),f(arr[i].r),,-arr[i].value); int cost=,flow=;
MCMF(s,t,flow,cost);
printf("%d\n",-cost);
}
return ;
}

Modular Production Line的更多相关文章

  1. Modular Production Line (MCMF)

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

  2. 【网络流】Modular Production Line

    [网络流]Modular Production Line 焦作上的一道,网络流24题中的原题.... https://nanti.jisuanke.com/t/31715 给出了1e5个点,但是因为最 ...

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

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

  4. 焦作F Modular Production Line 费用流

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

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

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

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

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

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

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

  8. ACM-ICPC 2018 焦作网络赛

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

  9. 计算机视觉code与软件

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

随机推荐

  1. iosopendev配置

    Permission denied, please try again.Permission denied, please try again.Permission denied (publickey ...

  2. 补题—Codeforces Round #346 (Div. 2) _智商欠费系列

    这次的题目相对容易 但是智商依旧不够用 原因有三点 1.英文水平堪忧 2 逻辑不严密 3 细节掌握不够好 传送门 http://codeforces.com/contest/659 A 题目大意 圆环 ...

  3. CPP-网络/通信:COM

    ))//打开串口 { ) { CloseCom();//关闭串口 break; } //添加处理代码. } //最后关闭串口 CloseCom();//关闭串口

  4. 换个语言学一下 Golang (1)

    做技术的总是有些拗.这么多年一直在.net的框框里打转转.直到现在市场上.net工作越来越难找,项目越来越老才发现不做出改变不行了.就从学习Go开始吧. Go语言的特点 简洁.快速.安全 并行.有趣. ...

  5. linux设置http/https proxy及忽略proxy的方法

    msys2设置网络代理 在文件 .bashrc 中添加 export http_proxy="proxy IP:port" 如 export http_proxy="19 ...

  6. 洛谷 P1514 引水入城

    这次不说闲话了,直接怼题 这道题用bfs其实并不难想,但比较困难的是怎么解决满足要求时输出蓄水厂的数量.其实就像其他题解说的那样,我们可以用bfs将它转化成一个区间覆盖问题,然后再进行贪心. 首先枚举 ...

  7. java在线聊天项目1.3版 ——设计好友列表框功能

    设计好友列表框功能,思路—— 1.当客户端成功登陆后,则客户端把成功登陆信息发送给服务端, 2.由服务端将接收到来自各个成功登陆的客户端的用户信息添加进好友列表, 3.每当有成功登陆的用户就向各个客户 ...

  8. Spring框架bean的注解管理方法之一 使用注解生成对象

    首先在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression 下载地址: https://pan.baidu.com/s/1qXLH ...

  9. MySql中引擎

    1. InnoDB 引擎 MySQL 5.5 及以后版本中的默认存储引擎,它的优点如下:灾难恢复性好,支持事务,使用行级锁,支持外键关联,支持热备份. InnoDB引擎中的表,其数据的物理组织形式是簇 ...

  10. Knockout v3.4.0 中文版教程-12-控制文本内容和外观-html绑定

    3. html绑定 目的 html绑定会使关联的DOM元素显示你参数指定的html内容. 当你的视图模型里面的值是HTML标记字符串,而你想要呈现它,这时候用html绑定特别合适. 例子 <di ...