Vijos 学姐的逛街计划
线性规划,最小费用最大流。
神奇的操作。
//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 学姐的逛街计划的更多相关文章
- vijos1891 学姐的逛街计划(线性规划)
P1891学姐的逛街计划 描述 doc 最近太忙了, 每天都有课. 这不怕, doc 可以请假不去上课.偏偏学校又有规定, 任意连续 n 天中, 不得请假超过 k 天. doc 很忧伤, 因为他还要陪 ...
- Vijos1891 学姐的逛街计划 【费用流】*
Vijos1891 学姐的逛街计划 描述 doc 最近太忙了, 每天都有课. 这不怕, doc 可以请假不去上课. 偏偏学校又有规定, 任意连续 n 天中, 不得请假超过 k 天. doc 很忧伤, ...
- [vijos1891]学姐的逛街计划
学姐的逛街计划 描述 doc 最近太忙了, 每天都有课. 这不怕, d ...
- 刷题总结——学姐的逛街计划(vijos1891费用流)
题目: doc 最近太忙了, 每天都有课. 这不怕, doc 可以请假不去上课.偏偏学校又有规定, 任意连续 n 天中, 不得请假超过 k 天. doc 很忧伤, 因为他还要陪学姐去逛街呢. 后来, ...
- Vijos1901 学姐的钱包
描述 学姐每次出门逛街都要带恰好M元钱, 不过她今天却忘记带钱包了.可怜的doc只好自己凑钱给学姐, 但是他口袋里只有一元钱.好在doc的N位朋友们都特别有钱, 他们答应与doc作一些交换.其中第i位 ...
- cdoj 1328 卿学姐与诡异村庄 Label:并查集 || 二分图染色
卿学姐与诡异村庄 Time Limit: 4500/1500MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- cdoj 1329 卿学姐与魔法 优先队列
卿学姐与魔法 Time Limit: 1200/800MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Sta ...
- cdoj 1324 卿学姐与公主 线段树裸题
卿学姐与公主 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit St ...
- vijosP1903学姐的实习工资
描述 学姐去实习了, 一共实习了N天, 每一天都可以得到实习工资V[i], 这里V[1..N]被看作是整数序列.因为学姐很厉害, 所以V[1..N]是不下降的.也就是说学姐每天的工资只会越来越多, 不 ...
随机推荐
- spring:AOP面向切面编程02
参考: https://blog.csdn.net/jeffleo/article/details/54136904 一.AOP的核心概念AOP(Aspect Oriented Programming ...
- 【默默努力】fishingGame
这个捕鱼游戏挺有意思的,通过发射子弹,打鱼.打鱼的子弹会消耗金币,但是打鱼如果打到了鱼,就会奖励金币的数量. 我如果写这个的话,应该会画一个 背景海底,然后生成很多鱼的图片,还要有一个大炮,金币.大炮 ...
- pycharm远程调试和debug
目的: 通过pycharm远程连接服务器,实现在pycharm上开发,代码同步到服务器(或者可以从服务器download到pycharm),利用服务器开发环境在pycharm上debug. ...
- LoadRunner穿过防火墙运行Vuser和进行监控
LoadRunner穿过防火墙运行Vuser和进行监控 LoadRunner穿过防火墙进行测试,总结下来是2个方法:1. 在controller和Vuser的LAN中的防火墙都打开54345端口即 ...
- Docker在线文档收集
极客学院 kubernetes中文社区 易百教程
- PAT甲级——A1098 Insertion or Heap Sort
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 黑裙辉DS918+安装错误码21,安装教程 重装需要重新制作启动盘
不然报错误码21
- java基础之DateFormat类
DateFormat DateFormat类概述 DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间. 是抽象类,所以使用其子类SimpleDateFor ...
- Python学习day34-面向对象和网络编程总结
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- 12_PCA之探究用户对物品类别的喜好细分降维
案例: 探究:用户对物品类别的喜好细分降维. 背景:把用户分成几个类别,分类的依据是用户购买了哪些物品. 先看商品products.csv数据,有product_id,product_name,ais ...