题解:

似乎是放在费用流里的

然而是有上下界的网络流QAQ

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=;
int n,m,x,mincost,s,t,S,ss,tt,tot,fi[N],ne[N],zz[N],sl[N],c[N];
int dis[N],last[N],d[N],vis[N];
queue <int> q;
void jb(int x,int y,int cap,int z)
{
ne[++tot]=fi[x];
fi[x]=tot;
zz[tot]=y;
sl[tot]=cap;
c[tot]=z;
ne[++tot]=fi[y];
fi[y]=tot;
zz[tot]=x;
sl[tot]=;
c[tot]=-z;
}
int addflow(int s,int t)
{
int now=t,ans=1e9;
while (now!=s)
{
ans=min(ans,sl[last[now]]);
now=zz[last[now]^];
}
now=t;
while (now!=s)
{
sl[last[now]]-=ans;
sl[last[now]^]+=ans;
now=zz[last[now]^];
}
return ans;
}
int spfa(int s,int t)
{
memset(dis,,sizeof(dis));
dis[s]=;
memset(vis,,sizeof(vis));
vis[s]=;
while (!q.empty())q.pop();
q.push(s);
while (!q.empty())
{
int now=q.front();q.pop();
vis[now]=;
for (int i=fi[now];i!=-;i=ne[i])
if (dis[zz[i]]>dis[now]+c[i]&&sl[i])
{
dis[zz[i]]=dis[now]+c[i];
last[zz[i]]=i;
if (!vis[zz[i]])
{
vis[zz[i]]=;
q.push(zz[i]);
}
}
}
if (dis[t]>1e9) return ;
int flow=addflow(s,t);
mincost+=flow*dis[t];
return ;
}
int main()
{
tot=-;
memset(fi,-,sizeof(fi));
scanf("%d%d",&n,&m);
S=n+n+,s=S+,t=s+;ss=t+,tt=ss+;
d[s]-=m,d[S]+=m;
for (int i=;i<=n;i++)
{
scanf("%d",&x);
jb(S,i,1e9,);
jb(n+i,t,1e9,);
d[i]-=x,d[n+i]+=x;
}
for (int i=;i<n;i++)
for (int j=i+;j<=n;j++)
{
scanf("%d",&x);
if (x==-) continue;
jb(n+i,j,1e9,x);
}
for (int i=;i<=t;i++)
{
if (d[i]>)jb(ss,i,d[i],);
if (d[i]<)jb(i,tt,-d[i],);
}
jb(t,s,1e9,);
while (spfa(ss,tt));
printf("%d\n",mincost);
}
05-08 08:28