hdu 6611 (vector 版本费用流模板 )

#include <bits/stdc++.h>
using namespace std;
typedef int lint;
typedef long long LL;
const lint maxn = 5011;
const lint maxm = 4e7;

namespace MFMC {
    struct EDGE {
        int from, to, inv;
        int cap, cost;
    };
    vector<vector<EDGE> > es;
    void Init(int n) {
        for (auto& i : es)
            i.resize(0);
        es.resize(n+1);
    }
    void AddDi(int u, int v, int cap, int cost) {  //  如果需要修改 cost为LL
        es[u].push_back(EDGE{u, v, (int)es[v].size(), cap, cost});
        es[v].push_back(EDGE{v, u, (int)es[u].size() - 1, 0, -cost});
    }
//O(VE)
    template<typename DT>
    void spfa(int s, DT dist[], EDGE* rec[]) {
        queue<int> q;
        static bool inq[maxn];

        memset(dist, 0x3f, es.size() * sizeof(DT));
        memset(inq, 0, es.size() * sizeof(bool));
        memset(rec, 0, es.size() * sizeof(int));
        dist[s] = 0;
        q.push(s);
        while (!q.empty()) {
            s = q.front();
            q.pop();
            inq[s] = false;
            for (auto& e : es[s]) {
                if (0 == e.cap)
                    continue;
                if (dist[e.to] > dist[s] + e.cost) {
                    dist[e.to] = dist[s] + e.cost;
                    rec[e.to] = &e;
                    if (!inq[e.to]) {
                        q.push(e.to);
                        inq[e.to] = true;
                    }
                }
            }
        }
    }

    template<typename DT>
    void dijkstra_pq(int s, DT dist[], EDGE* rec[]) {
        priority_queue<pair<DT, int> > q;//-dist, vertex

        memset(dist, 0x3f, es.size() * sizeof(DT));
        memset(rec, 0, es.size() * sizeof(int));
        dist[s] = 0;
        q.push(make_pair(0, s));
        while (!q.empty()) {
            s = q.top().second;
            DT c = -q.top().first;
            q.pop();
            if (c != dist[s]) continue;
            for (auto& e : es[s]) {
                if (0 == e.cap)
                    continue;
                if (dist[e.to] > c + e.cost) {
                    dist[e.to] = c + e.cost;
                    rec[e.to] = &e;
                    q.push(make_pair(-dist[e.to], e.to));
                }
            }
        }
    }

//Need dijkstra_GRAPH_EDGES_PQ
//O(FE log(E)),F is the maximum flow
    template<typename FT, typename CT>
    void mfmc(int s, int t, FT &maxflow, CT &mincost) {
        CT inf;
        memset(&inf, 0x3f, sizeof(CT));
        static CT dist[maxn];
        static EDGE* rec_e[maxn];
        maxflow = mincost = 0;
        CT realdist = 0;    //real distance from s to t

        bool first = true;
        while (true) {
            if (first) {
                spfa( s, dist, rec_e);
                first = false;
            } else {
                dijkstra_pq( s, dist, rec_e);
            }
            if (inf == dist[t])
                break;
            FT minF = numeric_limits<FT>::max();
            for (auto e = rec_e[t]; e; e = rec_e[e->from])
                minF = min(minF, (FT)e->cap);
            maxflow += minF;
            realdist += dist[t];
            mincost += minF * realdist;
            for (auto e = rec_e[t]; e; e = rec_e[e->from]) {
                e->cap -= minF;
                es[e->to][e->inv].cap += minF;
            }
            for (auto& i : es)
                for (auto& e : i)
                    e.cost += dist[e.from] - dist[e.to];
        }
    }
};
int a[maxn];
int main(){
    int TT,n,k;
    scanf("%d",&TT);
    while(TT--)
    {
        scanf("%d%d",&n,&k);
        lint s=0, S=1,t=2*n+2;
        MFMC::Init(t + 10);
        MFMC::AddDi( s,S,k,0);
        for(int i=1;i<=n;i++) {
            scanf("%d",&a[i]);
            MFMC::AddDi( S,2*i,1,0);
            MFMC::AddDi( 2*i+1,t,1,0);
            MFMC::AddDi( 2*i,2*i+1,1,-a[i]);
        }
        for (int i = 1; i <= n; ++i) {
            int last = 0x3f3f3f3f;
            for (int j = i + 1; j <= n; ++j) {
                bool first = true;
                if (a[j] >= a[i] && a[j] < last) {
                    MFMC::AddDi(2 * i + 1, 2 * j, 1,0);
                    if(first){
                        MFMC::AddDi( 2 * i , 2 * j,0x3f3f3f3f,0);
                        first = false;
                    }
                    last = a[j];
                }
            }
        }
        LL cost = 0;
        LL flow = 0;
        MFMC::mfmc(s, t,flow,cost );
        //g.mincost( s,t,flow,cost );
        //printf("%d\n",-ans);
        cout << -cost << endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值