LUOGU 4016 load balancing network flow 24 questions

Copyright: https: //blog.csdn.net/huashuimu2003 https://blog.csdn.net/huashuimu2003/article/details/91354724

title

LUOGU 4016
Title Description

Company G of n transport lines arranged along the annular rail warehouse, an unequal number of goods stored in each warehouse. How to carry a minimum amount of inventory can make the same amount of n warehouse. When handling cargo, handling only between adjacent warehouse.

Input and output format
input format:

The first line in the file has a positive integer n, n expressed warehouses.
Line 2 has a positive integer n, n represents the inventory of warehouses.

Output formats:

Conveying the least amount of output.

Input Output Sample
Input Sample # 1:

5
17 9 14 16 4

Output Sample # 1:

11

Explanation

1 n 100 1 \ leq n \ leq 100

analysis

  • 1. calculate their average, with each number obtained by subtracting the average number of new value, this value is a negative number means that if it sounded from somewhere else to, if it shows positive can send out a little.

  • 2. We have established s Source s and t Meeting Point t .

    • If the right value is positive, that is greater than the average store, we went from s Source s toward one side it is connected, the capacity of Stored value - average , the price for the 0 0 , meaning that it can from s Source s free to get more out of stored value - the average flow rate, is equivalent to the storage itself has value - the average flow of the above do not say, build a source point, just for this feature.

    • If the weight value is negative, i.e., smaller than the average value is stored, from which we have to t Meeting Point t even an edge capacity Average - the stored value , the cost of 0 0 , meaning that it must be coming from the other node traffic and import itself, meaning an analogy with the above mentioned.

  • 3. For each node can pass each other, that left neighbors and the right neighbor, even to them respectively side, he said his stream can flow past.

Then big brother said it was to build a single-node map? ? This represents konjac do not know what a single-node, two-node, want to know, then, reference tonight beautiful moonlight .

code

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10,maxm=210,inf=0x3f3f3f3f;

char buf[1<<15],*fs,*ft;
inline char getc() { return (ft==fs&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),ft==fs))?0:*fs++; }
template<typename T>inline void read(T &x)
{
    x=0;
    T f=1, ch=getchar();
    while (!isdigit(ch) && ch^'-') ch=getchar();
    if (ch=='-') f=-1, ch=getchar();
    while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
    x*=f;
}

template<typename T>inline void write(T x)
{
    if (!x) { putchar('0'); return ; }
    if (x<0) putchar('-'), x=-x;
    T num=0, ch[20];
    while (x) ch[++num]=x%10+48, x/=10;
    while (num) putchar(ch[num--]);
}

int ver[maxn<<1],edge[maxn<<1],Next[maxn<<1],cost[maxn<<1],head[maxn],len=1;
inline void add(int x,int y,int z,int c)
{
    ver[++len]=y,edge[len]=z,cost[len]=c,Next[len]=head[x],head[x]=len;
    ver[++len]=x,edge[len]=0,cost[len]=-c,Next[len]=head[y],head[y]=len;
}

int s,t;
int dist[maxn],incf[maxn],pre[maxn];
bool vis[maxn];
inline bool spfa()
{
    memset(dist,0x3f,sizeof(dist));
    memset(vis,0,sizeof(vis));
    queue<int>q;q.push(s);
    dist[s]=0,vis[s]=1,incf[s]=1<<30;
    while (!q.empty())
    {
        int x=q.front();
        q.pop();
        vis[x]=0;
        for (int i=head[x]; i; i=Next[i])
        {
            if (!edge[i]) continue;
            int y=ver[i];
            if (dist[y]>dist[x]+cost[i])
            {
                dist[y]=dist[x]+cost[i];
                incf[y]=min(incf[x],edge[i]);
                pre[y]=i;
                if (!vis[y]) q.push(y),vis[y]=1;
            }
        }
    }
    if (dist[t]==inf) return false;
    else return true;
}

long long maxflow,ans;
inline void update()
{
    int x=t;
    while (x!=s)
    {
        int i=pre[x];
        edge[i]-=incf[t];
        edge[i^1]+=incf[t];
        x=ver[i^1];
    }
    maxflow+=incf[t];
    ans+=dist[t]*incf[t];
}

int main()
{
	int n,sum=0,a[maxm];read(n);
	for (int i=1; i<=n; ++i) read(a[i]),sum+=a[i];
	sum/=n;
	s=0,t=n<<1|1;
	for (int i=1; i<=n; ++i) a[i]-=sum;
	for (int i=1; i<=n; ++i)
	{
		if (a[i]>0) add(s,i,a[i],0);
		else if (a[i]<0) add(i,t,-a[i],0);
	}
	for (int i=1; i<=n; ++i)
	{
		if (i!=1) add(i,i-1,inf,1);
		if (i!=n) add(i,i+1,inf,1);
	}
	add(1,n,inf,1);add(n,1,inf,1);
	while (spfa()) update();
	write(ans),puts("");
	return 0;
}

Guess you like

Origin blog.csdn.net/huashuimu2003/article/details/91354724