hdu 6611 ( K Subsequence ) (费用流模板)

#include 
using namespace std;
typedef int lint;
typedef long long LL;
const lint maxn = 5011;
const lint maxm = 4e7;
struct EDGE {
    int from, to, next, cost, cap;  //  如果需要修改 cost为LL
};
namespace MFMC {
    const static int maxn = 5011;
    const static int maxm = 4e7;
    EDGE edge[maxm];
    int tot, he[maxn], n;
    void Init(int _n) {
        tot = 0;
        n = _n + 1;
        memset(he, -1, n * sizeof(int));
    }
    void AddDi(int u, int v, int cap,int cost) {   //  如果需要修改 cost为LL
        edge[tot] = EDGE{u, v, he[u], cost, cap};
        he[u] = tot++;
    }
    void AddDi_mfmc(int u, int v, int cap,int cost) {  //  如果需要修改 cost为LL
        AddDi(u, v,  cap,cost);
        AddDi(v, u,  0,-cost);
    }
//O(VE)
//record_e[i]是fa[i]->i的边的编号
    template
    void spfa(int s, DT dist[], int rec[]) {
        queue q;
        static bool inq[maxn];

        memset(dist, 0x3f, n * sizeof(DT));
        memset(inq, 0, n * sizeof(bool));
        memset(rec, -1, n * sizeof(int));
        q.push(s);
        dist[s] = 0;
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            inq[u] = 0;
            for (int e = he[u]; ~e; e = edge[e].next) {
                if (0 == edge[e].cap)
                    continue;
                int v = edge[e].to;
                if (dist[v] > dist[u] + edge[e].cost) {
                    dist[v] = dist[u] + edge[e].cost;
                    rec[v] = e;
                    if (!inq[v]) {
                        q.push(v);
                        inq[v] = 1;
                    }
                }
            }
        }
    }

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

        memset(dist, 0x3f, n * sizeof(DT));
        memset(rec, -1, n * 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 (int e = he[s]; ~e; e = edge[e].next) {
                if (0 == edge[e].cap) continue;
                int v = edge[e].to;
                if (dist[v] > c + edge[e].cost) {
                    dist[v] = c + edge[e].cost;
                    rec[v] = e;
                    q.push(make_pair(-dist[v], v));
                }
            }
        }
    }

//Need dijkstra_GRAPH_EDGES_PQ
//O(FE log(E)),F is the maximum flow

    template
    void mfmc(int s, int t, FT &maxflow, CT &mincost) {
        CT inf;
        memset(&inf, 0x3f, sizeof(CT));
        static CT dist[maxn];
        static int rec_e[maxn];
        maxflow = mincost = 0;
        CT realdist = 0;    //real distance from s to t

        bool first = true;
        while (1) {
            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::max();
            for (int e = rec_e[t]; ~e; e = rec_e[edge[e].from])
                minF = min(minF, (FT) edge[e].cap);
            maxflow += minF;
            realdist += dist[t];
            mincost += minF * realdist;
            for (int e = rec_e[t]; ~e; e = rec_e[edge[e].from]) {
                edge[e].cap -= minF;
                edge[e ^ 1].cap += minF;
            }
            for (int e = 0; e < tot; ++e) {
                EDGE &ed = edge[e];
                ed.cost += dist[ed.from] - dist[ed.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_mfmc( s,S,k,0);
        for(int i=1;i<=n;i++) {
            scanf("%d",&a[i]);
            MFMC::AddDi_mfmc( S,2*i,1,0);
            MFMC::AddDi_mfmc( 2*i+1,t,1,0);
            MFMC::AddDi_mfmc( 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_mfmc(2 * i + 1, 2 * j, 1,0);
                    if(first){
                        MFMC::AddDi_mfmc( 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;
}

 

你可能感兴趣的:(费用流)