真是一道好题。。一开始自己没想出来,看完BYVoid神的题解就会做了。。https://www.byvoid.com/blog/noi-2008-employee/写的非常详细,Orz。。
最小费用流还是不难看出来,不过构图够难想的。。由这题要思考用费用流解线性规划的方法。。这题还有一什么单纯形法,以后再看了。。
代码用了一种奇怪的构图:
#include<iostream>
#include<cstdio>
#include<memory.h>
using namespace std;
struct edge{
int e,f,q,c,next;
} ed[100005];
const int inf=999999999;
int i,j,m,n,nedge,need,s,t,c,a[100005],last[100005],que[100005],dui[100005],dij[100005],minf[100005],inq[100005],road[100005];
void add(int s,int e,int f,int q=0)
{
ed[++nedge].e=e;ed[nedge].f=f;ed[nedge].q=q;ed[nedge].c=q;
ed[nedge].next=a[s];
a[s]=nedge;dui[nedge]=nedge+1;
ed[++nedge].e=s;ed[nedge].f=0;ed[nedge].q=inf;ed[nedge].c=-q;
ed[nedge].next=a[e];
a[e]=nedge;dui[nedge]=nedge-1;
}
bool spfa(int s,int t)
{
int get,head,tail,hh,tt,j,i;
for (i=0;i<=n+2;i++)
dij[i]=inf,minf[i]=inf,inq[i]=0,last[i]=-1,road[i]=-1;
dij[s]=0;
hh=tt=head=tail=1;
que[1]=s;inq[s]=1;
while (hh<=tt)
{
get=que[head]; //printf("%d %d\n",get,dij[t]);
for (j=a[get];j;j=ed[j].next)
if (ed[j].f>0&&dij[get]+ed[j].q<dij[ed[j].e])
{
dij[ed[j].e]=dij[get]+ed[j].q;
minf[ed[j].e]=min(minf[get],ed[j].f);
last[ed[j].e]=get;road[ed[j].e]=j;
if (!inq[ed[j].e])
{
tt++;tail++;
if (tail>100000) tail=1;
que[tail]=ed[j].e;
inq[ed[j].e]=1;
}
}
hh++;head++;
if (head>100000) head=1;
inq[get]=0;
}
return last[t]!=-1;
}
int fyl(int s,int t)
{
int ans=0,i,c,j;
while (spfa(s,t))
{
i=t;ans+=dij[t]*minf[t];
while (last[i]!=-1)
{
j=road[i];
ed[j].f-=minf[t];ed[dui[j]].f+=minf[t];
if (ed[j].f) ed[j].q=ed[j].c; else ed[j].q=inf;
if (ed[dui[j]].f) ed[dui[j]].q=ed[dui[j]].c; else ed[dui[j]].q=inf;
// printf("%d %d %d %d %d %d\n",ed[j].e,i,ed[j].f,ed[j].q,ed[dui[j]].f,ed[dui[j]].q);
i=last[i];
}
// printf("**%d %d %d\n",dij[t],minf[t],ans);
}
return ans;
}
int main()
{
freopen("employee.in","r",stdin);
scanf("%d%d",&n,&m);
memset(a,0,sizeof(a));
for (i=1;i<=n;i++)
{
scanf("%d",&need);
add(0,i,need);add(i+1,n+2,need);add(i+1,i,inf);
}
for (i=1;i<=m;i++)
{
scanf("%d%d%d",&s,&t,&c);
add(s,t+1,inf,c);
}
printf("%d\n",fyl(0,n+2));
}