线性规划:

 #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]志愿者招募的更多相关文章

  1. 洛谷P3980 [NOI2008]志愿者招募

    题解 最小费用最大流 每一天是一条边\((inf-a[i], 0)\) 然后对于一类志愿者, 区间两端连一条\((inf, c[i])\) \(S\)向第一个点连\((inf, 0)\) 最后一个点向 ...

  2. Solution -「NOI 2008」「洛谷 P3980」志愿者招募

    \(\mathcal{Description}\)   Link.   一项持续 \(n\) 天的任务,第 \(i\) 天需要至少 \(a_i\) 人工作.还有 \(m\) 种雇佣方式,第 \(i\) ...

  3. 【洛谷】P3980 [NOI2008]志愿者招募

    [洛谷]P3980 [NOI2008]志愿者招募 我居然现在才会用费用流解线性规划-- 当然这里解决的一类问题比较特殊 以式子作为点,变量作为边,然后要求就是变量在不同的式子里出现了两次,系数一次为+ ...

  4. P3980 [NOI2008]志愿者招募 费用流 (人有多大胆地有多大产

    https://www.luogu.org/problemnew/show/P3980 感觉费用流比网络流的图更难想到,要更大胆.首先由于日期是连续的,所以图中的点是横向排列的. 这道题有点绕道走的意 ...

  5. P3980 [NOI2008]志愿者招募

    思路 巧妙的建图 因为每个志愿者有工作的时段,所以考虑让一个志愿者的流量能够从S流到T产生贡献 所以每个i向i+1连INF-a[x]的边(类似于k可重区间集),每个si向ti连边cap=INF,cos ...

  6. luogu P3980 [NOI2008]志愿者招募

    传送门 网络流又一神仙套路应用 首先考虑列不等式,设\(x_i\)为第i种人的个数,记\(b_{i,j}\)为第i种人第j天是否能工作,那么可以列出n个不等式,第j个为\(\sum_{i=1}^{m} ...

  7. P3980 [NOI2008]志愿者招募 (费用流)

    题意:最多1000天 每天需要至少ai个工人施工 有10000种工人可以雇佣 每种工人可以工作si到ti天 雇佣一个的花费是ci 问怎样安排使得施工花费最少 思考:最直白的建模方式 就是每种工人可以和 ...

  8. BZOJ 1061: [Noi2008]志愿者招募

    1061: [Noi2008]志愿者招募 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 4064  Solved: 2476[Submit][Stat ...

  9. BZOJ 1061: [Noi2008]志愿者招募 [单纯形法]【学习笔记】

    1061: [Noi2008]志愿者招募 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 3975  Solved: 2421[Submit][Stat ...

  10. [BZOJ1061][Noi2008]志愿者招募

    [BZOJ1061][Noi2008]志愿者招募 试题描述 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难 题:为即将启动的奥运新项目招募一批短期志愿 ...

随机推荐

  1. Alpha冲刺Day11

    Alpha冲刺Day11 一:站立式会议 今日安排: 由周静平继续完成昨日第三方机构剩余的核实企业风险数据和企业风险数据详情模块 由张梨贤和黄腾飞共同完成第三方机构的分级统计展示模块 由林静开始登录/ ...

  2. 201621123060《JAVA程序设计》第一周学习总结

    1.本周学习总结 1.讲述了JAVA的发展史,关于JDK.JRE.JVM的联系和区别 2.JDK是用JAVA开发工具.做项目的关键.JRE是JAVA的运行环境(JAVA也是JAVA语言开发的).JVM ...

  3. Bate版敏捷冲刺报告--day0

    1 团队介绍 团队组成: PM:齐爽爽(258) 小组成员:马帅(248),何健(267),蔡凯峰(285)  Git链接:https://github.com/WHUSE2017/C-team 2 ...

  4. python中使用flask时遇到的markupsafe._compat包缺失的问题与解决

    环境:windows7 + python3.6.0 在尝试使用python的flask时,按照flask的tutorial操作,装好flask.venv后,对tutorial中的hello.py进行运 ...

  5. Java的暑期作业

    Java暑期作业 一.<恶意>读书笔记 <恶意>是日本作家东野圭吾写的推理小说之一.看完后不禁为东野先生的奇特的写作手法以及书中所展现的人性的丑恶所震撼.我认为这本书相较< ...

  6. 织梦dedecms默认网站地图sitemap.html优化

    网站地图对于网站优化很重要,搜索引擎就是靠网站地图去收录网站页面,本文主要讲解优化织梦自带的网站地图功能.     织梦自带的网站地图使用方法:织梦后台--生成--HTML更新--更新网站地图,可以在 ...

  7. vue初尝试--项目结构

    新建一个项目之后,我们来看一下项目的目录结构 几个主要文件的内容 index.html文件(入口文件,系统进入之后先进入index.html) <!DOCTYPE html> <ht ...

  8. 新概念英语(1-69)The car race

    新概念英语(1-69)The car race Which car was the winner in 1995 ? There is  car race near our town every ye ...

  9. 新概念英语(1-59)Is that all

    Does the lady buy any chalk? A:I want some envelopes, please. B:Do you want the large size or the sm ...

  10. 在Android项目中使用Java8

    前言 在过去的文章中我介绍过Java8的一些新特性,包括: Java8新特性第1章(Lambda表达式) Java8新特性第2章(接口默认方法) Java8新特性第3章(Stream API) 之前由 ...