hdu3667 最小费用流

拆边1,3,5,7。。建图即可

#include <cstring>
#include <cstdio>
#include <queue>
#include <iostream>
#define inf 0x3f3f3f3f
#define MAXN 2000
#define MAXM 50000
using namespace std;
struct node
{
    int u,v,f,c;
};
node e[MAXM];
int first[MAXN],next[MAXM],cc,ans_flow;
int inq[MAXN],pre[MAXN],preedge[MAXN],d[MAXN];
inline void add_edge(int u,int v,int f,int c)
{
    e[cc].u=u;
    e[cc].v=v;
    e[cc].f=f;
    e[cc].c=c;
    next[cc]=first[u];
    first[u]=cc;
    cc++;

    e[cc].v=u;
    e[cc].u=v;
    e[cc].f=0;
    e[cc].c=-c;
    next[cc]=first[v];
    first[v]=cc;
    cc++;
}
int SPFA(int s,int t)
{
    memset(inq,0,sizeof(inq));
    memset(d,inf,sizeof(d));
    memset(pre,-1,sizeof(pre));
    memset(preedge,-1,sizeof(preedge));
    d[s]=0;
    queue<int> q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        inq[u]=0;
        int i;
        for(i=first[u];i!=-1;i=next[i])
        {
            int v=e[i].v;
            if(e[i].f)
            {
                if(d[v]>d[u]+e[i].c)
                {
                    d[v]=d[u]+e[i].c;
                    pre[v]=u;
                    preedge[v]=i;
                    if(!inq[v])
                    {
                        inq[v]=1;
                        q.push(v);
                    }
                }
            }
        }
    }
    if(d[t]>=inf)
        return 0;
    else
        return 1;
}
int Min_Cost_Flow(int s,int t,int k)
{
    ans_flow=0;
    int ans_cost=0,mm,tmp;
    while(SPFA(s,t))
    {
        mm=inf;
        int u=t;
        while(pre[u]!=-1)
        {
            tmp=preedge[u];
            mm=min(mm,e[tmp].f);
            u=pre[u];
        }
        u=t;
        while(pre[u]!=-1)
        {
            tmp=preedge[u];
            e[tmp].f-=mm;
            e[tmp^1].f+=mm;
            u=pre[u];
        }
        ans_flow+=mm;
        ans_cost+=mm*d[t];
    }
    return ans_cost;
}
int main()
{
    int n,m,k;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        memset(first,-1,sizeof(first));
        memset(next,-1,sizeof(next));
        cc=0;
        int i;
        for(i=0;i<m;i++)
        {
            int u,v,a,c;
            scanf("%d%d%d%d",&u,&v,&a,&c);
            int j;
            for(j=1;j<=c;j++)
            {
                add_edge(u,v,1,(2*j-1)*a);
            }
        }
        int s=0,t=n;
        add_edge(0,1,k,0);
        int res=Min_Cost_Flow(s,t,k);
        if(ans_flow<k)
            printf("-1\n");
        else
        {
            printf("%d\n",res);
        }
    }

    return 0;
}


你可能感兴趣的:(hdu3667 最小费用流)