BZOJ传送门
Time Limit: 20 Sec Memory Limit: 162 MB
Submit: 3030 Solved: 1874
[Submit][Status][Discuss]
Description
申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管。布布刚上任就遇到了一个难题:为即将启动的奥运新项目招募一批短期志愿者。经过估算,这个项目需要N 天才能完成,其中第i 天至少需要Ai 个人。 布布通过了解得知,一共有M 类志愿者可以招募。其中第i 类可以从第Si 天工作到第Ti 天,招募费用是每人Ci 元。新官上任三把火,为了出色地完成自己的工作,布布希望用尽量少的费用招募足够的志愿者,但这并不是他的特长!于是布布找到了你,希望你帮他设计一种最优的招募方案。
Input
第一行包含两个整数N, M,表示完成项目的天数和可以招募的志愿者的种类。 接下来的一行中包含N 个非负整数,表示每天至少需要的志愿者人数。 接下来的M 行中每行包含三个整数Si, Ti, Ci,含义如上文所述。为了方便起见,我们可以认为每类志愿者的数量都是无限多的。
Output
仅包含一个整数,表示你所设计的最优方案的总费用。
Sample Input
3 3
2 3 4
1 2 2
2 3 5
3 3 2
Sample Output
14
HINT
招募第一类志愿者3名,第三类志愿者4名 30%的数据中,1 ≤ N, M ≤ 10,1 ≤ Ai ≤ 10; 100%的数据中,1 ≤ N ≤ 1000,1 ≤ M ≤ 10000,题目中其他所涉及的数据均 不超过2^31-1。
Source
题解->戳这里
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
#define N 1050
#define INF 2139062143
inline int in(int x=0,int v=1,char ch=getchar()){
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();if(ch=='-') v=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*v;
}
struct NetWork{
struct Edge{int fr,to,cap,flow,cost;}edge[N*40];
vector<int> g[N];
int n,s,t,m,b[N],d[N],a[N],p[N],v[N],cost,cnt;
inline void Add_Edge(int fr,int to,int flow,int cost){
edge[cnt++]=((Edge){fr,to,flow,0,cost});
edge[cnt++]=((Edge){to,fr,0,0,-cost});
g[fr].push_back(cnt-2);g[to].push_back(cnt-1);
}
inline int spfa(){
queue<int> q;bool b[N];int x,l;
memset(b,0,sizeof(b));memset(d,0x7f,sizeof(d));
b[s]=1,d[s]=0,a[s]=INF;q.push(s);
while(!q.empty()){
x=q.front();q.pop();b[x]=false;l=g[x].size();
for(int i=0;i<l;i++){
Edge& e=edge[g[x][i]];
if(e.cap>e.flow&&d[e.to]>d[x]+e.cost){
d[e.to]=d[x]+e.cost;
p[e.to]=g[x][i];
a[e.to]=min(a[x],e.cap-e.flow);
if(!b[e.to]){b[e.to]=1;q.push(e.to);}
}
}
}
if(d[t]==INF) return 0;
cost+=d[t]*a[t];
for(x=t;x!=s;x=edge[p[x]].fr) edge[p[x]].flow+=a[t],edge[p[x]^1].flow-=a[t];
return 1;
}
void init(){
n=in(),m=in();int x,y,z;s=n+2,t=s+1;
for(int i=1;i<=n;i++) v[i]=in();
for(int i=1;i<=m;i++) x=in(),y=in(),z=in(),Add_Edge(x,y+1,INF,z);
for(int i=1;i<=n+1;i++){
x=v[i]-v[i-1];
if(x>0) Add_Edge(s,i,x,0);else Add_Edge(i,t,-x,0);
if(i>1) Add_Edge(i,i-1,INF,0);
}
while(spfa());
printf("%d\n",cost);
}
}s;
int main(){
s.init();
return 0;
}
另一种比较神的预流填坑,跑的飞快,原链接找不到了,对不起作者QuQ
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<iostream>
using namespace std;
#define N 1050
#define U 100000
#define INF 2139062143
inline int in(int x=0,int v=1,char ch=getchar()){
while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();if(ch=='-') v=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*v;
}
struct NetWork{
struct Edge{int fr,to,cap,flow,cost;}edge[N*40];
vector<int> g[N];
int n,s,t,m,b[N],d[N],a[N],p[N],cost,cnt;
inline void Add_Edge(int fr,int to,int flow,int cost){
edge[cnt++]=((Edge){fr,to,flow,0,cost});
edge[cnt++]=((Edge){to,fr,0,0,-cost});
g[fr].push_back(cnt-2);g[to].push_back(cnt-1);
}
inline int spfa(){
queue<int> q;bool b[N];int x,l;
memset(b,0,sizeof(b));memset(d,0x7f,sizeof(d));
b[s]=1,d[s]=0,a[s]=INF;q.push(s);
while(!q.empty()){
x=q.front();q.pop();b[x]=false;l=g[x].size();
for(int i=0;i<l;i++){
Edge& e=edge[g[x][i]];
if(e.cap>e.flow&&d[e.to]>d[x]+e.cost){
d[e.to]=d[x]+e.cost;
p[e.to]=g[x][i];
a[e.to]=min(a[x],e.cap-e.flow);
if(!b[e.to]){b[e.to]=1;q.push(e.to);}
}
}
}
if(d[t]==INF) return 0;
cost+=d[t]*a[t];
for(x=t;x!=s;x=edge[p[x]].fr) edge[p[x]].flow+=a[t],edge[p[x]^1].flow-=a[t];
return 1;
}
void init(){
n=in(),m=in();int x,y,z;s=0,t=n+1;
Add_Edge(s,1,U,0);
for(int i=1;i<=n;i++) x=in(),Add_Edge(i,i+1,U-x,0);
for(int i=1;i<=m;i++) x=in(),y=in(),z=in(),Add_Edge(x,y+1,INF,z);
while(spfa());
printf("%d\n",cost);
}
}s;
int main(){s.init();return 0;}