负载平衡问题(最小费用流)

来源:互联网 发布:手机加油软件代理 编辑:程序博客网 时间:2024/06/05 08:39


Description

    G 公司有n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等。如何用最少搬运量可以使n 个仓库的库存数量相同。搬运货物时,只能在相邻的仓库之间搬运。
    对于给定的n 个环形排列的仓库的库存量,编程计算使n 个仓库的库存数量相同的最少搬运量。

Input

多组数据输入.
每组输入第1 行中有1 个正整数n(n<=100),表示有n个仓库。第2 行中有n个正整数,表示n个仓库的库存量。

Output

每组输出最少搬运量

Sample Input

5
17 9 14 16 4

Sample Output

11

题目出自nefu493

思路:本题主要在于怎样去建图,这里介绍两种建图方式,主要在于对于费用流的理解。

方法一:

将每个点要分得的平均值avr算出来,如果该点a[i]比平均值多,则从源点连一条流量限制为a[i]-avr费用为0的边,如果a[i]比平均值少,则从该点连一条流量限制为avr-a[i]费用为0的边,然后相邻两个点相互连接一条流量为正无穷(oo),费用为1的边,表示调整一次的费用为1。建图的意义是从源点到汇点,让每一个多出来流量的点把流量流到少流量的点上,最终使每个点流量都相等,而且每一次调整的费用都是1,使得最后得到的费用答案等于调整次数。

#include <iostream>#include <stdio.h>#include <stdlib.h>#include<string.h>#include<algorithm>#include<math.h>#include<queue>#include<stack>using namespace std;typedef long long ll;const   int oo=1e9;const   int mm=11111111;const   int mn=888888;int node,src,dest,edge;int ver[mm],flow[mm],cost[mm],nex[mm];int head[mn],dis[mn],p[mn],q[mn],vis[mn];/**这些变量基本与最大流相同,增加了cost 表示边的费用,p记录可行流上节点对应的反向边*/void prepare(int _node,int _src,int _dest){    node=_node,src=_src,dest=_dest;    for(int i=0; i<node; i++)head[i]=-1,vis[i]=0;    edge=0;}void addedge(int u,int v,int f,int c)///起点,终点,流量,费用{    ver[edge]=v,flow[edge]=f,cost[edge]=c,nex[edge]=head[u],head[u]=edge++;    ver[edge]=u,flow[edge]=0,cost[edge]=-c,nex[edge]=head[v],head[v]=edge++;}bool spfa()/**spfa 求最短路,并用 p 记录最短路上的边*/{    int i,u,v,l,r=0,tmp;    for(i=0; i<node; ++i)dis[i]=oo;    dis[q[r++]=src]=0;    p[src]=p[dest]=-1;    for(l=0; l!=r; (++l>=mn)?l=0:l)        for(i=head[u=q[l]],vis[u]=0; i>=0; i=nex[i])            if(flow[i]&&dis[v=ver[i]]>(tmp=dis[u]+cost[i]))            {                dis[v]=tmp;                p[v]=i^1;                if(vis[v]) continue;                vis[q[r++]=v]=1;                if(r>=mn)r=0;            }    return p[dest]>-1;}int SpfaFlow()/**源点到汇点的一条最短路即可行流,不断的找这样的可行流*/{    int i,ret=0,delta;    while(spfa())    {        for(i=p[dest],delta=oo; i>=0; i=p[ver[i]])            if(flow[i^1]<delta)delta=flow[i^1];        for(i=p[dest]; i>=0; i=p[ver[i]])            flow[i]+=delta,flow[i^1]-=delta;        ret+=delta*dis[dest];    }    return ret;}int a[110];int main(){    int n;    while(~scanf("%d",&n))    {        int avr=0;        for(int i=1; i<=n; i++)            scanf("%d",&a[i]),avr+=a[i];        avr/=n;        prepare(n+2,0,n+1);        for(int i=1; i<=n; i++)        {            a[i]-=avr;            if(a[i]>0)                addedge(0,i,a[i],0);            else if(a[i]<0)                addedge(i,n+1,-a[i],0);            if(i==1)            {                addedge(1,n,oo,1);                addedge(n,1,oo,1);            }            else            {                addedge(i-1,i,oo,1);                addedge(i,i-1,oo,1);            }        }        printf("%d\n",SpfaFlow());    }    return 0;}

方法二:

将每个点要分得的平均值avr算出来,则从源点到每个点连一条流量限制为a[i]费用为0的边,从每个点到汇点连一条流量限制为avr费用为0的边,然后相邻两个点相互连接一条流量为正无穷(oo),费用为1的边,表示调整一次的费用为1。建图的意义是从源点到汇点,让每一个点把流量从最初的a[i]流到最终的avr使得最终流量相等,而且每一次调整的费用都是1,使得最后得到的费用答案等于调整次数。

#include <iostream>#include <stdio.h>#include <stdlib.h>#include<string.h>#include<algorithm>#include<math.h>#include<queue>#include<stack>using namespace std;typedef long long ll;const   int oo=1e9;const   int mm=11111111;const   int mn=888888;int node,src,dest,edge;int ver[mm],flow[mm],cost[mm],nex[mm];int head[mn],dis[mn],p[mn],q[mn],vis[mn];/**这些变量基本与最大流相同,增加了cost 表示边的费用,p记录可行流上节点对应的反向边*/void prepare(int _node,int _src,int _dest){    node=_node,src=_src,dest=_dest;    for(int i=0; i<node; i++)head[i]=-1,vis[i]=0;    edge=0;}void addedge(int u,int v,int f,int c)///起点,终点,流量,费用{    ver[edge]=v,flow[edge]=f,cost[edge]=c,nex[edge]=head[u],head[u]=edge++;    ver[edge]=u,flow[edge]=0,cost[edge]=-c,nex[edge]=head[v],head[v]=edge++;}bool spfa()/**spfa 求最短路,并用 p 记录最短路上的边*/{    int i,u,v,l,r=0,tmp;    for(i=0; i<node; ++i)dis[i]=oo;    dis[q[r++]=src]=0;    p[src]=p[dest]=-1;    for(l=0; l!=r; (++l>=mn)?l=0:l)        for(i=head[u=q[l]],vis[u]=0; i>=0; i=nex[i])            if(flow[i]&&dis[v=ver[i]]>(tmp=dis[u]+cost[i]))            {                dis[v]=tmp;                p[v]=i^1;                if(vis[v]) continue;                vis[q[r++]=v]=1;                if(r>=mn)r=0;            }    return p[dest]>-1;}int SpfaFlow()/**源点到汇点的一条最短路即可行流,不断的找这样的可行流*/{    int i,ret=0,delta;    while(spfa())    {        for(i=p[dest],delta=oo; i>=0; i=p[ver[i]])            if(flow[i^1]<delta)delta=flow[i^1];        for(i=p[dest]; i>=0; i=p[ver[i]])            flow[i]+=delta,flow[i^1]-=delta;        ret+=delta*dis[dest];    }    return ret;}int a[110];int main(){    int n;    while(~scanf("%d",&n))    {        int avr=0;        for(int i=1; i<=n; i++)            scanf("%d",&a[i]),avr+=a[i];        avr/=n;        prepare(n+2,0,n+1);        for(int i=1; i<=n; i++)        {            addedge(0,i,a[i],0);            addedge(i,n+1,avr,0);            if(i==1)            {                addedge(1,n,oo,1);                addedge(n,1,oo,1);            }            else            {                addedge(i-1,i,oo,1);                addedge(i,i-1,oo,1);            }        }        printf("%d\n",SpfaFlow());    }    return 0;}

0 0
原创粉丝点击