最小费用最大流

//**********************************//
const int maxn=250;
const int maxm=62272;
const int inf=0x4ffffff;
int n,m;
struct Edge
{
  int v,next,c,w;
}edge[maxm];

int head[maxn],cnt;
void init()
{
    cnt=0;memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int c)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].c=c;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    
    edge[cnt].v=u;
    edge[cnt].w=0;
    edge[cnt].c=-c;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int dis[maxn],pre[maxn];
int alpha[maxn];
int que[maxn],qhead,qrear;
int spfa(int s,int e)
{
    for(int i=0;i<maxn;++i)
    dis[i]=inf;
    memset(alpha,0,sizeof(alpha));
    dis[s]=0;
    alpha[s]=1;
    qrear=1;
    que[qhead=0]=s;
    while(qhead!=qrear)
    {
        int k=que[qhead++];
        qhead%=maxn;
        alpha[k]=0;
        for(int q=head[k];~q;q=edge[q].next)
        if(edge[q].w&&dis[k]+edge[q].c<dis[edge[q].v])
        {
         dis[edge[q].v]=dis[k]+edge[q].c;
         pre[edge[q].v]=q;
         if(!alpha[edge[q].v])
         {
             alpha[edge[q].v]=true;
             if(edge[q].c<0)
             {
                 qhead=(qhead-1+maxn)%maxn;
                 que[qhead]=edge[q].v;
             }
             else
             {
                 que[qrear++]=edge[q].v;
                 qrear%=maxn;
             }
          }
       }
    }

    if(dis[e]==inf)return -1;
    int k;
    for(int i=e;i!=s;i=edge[pre[i]^1].v)
    k=min(k,edge[pre[i]].w);
    return k;
}

int mincostmaxflow(int s,int t)
{
    int ans=0;
    int k;
    while(~(k=spfa(s,t)))
    {
        for(int i=t;i!=s;i=edge[pre[i]^1].v)
        {
            edge[pre[i]].w-=k;
            edge[pre[i]^1].w+=k;
        }
        ans+=k*dis[t];
    }
     return ans;
}
//**************************************//

你可能感兴趣的:(c,qq)