LOJ6013「網絡流 24 題 - 14」負載平衡 墜小費用墜大流

大家都很強, 可與之共勉 。

題意:
  G 公司有n 個沿鐵路運輸線環形排列的倉庫,每個倉庫存儲的貨物數量不等。如何用最少搬運量可以使n 個倉庫的庫存數量相同。搬運貨物時,只能在相鄰的倉庫之間搬運。

題解:
  求出平均值,轉化爲供求問題。這道題相當於把盈餘的倉庫供給虧損的倉庫。於是盈餘的在一個集合,虧損的在一個集合。注意到這道題可以再相鄰倉庫之間連邊,也就是所有倉庫可以互達,只是單位費用不一樣,我們方便處理直接在一個集合處理這種情況。
  拆點之後源點S 連盈餘的倉庫,費用爲0 ,流量爲 ,虧損的倉庫連匯點T ,費用爲0 ,流量爲 。然後再左邊的點相鄰之間連邊費用爲1 ,流量爲+ (只是爲了轉運貨物)。左邊往右邊的點(相鄰之間)連邊費用爲1 ,流量爲+ 。(用來供給)

  然後跑最小費用,最大流。

# include <bits/stdc++.h>

template < class T >  inline bool chkmax ( T& d, const T& x )  {  return d < x ? ( d = x ), 1 : 0 ;  }
template < class T >  inline bool chkmin ( T& d, const T& x )  {  return d > x ? ( d = x ), 1 : 0 ;  }

# define oo 0x3f3f3f3f

# define N 5010
# define M 16010

class  MinCostMaxFlow  {
    private :
        struct edge  {
            int to, nxt, w, cost ;
        } g [M << 1] ;

        int S, T ;
        int head [N], dis [N], pre [N], ecnt ;

        inline bool spfa ( int S, int T )  {
            static std :: bitset < N > inq ;
            static std :: deque < int > Q ;
            inq.reset ( ) ; Q.clear ( ) ;
            memset ( pre, 0, sizeof ( int ) * ( T + 1 ) ) ;
            memset ( dis, 0x3f, sizeof ( int ) * ( T + 1 ) ) ;
            Q.push_front ( S ) ;
            inq [S] = 1 ;
            dis [S] = 0 ;
            while ( ! Q.empty ( ) )  {
                int u = Q.front ( ) ; Q.pop_front ( ) ;
                inq [u] = 0 ;
                for ( int i = head [u] ; i ; i = g [i].nxt )  {
                    int& v = g [i].to ;
                    if ( g [i].w && chkmin ( dis [v], dis [u] + g [i].cost ) )  {
                        pre [v] = i ;
                        if ( ! inq [v] )  {
                            ( Q.empty ( ) || dis [v] < dis [Q.front ( )] ) ? Q.push_front ( v ) : Q.push_back ( v ) ;
                            inq [v] = 1 ;
                        }
                    }
                }
            }
            return ( bool ) pre [T] ;
        }
    public :
        MinCostMaxFlow ( )  {  ecnt = 1 ; memset ( head, 0, sizeof head ) ;  }

        inline void add_edge ( int u, int v, int w, int cost )  {
            g [++ ecnt] = ( edge )  {  v, head [u], w, cost } ; head [u] = ecnt ;
            g [++ ecnt] = ( edge )  {  u, head [v], 0, -cost } ; head [v] = ecnt ;
        }

        std :: pair < int, int > mcmf ( int S, int T )  {
            this -> S = S, this -> T = T ;
            int flow = 0, cost = 0, x ;
            while ( spfa ( S, T ) )  {
                x = oo ;
                for ( int i = pre [T] ; i ; i = pre [g [i ^ 1].to] )  chkmin ( x, g [i].w ) ;
                for ( int i = pre [T] ; i ; i = pre [g [i ^ 1].to] )  {
                    g [i].w -= x, g [i ^ 1].w += x ;
                    cost += x * g [i].cost ;
                }

                flow += x ;
            }
            return std :: make_pair ( flow, cost ) ;
        }
} Lazer ;

int a [N] ;

# undef N
# undef M

int main ( )  {
    int n ;
    scanf ( "%d", & n ) ;
    const int S = n + n + 1, T = n + n + 2 ;
    int sum ( 0 ) ;

    for ( int i = 1 ; i <= n ; ++ i )  scanf ( "%d", a + i ) ;
    for ( int i = 1 ; i <= n ; ++ i )  sum += a [i] ;

    int ave = sum / n ;

    for ( int i = 1 ; i <= n ; ++ i )  a [i] -= ave ;

    for ( int i = 1 ; i <= n ; ++ i )  {
        if ( a [i] > 0 )  Lazer.add_edge ( S, i, a [i], 0 ) ;
        else  Lazer.add_edge ( i + n, T, - a [i], 0 ) ;
        Lazer.add_edge ( i, ( ( i + 1 ) > n ? i + 1 - n : i + 1 ) + n, oo, 1 ) ;
        Lazer.add_edge ( i, ( ( i + 1 ) > n ? i + 1 - n : i + 1 ), oo, 1 ) ;
        Lazer.add_edge ( i, ( ( i - 1 ) < 1 ? i - 1 + n : i - 1 ) + n, oo, 1 ) ;
        Lazer.add_edge ( i, ( ( i - 1 ) < 1 ? i - 1 + n : i - 1 ), oo, 1 ) ;
    }

    printf ( "%d\n", Lazer.mcmf ( S, T ).second ) ;

    return 0 ;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章