zoukankan      html  css  js  c++  java
  • HDU 3667

    http://acm.hdu.edu.cn/showproblem.php?pid=3667

    最小费用最大流

    本题流量和费用不是线性关系,fee=a*flow*flow,所以常规套模板spfa无法得到最小费用

    观察到每条边流量上限只有5,则可以把一条流量为f的边拆成f条流量为1的边,每条边费用是a*(2*i-1)(1<=i<=f)

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    using namespace std ; 
    const int INF=0xfffffff ;
    struct node{
        int s,t,cap,cost,nxt ;
    }e[200005] ;
    int sumflow ;
    int n,m,k,cnt,head[1005],vis[1005],dis[1005],pre[1005] ;
    void add(int s,int t,int cap,int cost)
    {
        e[cnt].s=s ;e[cnt].t=t ;e[cnt].cap=cap ;e[cnt].cost=cost ;e[cnt].nxt=head[s] ;head[s]=cnt++ ;
        e[cnt].s=t ;e[cnt].t=s ;e[cnt].cap=0 ;e[cnt].cost=-cost ;e[cnt].nxt=head[t] ;head[t]=cnt++ ;
    }
    int spfa(int s,int t,int N)
    {
        for(int i=0 ;i<=N ;i++)
            dis[i]=INF ;
        dis[s]=0 ;
        memset(vis,0,sizeof(vis)) ;
        memset(pre,-1,sizeof(pre)) ;
        vis[s]=1 ;
        queue <int> q ;
        q.push(s) ;
        while(!q.empty())
        {
            int u=q.front() ;
            q.pop() ;
            vis[u]=0 ;
            for(int i=head[u] ;i!=-1 ;i=e[i].nxt)
            {
                int tt=e[i].t ;
                if(e[i].cap && dis[tt]>dis[u]+e[i].cost)
                {
                    dis[tt]=dis[u]+e[i].cost ;
                    pre[tt]=i ;
                    if(!vis[tt])
                    {
                        vis[tt]=1 ;
                        q.push(tt) ;
                    }
                }
            }
        }
        if(dis[t]==INF)return 0 ;
        return 1 ;
    }
    int MCMF(int s,int t,int N)
    {
        int flow,minflow,mincost ;
        mincost=flow=0 ;
        while(spfa(s,t,N))                                                
        {
            minflow=INF ;
            for(int i=pre[t] ;i!=-1 ;i=pre[e[i].s])
                minflow=min(minflow,e[i].cap) ;
            flow+=minflow ;
            for(int i=pre[t] ;i!=-1 ;i=pre[e[i].s])
            {
                e[i].cap-=minflow ;
                e[i^1].cap+=minflow ;
            }
            mincost+=dis[t]*minflow ;
        }
        sumflow=flow ;//最大流 
        if(sumflow<k)return -1 ;
        return mincost ;
    }
    int main()
    {
        while(~scanf("%d%d%d",&n,&m,&k))
        {
            cnt=0 ;
            memset(head,-1,sizeof(head)) ;
            while(m--)
            {
                int u,v,a,c ;
                scanf("%d%d%d%d",&u,&v,&a,&c) ;
                for(int i=1 ;i<=c ;i++)
                    add(u,v,1,a*(2*i-1)) ;//Σa*(2*i-1)=a*c*c 
            }
            add(0,1,k,0) ;
            printf("%d
    ",MCMF(0,n,n+1)) ;
        }
        return 0 ;
    }
    View Code
  • 相关阅读:
    bzoj 3308 九月的咖啡店
    8.13模拟赛
    8.10模拟赛
    8.9模拟赛
    8.8模拟赛
    Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)
    BZOJ 2957: 楼房重建 (分块)
    SPOJ BGSHOOT
    Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)
    Lightoj-1356 Prime Independence(质因子分解)(Hopcroft-Karp优化的最大匹配)
  • 原文地址:https://www.cnblogs.com/xiaohongmao/p/3714633.html
Copyright © 2011-2022 走看看