• luogu P4016 负载平衡问题


    本题有数学贪心解法和费用流解法

    数学解法就看看luogu题解吧,窝太菜了

    费用流就找建图法,依旧是设超级源点和汇点,初始数据就源点s向该点连点,流量就是初始量,代价为0,然后每个仓库向相邻的点连边,容量无限大,代价为1,再每个仓库向汇点连点,容量就是sum/n,代价为0,直接跑最大流最小费就行了,这样能保证源点出的汇点全进

    typedef long long LL;
    typedef pair<LL, LL> PLL;
    typedef pair<int, int> PINT;
    
    const int maxm = 2000+5;
    const int INF = 0x3f3f3f3f;
    
    struct edge{
        int u, v, cap, flow, cost, nex;
    } edges[maxm];
    
    int head[maxm], cur[maxm], cnt, fa[maxm], d[maxm], n;
    bool inq[maxm];
    
    void init() {
        memset(head, -1, sizeof(head));
    }
    
    void addedge(int u, int v, int cap, int cost) {
        edges[cnt] = edge{u, v, cap, 0, cost, head[u]};
        head[u] = cnt++;
    }
    
    bool spfa(int s, int t, int &flow, LL &cost) {
        for(int i = 0; i <= n+1; ++i) d[i] = INF; //init()
        memset(inq, false, sizeof(inq));
        d[s] = 0, inq[s] = true;
        fa[s] = -1, cur[s] = INF;
        queue<int> q;
        q.push(s);
        while(!q.empty()) {
            int u = q.front();
            q.pop();
            inq[u] = false;
            for(int i = head[u]; i != -1; i = edges[i].nex) {
                edge& now = edges[i];
                int v = now.v;
                if(now.cap > now.flow && d[v] > d[u] + now.cost) {
                    d[v] = d[u] + now.cost;
                    fa[v] = i;
                    cur[v] = min(cur[u], now.cap - now.flow);
                    if(!inq[v]) {q.push(v); inq[v] = true;}
                }
            }
        }
        if(d[t] == INF) return false;
        flow += cur[t];
        cost += 1LL*d[t]*cur[t];
        for(int u = t; u != s; u = edges[fa[u]].u) {
            edges[fa[u]].flow += cur[t];
            edges[fa[u]^1].flow -= cur[t];
        }
        return true;
    }
    
    int MincostMaxflow(int s, int t, LL &cost) {
        cost = 0;
        int flow = 0;
        while(spfa(s, t, flow, cost));
        return flow;
    }
    
    void run_case() {
        init();
        int sum = 0, x, l, r;
        cin >> n;
        int s = 0, t = n+1;
        for(int i = 1; i <= n; ++i) {
            cin >> x;
            sum += x;
            l = i-1, r = i+1;
            if(i == 1) l = n;
            else if(i == n) r = 1;
            addedge(s, i, x, 0), addedge(i, s, 0, 0);
            addedge(i, l, INF, 1), addedge(l, i, 0, -1);
            addedge(i, r, INF, 1), addedge(r, i, 0, -1);
        }
        sum /= n;
        for(int i = 1; i <= n; ++i)
            addedge(i, t, sum, 0), addedge(t, i, 0, 0);
        LL cost = 0;
        MincostMaxflow(s, t, cost);
        cout << cost;
    }
    
    int main() {
        ios::sync_with_stdio(false), cin.tie(0);
        run_case();
        //cout.flush();
        return 0;
    }
    View Code
  • 相关阅读:
    mysql 和navicat for mysql 安装教程
    Linux 下配置php开发环境
    RecyclerView的使用(四)
    RecyclerView的使用(三)
    RecyclerView的使用(二)
    RecyclerView的使用(一)
    适配器(一)
    ListView简介
    Android ListView 详解
    android SwipeRefreshLayout google官方下拉刷新控件
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12271050.html
Copyright © 2020-2023  润新知