网络流二十四题之十九 —— 负载平衡问题

来源:互联网 发布:视频制作特效软件 编辑:程序博客网 时间:2024/06/08 06:35

负载平衡问题


Description

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


Input

文件的第 1 行中有 1 个正整数 nn<=100),表示有 n 个仓库。
2 行中有 n 个正整数,表示 n 个仓库的库存量。


Output

程序运行结束时,将计算出的最少搬运量输出。


Sample Input

5
17 9 14 16 4


Sample Output

11


Solution

模拟题…………
不知道为什么放在网络流二十四题中。


Code

[cpp]
  1. #include <iostream>  
  2. #include <cstdio>  
  3.   
  4. #define Min(x,y) ((x)<(y)?(x):(y))  
  5. #define abs(x) ((x)>0?(x):(-(x)))  
  6.   
  7. using namespace std;  
  8.   
  9. int n,sum,ans=0x3f3f3f3f;  
  10. int s[1100][1100];  
  11.   
  12. int main(){  
  13.       
  14.     freopen(”move.in”,“r”,stdin);  
  15.     freopen(”move.out”,“w”,stdout);  
  16.       
  17.     scanf(”%d”,&n);  
  18.     for(int i=1;i<=n;i++){  
  19.         scanf(”%d”,&s[1][i]);  
  20.         sum+=s[1][i];  
  21.     }  
  22.     sum/=n;  
  23.     for(int i=1;i<=n;i++)  
  24.         s[1][i]-=sum,s[1][i+n]=s[1][i];  
  25.     for(int i=2;i<=n;i++)  
  26.         for(int j=1;j<=2*n;j++)s[i][j]=s[1][j];  
  27.     for(int i=1;i<=n;i++){  
  28.         int ans0=0;  
  29.         for(int j=i;j<i+n-1;j++){  
  30.             ans0+=abs(s[i][j]);  
  31.             s[i][j+1]+=s[i][j];  
  32.         }  
  33.         ans=Min(ans,ans0);  
  34.     }  
  35.     printf(”%d\n”,ans);  
  36.     return 0;  
  37. }  
1 0
原创粉丝点击