传送门

题解传送门

线性规划,最小费用最大流。

神奇的操作。

//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<queue>
#include<ctime>
#include<cmath>
#define inf 1e18
const int N=;
typedef long long LL;
using namespace std;
int n,k;
LL c[N]; template<typename T> void read(T &x) {
char ch=getchar(); x=; T f=;
while(ch!='-'&&(ch<''||ch>'')) ch=getchar();
if(ch=='-') f=-,ch=getchar();
for(;ch>=''&&ch<='';ch=getchar()) x=x*+ch-''; x*=f;
} struct edge {
int u,v,nx;
LL fl,cap,cost;
edge(){}
edge(int u,int v,LL fl,LL cap,LL cost,int nx):u(u),v(v),fl(fl),cap(cap),cost(cost),nx(nx){}
}e[N]; int fir[N],ecnt=;
void add(int u,int v,LL cap,LL cost) {
e[++ecnt]=edge(u,v,,cap,cost,fir[u]); fir[u]=ecnt;
e[++ecnt]=edge(v,u,,,-cost,fir[v]); fir[v]=ecnt;
} int vis[N],p[N];
LL dis[N];
queue<int>que;
int spfa(int s,int t) {
for(int i=;i<=n;i++) vis[i]=,dis[i]=inf;
que.push(s); dis[s]=;
while(!que.empty()) {
int x=que.front();
que.pop(); vis[x]=;
for(int i=fir[x];i;i=e[i].nx)
if(e[i].cap>e[i].fl) {
int y=e[i].v;
if(dis[y]>dis[x]+e[i].cost) {
dis[y]=dis[x]+e[i].cost;
p[y]=i;
if(!vis[y]) {
vis[y]=;
que.push(y);
}
}
}
}
return dis[t]!=inf;
} LL calc(int s,int t) {
LL fl=inf,res=;
for(int i=t;i!=s;i=e[p[i]].u)
fl=min(fl,e[p[i]].cap-e[p[i]].fl),
res+=e[p[i]].cost;
for(int i=t;i!=s;i=e[p[i]].u)
e[p[i]].fl+=fl,e[p[i]^].fl-=fl;
return fl*res;
} LL max_flow(int s,int t) {
LL res=;
while(spfa(s,t))
res+=calc(s,t);
return res;
} int main() {
read(n); read(k);
for(int i=;i<=*n;i++) read(c[i]);
int s=*n+,t=s+;
add(s,*n+,k,);
add(*n+,t,k,);
for(int i=;i<=n;i++) {
add(i,*n+,,-c[i]);
add(*n+,n+i,,-c[*n+i]);
add(n+i,i,,-c[n+i]);
}
for(int i=;i<=*n;i++) add(i,i-,k,);
add(,*n+,k,);
add(*n+,*n,k,);
n=t;
LL ans=-max_flow(s,t);
printf("%lld\n",ans);
return ;
}

Vijos 学姐的逛街计划的更多相关文章

  1. vijos1891 学姐的逛街计划(线性规划)

    P1891学姐的逛街计划 描述 doc 最近太忙了, 每天都有课. 这不怕, doc 可以请假不去上课.偏偏学校又有规定, 任意连续 n 天中, 不得请假超过 k 天. doc 很忧伤, 因为他还要陪 ...

  2. Vijos1891 学姐的逛街计划 【费用流】*

    Vijos1891 学姐的逛街计划 描述 doc 最近太忙了, 每天都有课. 这不怕, doc 可以请假不去上课. 偏偏学校又有规定, 任意连续 n 天中, 不得请假超过 k 天. doc 很忧伤, ...

  3. [vijos1891]学姐的逛街计划

                                                                     学姐的逛街计划 描述 doc 最近太忙了, 每天都有课. 这不怕, d ...

  4. 刷题总结——学姐的逛街计划(vijos1891费用流)

    题目: doc 最近太忙了, 每天都有课. 这不怕, doc 可以请假不去上课.偏偏学校又有规定, 任意连续 n 天中, 不得请假超过 k 天. doc 很忧伤, 因为他还要陪学姐去逛街呢. 后来, ...

  5. Vijos1901 学姐的钱包

    描述 学姐每次出门逛街都要带恰好M元钱, 不过她今天却忘记带钱包了.可怜的doc只好自己凑钱给学姐, 但是他口袋里只有一元钱.好在doc的N位朋友们都特别有钱, 他们答应与doc作一些交换.其中第i位 ...

  6. cdoj 1328 卿学姐与诡异村庄 Label:并查集 || 二分图染色

    卿学姐与诡异村庄 Time Limit: 4500/1500MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...

  7. cdoj 1329 卿学姐与魔法 优先队列

    卿学姐与魔法 Time Limit: 1200/800MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...

  8. cdoj 1324 卿学姐与公主 线段树裸题

    卿学姐与公主 Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit St ...

  9. vijosP1903学姐的实习工资

    描述 学姐去实习了, 一共实习了N天, 每一天都可以得到实习工资V[i], 这里V[1..N]被看作是整数序列.因为学姐很厉害, 所以V[1..N]是不下降的.也就是说学姐每天的工资只会越来越多, 不 ...

随机推荐

  1. 线性回归代码实现(matlab)

    1 代价函数实现(cost function) function J = computeCost(X, y, theta) %COMPUTECOST Compute cost for linear r ...

  2. iOS开发系列-线程状态

    概述 线程从创建到销毁中间存在很多种状态. 线程的状态 通过NSThread创建一条线程,开发者需要负责线程的创建和执行,线程的销毁由系统决定.创建一个继承NSThread的FMThread类,重写d ...

  3. 2019-5-21-Total-Commander-显示文件包含文件名扩展

    title author date CreateTime categories Total Commander 显示文件包含文件名扩展 lindexi 2019-5-21 11:37:6 +0800 ...

  4. 【学术篇】luogu2778 [AHOI2016初中组]迷宫(代码高能!)

    好久好久我都没有刷题了. 题目の传送门:https://www.luogu.org/problem/show?pid=2778 题目大意:(啥 题目讲得不够清楚?)平面内有n个以整点(就是坐标都是整数 ...

  5. ubuntu解压/压缩rar文件

    一般通过默认安装的ubuntu是不能解压rar文件的,只有在安装了rar解压工具之后,才可以解压.其实在ubuntu下安装rar解压工具是非常简单的,只需要两个步骤就可以迅速搞定.ubuntu 下ra ...

  6. 扩展kmp板子

    using namespace std; #include <cstdio> #include <cstring> #include <algorithm> #de ...

  7. LUOGU P2580 于是他错误的点名开始了(trie树)

    传送门 解题思路 trie树模板

  8. C++数组或vector求最大值最小值

    可以用max_element()及min_element()函数,二者返回的都是迭代器或指针. 头文件:#include<algorithm> 1.求数组的最大值或最小值 1)vector ...

  9. UltraISO刻录CentOS 7安装指南

    CentOS 7.2 安装指南(U盘版) 一.准备阶段 1.下载CentOS7镜像文件(ISO文件)到自己电脑,官网下载路径: http://isoredirect.centos.org/centos ...

  10. Python学习day36-并发编程(2)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...