zoukankan      html  css  js  c++  java
  • 数字梯形问题

    费用流,第一个拆点,其它两个改一下INF就好

    # include <bits/stdc++.h>
    # define RG register
    # define IL inline
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(1010), __(1e6 + 10), INF(2e9);
    
    IL ll Read(){
        char c = '%'; ll x = 0, z = 1;
        for(; c > '9' || c < '0'; c = getchar()) if(c == '-') z = -1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0';
        return x * z;
    }
    
    int m, n, num, a[50][50], id[50][50];
    int cnt, fst[_], w[__], to[__], nxt[__], dis[_], vis[_], S, T, cost[__], pe[_], pv[_], max_flow, max_cost;
    queue <int> Q;
    
    IL void Add(RG int u, RG int v, RG int f, RG int co){
        cost[cnt] = co; w[cnt] = f; to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++;
        cost[cnt] = -co; w[cnt] = 0; to[cnt] = u; nxt[cnt] = fst[v]; fst[v] = cnt++;
    }
    
    IL bool Bfs(){
        Q.push(S); Fill(dis, 127); dis[S] = 0; vis[S] = 1;
        while(!Q.empty()){
            RG int u = Q.front(); Q.pop();
            for(RG int e = fst[u]; e != -1; e = nxt[e]){
                if(!w[e] || dis[to[e]] <= dis[u] + cost[e]) continue;
                dis[to[e]] = dis[u] + cost[e];
                pe[to[e]] = e; pv[to[e]] = u;
                if(!vis[to[e]]) vis[to[e]] = 1, Q.push(to[e]);
            }
            vis[u] = 0;
        }
        if(dis[T] >= dis[T + 1]) return 0;
        RG int ret = INF;
        for(RG int u = T; u != S; u = pv[u]) ret = min(ret, w[pe[u]]);
        for(RG int u = T; u != S; u = pv[u]) w[pe[u]] -= ret, w[pe[u] ^ 1] += ret;
        max_cost -= ret * dis[T]; max_flow += ret;
        return 1;
    }
    
    IL void Calc(){  for(max_cost = 0; Bfs(); ); printf("%d
    ", max_cost);  }
    
    IL void Work1(){
        Fill(fst, -1); cnt = 0; T = num + num + 1;
        for(RG int i = 1; i <= n; ++i)
            for(RG int j = 1; j < m + i; ++j)
                Add(id[i][j], id[i][j] + num, 1, -a[i][j]);
        for(RG int i = 1; i < n; ++i)
            for(RG int j = 1; j < m + i; ++j)
                Add(id[i][j] + num, id[i + 1][j], 1, 0), Add(id[i][j] + num, id[i + 1][j + 1], 1, 0);
        for(RG int i = 1; i <= m; ++i) Add(S, id[1][i], 1, 0);
        for(RG int i = 1; i < n + m; ++i) Add(id[n][i] + num, T, 1, 0);
        Calc();
    }
    
    IL void Work2(){
        Fill(fst, -1); cnt = 0; T = num + num + 1;
        for(RG int i = 1; i <= n; ++i)
            for(RG int j = 1; j < m + i; ++j)
                Add(id[i][j], id[i][j] + num, INF, -a[i][j]);
        for(RG int i = 1; i < n; ++i)
            for(RG int j = 1; j < m + i; ++j)
                Add(id[i][j] + num, id[i + 1][j], 1, 0), Add(id[i][j] + num, id[i + 1][j + 1], 1, 0);
        for(RG int i = 1; i <= m; ++i) Add(S, id[1][i], 1, 0);
        for(RG int i = 1; i < n + m; ++i) Add(id[n][i] + num, T, INF, 0);
        Calc();
    }
    
    IL void Work3(){
        Fill(fst, -1); cnt = 0; T = num + num + 1;
        for(RG int i = 1; i <= n; ++i)
            for(RG int j = 1; j < m + i; ++j)
                Add(id[i][j], id[i][j] + num, INF, -a[i][j]);
        for(RG int i = 1; i < n; ++i)
            for(RG int j = 1; j < m + i; ++j)
                Add(id[i][j] + num, id[i + 1][j], INF, 0), Add(id[i][j] + num, id[i + 1][j + 1], INF, 0);
        for(RG int i = 1; i <= m; ++i) Add(S, id[1][i], 1, 0);
        for(RG int i = 1; i < n + m; ++i) Add(id[n][i] + num, T, INF, 0);
        Calc();
    }
    
    int main(RG int argc, RG char *argv[]){
        m = Read(); n = Read();
        for(RG int i = 1; i <= n; ++i)
            for(RG int j = 1; j < m + i; ++j)
                a[i][j] = Read(), id[i][j] = ++num;
        Work1(); Work2(); Work3();
        return 0;
    }
    
  • 相关阅读:
    c/c++基础 输入函数/流
    数据库的码/键
    sql plus笔记
    cmd delete oracle related
    CAN总线
    SAR ADC : 逐次逼近寄存器型(SAR)模数转换器(ADC)
    Cortex-M3寄存器等基础知识
    Cortex-M3知识点
    ARM指令和Thumb指令区别
    8051、ARM、AVR
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8206320.html
Copyright © 2011-2022 走看看