• UVA1486 Transportation(最小费用最大流)


    题意:

    N个点(1<=N<=100)M(1<=M<=5000)条有向边,运送K(1<=K<=100)个单位商品,每条边都有一个系数ai(0<ai<=100)和容量ci(ci<=5),若运送x个单位的话,就得交ai*x^2美元.

    问你,将K个单位商品从1运到N,最小花费.若运送不到,输出-1;

    分析:因为容量Ci比较小,故可以拆边,把容量为Ci的边拆成Ci条边,每条边的花费为xi*xi*ai-(xi-1)(xi-1)*ai;

    详细见代码

    // File Name: 1486.cpp
    // Author: Zlbing
    // Created Time: 2013/4/26 13:53:55
    
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdlib>
    #include<cstdio>
    #include<set>
    #include<map>
    #include<vector>
    #include<cstring>
    #include<stack>
    #include<cmath>
    #include<queue>
    using namespace std;
    #define CL(x,v); memset(x,v,sizeof(x));
    #define INF 0x3f3f3f3f
    #define LL long long
    #define REP(i,r,n) for(int i=r;i<=n;i++)
    #define RREP(i,n,r) for(int i=n;i>=r;i--)
    const int MAXN=205;
    struct Edge{
        int from,to,cap,flow,cost;
    };
    struct MCMF{
        int n,m,s,t;
        vector<Edge>edges;
        vector<int> G[MAXN];
        int inq[MAXN];
        int d[MAXN];
        int p[MAXN];
        int a[MAXN];
        void init(int n){
            this->n=n;
            for(int i=0;i<=n;i++)G[i].clear();
            edges.clear();
        }
        void AddEdge(int from,int to,int cap,int cost){
            edges.push_back((Edge){from,to,cap,0,cost});
            edges.push_back((Edge){to,from,0,0,-cost});
            m=edges.size();
            G[from].push_back(m-2);
            G[to].push_back(m-1);
        }
        bool BellmanFord(int s,int t,int& flow,int& cost){
            for(int i=0;i<=n;i++)d[i]=INF;
                CL(inq,0);
            d[s]=0;inq[s]=1;p[s]=0;a[s]=INF;
    
            queue<int>Q;
            Q.push(s);
            while(!Q.empty()){
                int u=Q.front();Q.pop();
                inq[u]=0;
                for(int i=0;i<G[u].size();i++){
                    Edge& e=edges[G[u][i]];
                    if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
                        d[e.to]=d[u]+e.cost;
                        p[e.to]=G[u][i];
                        a[e.to]=min(a[u],e.cap-e.flow);
                        if(!inq[e.to]){
                            Q.push(e.to);
                            inq[e.to]=1;
                        }
                    }
                }
            }
            if(d[t]==INF)return false;
            flow+=a[t];
            cost+=d[t]*a[t];
            int u=t;
            while(u!=s){
                edges[p[u]].flow+=a[t];
                edges[p[u]^1].flow-=a[t];
                u=edges[p[u]].from;
            }
            return true;
        }
        int Mincost(int s,int t,int k){
            int flow=0,cost=0;
            while(BellmanFord(s,t,flow,cost));
            if(flow!=k)
                return -1;
            else
            return cost;
        }
    };
    
    int n,m,k;
    MCMF solver;
    int main()
    {
        while(~scanf("%d%d%d",&n,&m,&k))
        {
            int a,b,c,d;
            solver.init(n);
            REP(i,1,m)
            {
                scanf("%d%d%d%d",&a,&b,&c,&d);
                REP(j,1,d)
                {
                    solver.AddEdge(a,b,1,j*j*c-(j-1)*(j-1)*c);
                }
            }
            solver.AddEdge(0,1,k,0);
            int s=0,t=n;
            int ans=solver.Mincost(s,t,k);
            printf("%d\n",ans);
        }
        return 0;
    }
  • 相关阅读:
    SpringMVC从Request域中获取数据
    SpringMVC重定向
    SpringMVC的请求转发的三种方法
    SpringMVC文件上传
    SpringMVC处理请求释放静态资源的三种方式
    jackson实现json转换
    SpringMVC之请求部分
    SpringMVC的执行流程
    Java [Leetcode 39]Combination Sum
    深入解析Java对象的hashCode和hashCode在HashMap的底层数据结构的应用
  • 原文地址:https://www.cnblogs.com/arbitrary/p/3044842.html
Copyright © 2020-2023  润新知