ACM-ICPC 2018 焦作赛区网络预赛 F. Modular Production Line (区间K覆盖-最小费用流)
很明显的区间K覆盖模型,用费用流求解.只是这题N可达1e5,需要将点离散化.
建模方式步骤:
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.跑出最大流后,最小费用取绝对值就是能获得的最大权
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
struct Edge{
int to, next, cap, flow, cost;
} edge[MAXM];
int head[MAXN], tol;
int pre[MAXN], dis[MAXN];
bool vis[MAXN];
int N;
void init(int n)
{
N = n;
tol = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)
{
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = 0;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = 0;
edge[tol].cost = -cost;
edge[tol].flow = 0;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa(int s, int t){
queue<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(s);
while (!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for (int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].to;
if (edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost){
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if (!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
if (pre[t] == -1) return false;
else return true;
}
int minCostMaxflow(int s, int t, int &cost){
int flow = 0;
cost = 0;
while (spfa(s, t)){
int Min = INF;
for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]){
if (Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]){
edge[i].flow += Min;
edge[i ^ 1].flow -= Min;
cost += edge[i].cost * Min;
}
flow += Min;
}
return flow;
}
map<int,int> dp;
struct edg{
int u,v,w;
}ed[MAXN];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,M,K,u,v,w;
scanf("%d",&T);
map<int,int> ::iterator it;
while(T--){
dp.clear();
scanf("%d %d %d",&N, &K, &M);
for(int i=1;i<=M;++i){
scanf("%d %d %d",&u,&v,&w);
v++;
ed[i] = (edg){u,v,w};
dp[u] = dp[v] = 1;
}
int cnt=0;
for(it = dp.begin();it!=dp.end();++it){
int id = it->first;
dp[id] = ++cnt;
}
init(cnt+5);
int s = 0,t = cnt+1;
addedge(s,1,K,0);
addedge(cnt,t,K,0);
for(int i=1;i<cnt;++i){
addedge(i,i+1,INF,0);
}
for(int i=1;i<=M;++i){
u = ed[i].u, v = ed[i].v;
u = dp[u], v =dp[v];
addedge(u,v,1,-ed[i].w);
}
int cost;
minCostMaxflow(s,t,cost);
printf("%d\n",-cost);
}
return 0;
}
ACM-ICPC 2018 焦作赛区网络预赛 F. Modular Production Line (区间K覆盖-最小费用流)的更多相关文章
- ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- ACM-ICPC 2018 焦作赛区网络预赛
这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...
- ACM-ICPC 2018 焦作赛区网络预赛 B题 Mathematical Curse
A prince of the Science Continent was imprisoned in a castle because of his contempt for mathematics ...
- ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)
There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...
- ACM-ICPC 2018 焦作赛区网络预赛J题 Participate in E-sports
Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know ...
- ACM-ICPC 2018 焦作赛区网络预赛 K题 Transport Ship
There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry th ...
- ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- ACM-ICPC 2018 焦作赛区网络预赛 I题 Save the Room
Bob is a sorcerer. He lives in a cuboid room which has a length of AA, a width of BB and a height of ...
- ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)
Now you have a string consists of uppercase letters, two integers AA and BB. We call a substring won ...
随机推荐
- Apache版Phoenix的安装(图文详解)
不多说,直接上干货! 写在前面的话 我这里,三个节点的bigdata集群.分别为master.slave1和slave2. 1.Phoenix的下载 我的HBase版本是hbase-0.98.19. ...
- string类(一、string基础)
string基础 1.字符串常量具备字符串池特性. 字符串常量在创建前,首先在字符串池中查找是否存在相同文本. 如果存在,则直接返回该对象引用:若不存在,则开辟空间存储. 目的:提高内存利用率. 2. ...
- The user specified as a definer (”@’%') does not exist解决方法
报错如下: 遇见这个问题,网上都是千篇一律,改权限( grant all privileges on *.* to root@”%” identified by “.”; flush privil ...
- Linq------错误: Unable to determine the principal end of an association between the types
[Table("bma_stores")] public class Store { //加上即可 [Required] public virtual Product Produc ...
- ionic跳转(二)
1)网上说要想在js里跳转用,$state.go()方法,但找了大半天都没找到在ionic使用$state的方法 2)要想用js跳转,直接用原生js跳转也是可以的 location.href='#ho ...
- C++编译遇到参数错误(cannot convert parameter * from 'const char [**]' to 'LPCWSTR')
转:http://blog.sina.com.cn/s/blog_9ffcd5dc01014nw9.html 前面的几天一直都在复习着被实习落下的C++基础知识.今天在复习着上次创建的窗口程序时,出现 ...
- ios UITableView高度自适应(转)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // ...
- 基于开源博客系统(jpress)搭建网站
基于开源博客系统(jpress)搭建网站 JPress 使用 Java8 开发,基于流行的JFinal和Jboot框架. 目前JPress已经内置的文章和页面其实是两个模块,可以移除和新增其他模块,因 ...
- [算法][LeetCode]Single Number——异或运算的巧妙运用
题目要求 Given an array of integers, every element appears twice except for one. Find that single one. N ...
- JUnit常用断言及注解
断言是编写测试用例的核心实现方式,即期望值是多少,测试的结果是多少,以此来判断测试是否通过. 断言核心方法 assertArrayEquals(expecteds, actuals) 查看两个数组 ...