洛谷P3980:[NOI2008]志愿者招募
线性规划:
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define rint register int
#define ll long long
#define MAXN 1000+10
#define pb push_back
#define INF 0x7f7f7f7f
#define oo 0x7f7f7f7f7f7f7f7f
#define pil pair<int,ll>
#define mp make_pair
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if('-'==ch)f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct E{
int from,to,cap,flow;
ll cost;
E(int x=,int y=,int c=,int f=,ll w=0LL){
from=x,to=y,cap=c,flow=f,cost=w;
}
};
struct Dinic{
int n,m,s,t;
vector<E> es;
vector<int> G[MAXN];
void init(int n,int s,int t){
this->n=n;
this->s=s,this->t=t;
es.clear();
for(int i=;i<=n;i++)G[i].clear();
}
void add(int x,int y,int cap,ll cost){
es.pb(E(x,y,cap,,cost));
es.pb(E(y,x,,,-cost));
m=es.size();
G[x].pb(m-),G[y].pb(m-);
}
int p[MAXN],a[MAXN];
ll d[MAXN];
int b[MAXN];
bool SPFA(int &flow,ll &cost){
p[s]=,a[s]=INF;
memset(d,0x7f,sizeof(d));
d[s]=;
memset(b,,sizeof(b));
b[s]=;
queue<int> q;
q.push(s);
while(!q.empty()){
int x=q.front();q.pop();b[x]=;
for(rint i=;i<G[x].size();i++){
E &e=es[G[x][i]];
if(e.cap>e.flow&&d[e.to]>d[x]+e.cost){
p[e.to]=G[x][i];
a[e.to]=min(a[x],e.cap-e.flow);
d[e.to]=d[x]+e.cost;
if(!b[e.to]){
b[e.to]=;
q.push(e.to);
}
}
}
}
if(oo==d[t]){
return ;
}
flow+=a[t];
cost+=a[t]*d[t];
for(rint i=t;i!=s;i=es[p[i]].from){
es[p[i]].flow+=a[t];
es[p[i]^].flow-=a[t];
}
return ;
}
pil MaxfMinc(){
int flow=;
ll cost=0LL;
while(SPFA(flow,cost));
return mp(flow,cost);
}
}D;
int n,m,s=,t=MAXN-;
int a[MAXN];
void init(){
D.init(t,s,t);
n=read(),m=read();
for(rint i=;i<=n;i++){
a[i]=read();
}
int x,y,z;
for(rint i=;i<=m;i++){
x=read(),y=read(),z=read();
D.add(x,y+,INF,1LL*z);
}
for(rint i=;i<=n+;i++){
x=a[i]-a[i-];
if(x>)D.add(s,i,x,0LL);
else D.add(i,t,-x,0LL);
if(i>)D.add(i,i-,INF,0LL);
}
}
int main()
{
init();
printf("%lld\n",D.MaxfMinc().second);
return ;
}
转化为最小费用最大流
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define rint register int
#define ll long long
#define MAXN 1000+10
#define pb push_back
#define INF 0x7f7f7f7f
#define oo 0x7f7f7f7f7f7f7f7f
#define pil pair<int,ll>
#define mp make_pair
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if('-'==ch)f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct E{
int from,to,cap,flow;
ll cost;
E(int x=,int y=,int c=,int f=,ll w=0LL){
from=x,to=y,cap=c,flow=f,cost=w;
}
};
struct Dinic{
int n,m,s,t;
vector<E> es;
vector<int> G[MAXN];
void init(int n,int s,int t){
this->n=n;
this->s=s,this->t=t;
es.clear();
for(int i=;i<=n;i++)G[i].clear();
}
void add(int x,int y,int cap,ll cost){
es.pb(E(x,y,cap,,cost));
es.pb(E(y,x,,,-cost));
m=es.size();
G[x].pb(m-),G[y].pb(m-);
}
int p[MAXN],a[MAXN];
ll d[MAXN];
int b[MAXN];
bool SPFA(int &flow,ll &cost){
p[s]=,a[s]=INF;
memset(d,0x7f,sizeof(d));
d[s]=;
memset(b,,sizeof(b));
b[s]=;
queue<int> q;
q.push(s);
while(!q.empty()){
int x=q.front();q.pop();b[x]=;
for(rint i=;i<G[x].size();i++){
E &e=es[G[x][i]];
if(e.cap>e.flow&&d[e.to]>d[x]+e.cost){
p[e.to]=G[x][i];
a[e.to]=min(a[x],e.cap-e.flow);
d[e.to]=d[x]+e.cost;
if(!b[e.to]){
b[e.to]=;
q.push(e.to);
}
}
}
}
if(oo==d[t]){
return ;
}
flow+=a[t];
cost+=a[t]*d[t];
for(rint i=t;i!=s;i=es[p[i]].from){
es[p[i]].flow+=a[t];
es[p[i]^].flow-=a[t];
}
return ;
}
pil MaxfMinc(){
int flow=;
ll cost=0LL;
while(SPFA(flow,cost));
return mp(flow,cost);
}
}D;
int n,m,s=,t=MAXN-;
int main()
{
D.init(t,s,t);
int k;
n=read(),m=read();
for(rint i=;i<=n;i++){
k=read();
D.add(i,i+,INF-k,0LL);
}
D.add(s,,INF,0LL);
D.add(n+,t,INF,0LL);
int x,y;
for(rint i=;i<=m;i++){
x=read(),y=read(),k=read();
D.add(x,y+,INF,1LL*k);
}
printf("%lld\n",D.MaxfMinc().second);
return ;
}
洛谷P3980:[NOI2008]志愿者招募的更多相关文章
- 洛谷P3980 [NOI2008]志愿者招募
题解 最小费用最大流 每一天是一条边\((inf-a[i], 0)\) 然后对于一类志愿者, 区间两端连一条\((inf, c[i])\) \(S\)向第一个点连\((inf, 0)\) 最后一个点向 ...
- Solution -「NOI 2008」「洛谷 P3980」志愿者招募
\(\mathcal{Description}\) Link. 一项持续 \(n\) 天的任务,第 \(i\) 天需要至少 \(a_i\) 人工作.还有 \(m\) 种雇佣方式,第 \(i\) ...
- 【洛谷】P3980 [NOI2008]志愿者招募
[洛谷]P3980 [NOI2008]志愿者招募 我居然现在才会用费用流解线性规划-- 当然这里解决的一类问题比较特殊 以式子作为点,变量作为边,然后要求就是变量在不同的式子里出现了两次,系数一次为+ ...
- P3980 [NOI2008]志愿者招募 费用流 (人有多大胆地有多大产
https://www.luogu.org/problemnew/show/P3980 感觉费用流比网络流的图更难想到,要更大胆.首先由于日期是连续的,所以图中的点是横向排列的. 这道题有点绕道走的意 ...
- P3980 [NOI2008]志愿者招募
思路 巧妙的建图 因为每个志愿者有工作的时段,所以考虑让一个志愿者的流量能够从S流到T产生贡献 所以每个i向i+1连INF-a[x]的边(类似于k可重区间集),每个si向ti连边cap=INF,cos ...
- luogu P3980 [NOI2008]志愿者招募
传送门 网络流又一神仙套路应用 首先考虑列不等式,设\(x_i\)为第i种人的个数,记\(b_{i,j}\)为第i种人第j天是否能工作,那么可以列出n个不等式,第j个为\(\sum_{i=1}^{m} ...
- P3980 [NOI2008]志愿者招募 (费用流)
题意:最多1000天 每天需要至少ai个工人施工 有10000种工人可以雇佣 每种工人可以工作si到ti天 雇佣一个的花费是ci 问怎样安排使得施工花费最少 思考:最直白的建模方式 就是每种工人可以和 ...
- BZOJ 1061: [Noi2008]志愿者招募
1061: [Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 4064 Solved: 2476[Submit][Stat ...
- BZOJ 1061: [Noi2008]志愿者招募 [单纯形法]【学习笔记】
1061: [Noi2008]志愿者招募 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3975 Solved: 2421[Submit][Stat ...
- [BZOJ1061][Noi2008]志愿者招募
[BZOJ1061][Noi2008]志愿者招募 试题描述 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难 题:为即将启动的奥运新项目招募一批短期志愿 ...
随机推荐
- Alpha第八天
Alpha第八天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- 201621123062《java程序设计》第十周作业总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 思维导图: 2. 书面作业 本次PTA作业题集异常 2.1. 常用异常 结合题集题目7-1回答 2.1.1 自己以前 ...
- 14-TypeScript简单工厂模式
在TypeScript中,要调用功能,通常在调用方通过实例化被调用方对象来调用相关方法,但这种实现在调用方和被调用方形成了强耦合的关系. 另外如果被调用方有种实现,在调用方需要根据场景去实例化不同的类 ...
- CSS揭秘(三)形状
Chapter 3 1. 椭圆 椭圆的实现主要依靠 border-radius 属性,该属性确定边框切圆角的半径大小,可以指定数值 px,也可以使用百分比显示 而且该属性非常灵活,四个角可以分别设置 ...
- C语言Linix服务器网络爬虫项目(一)项目初衷和网络爬虫概述
一.项目初衷和爬虫概述 1.项目初衷 本人的大学毕设就是linux上用c写的一个爬虫,现在我想把它完善起来,让他像一个企业级别的项目.为了重复发明轮子来学习轮子的原理,我们不使用第三方框架(这里是说的 ...
- webapi 使用Autofac 开发经历
2018/4/6 号 早上五点..被手机震动吵醒. 之后直接打开电脑,打算再加强下我自己的webapi这套东西. 虽然三年的工作经验接触了N多框架和各种风格的开发方式,但是让我自己来搞一套实在不会搞, ...
- Mysql数据库主从配置
一.为什么要使用数据库主从架构 一个网站损耗资源最厉害的就是数据库,最易崩溃的也是数据库,而数据库崩溃带来的后果是非常严重的.数据库分为读和写操作,在实际的应用中,读操作的损耗远比写操作多太多,因此读 ...
- Python内置函数(55)——globals
英文文档: globals() Return a dictionary representing the current global symbol table. This is always the ...
- 基于python的统计公报关键数据爬取
# -*- coding: utf-8 -*- """ Created on Wed Nov 8 14:23:14 2017 @author: 123 "&qu ...
- leetcode算法: Keyboard Row
Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...