K Subsequence

题目描述

Master QWsin is dating with Sindar. And now they are in a restaurant, the restaurant has n dishes in order. For each dish, it has a delicious value ai. However, they can only order k times. QWsin and Sindar have a special ordering method, because they believe that by this way they can get maximum happiness value.

Specifically, for each order, they will choose a subsequence of dishes and in this subsequence, when i<j, the subsequence must satisfies ai≤aj. After a round, they will get the sum of the subsequence and they can't choose these dishes again. 

Now, master QWsin wants to know the maximum happiness value they can get but he thinks it's too easy, so he gives the problem to you. Can you answer his question?

 

输入

There are multiple test cases. The first line of the input contains an integer T, indicating the number of test cases. For each test case:

First line contains two positive integers n and k which are separated by spaces.

Second line contains n positive integer a1,a2,a3...an represent the delicious value of each dish.


1≤T≤5

1≤n≤2000

1≤k≤10

1≤ai≤1e5

 

输出

Only an integer represent the maximum happiness value they can get.

 

样例输入

复制样例数据

1

9 2
5 3 2 1 4 2 1 4 6

样例输出

22

 

1.对于每个点i,将它拆成两个点ia和ib,在这两个点之间建立一条容量为1,费用为-a[i],表示这个点只能选一次,选了之后可以得到a[i](因为求最大值,所以费用为负)

2.建立超级源点和汇点,从源点向ia建立一条容量为1,费用为0的边,再从ib向汇点建立一条容量为1,费用为0的边

3.对于i点和j点,如果有i<j且a[i]<=a[j],就从ib向ja建立一条容量为1,费用为0的边
--------------------- 
https://blog.csdn.net/qq_41279172/article/details/97889588

dijkstra最小费用最大流

https://blog.csdn.net/u014800748/article/details/44059993

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn = 5005;

struct node{
    int t,cap,cost,rev;
    node(int to=0,int c=0,int ct=0,int r=0):t(to),cap(c),cost(ct),rev(r){}
};
vector<node> vec[maxn];
int dis[maxn];
int prevv[maxn],preve[maxn];
int head[maxn];

int min_cost_flow(int v,int s,int t,int f)
{
    int ans=0;
    memset(head,0,sizeof(head));
    while(f>0){
        priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
        fill(dis,dis+v,INF);
        dis[s]=0;
        q.push(make_pair(0,s));
        while(q.size()){
            pair<int,int> tmp=q.top();
            q.pop();
            int v=tmp.second;
            if(dis[v]<tmp.first){
                continue;
            }
            int siz=vec[v].size();
            for(int i=0;i<siz;i++){
                node tt=vec[v][i];
                if(tt.cap>0&&dis[tt.t]>dis[v]+tt.cost+head[v]-head[tt.t]){
                    dis[tt.t]=dis[v]+tt.cost+head[v]-head[tt.t];
                    preve[tt.t]=i;
                    prevv[tt.t]=v;
                    q.push(make_pair(dis[tt.t],tt.t));
                }
            }
        }

        if(dis[t]==INF){
            return -1;
        }
        for(int i=0;i<v;++i){
            head[i]+=dis[i];
        }

        int d=f;
        for(int i=t;i!=s;i=prevv[i]){
            d=min(d,vec[prevv[i]][preve[i]].cap);
        }
        ans+=d*head[t];

        f-=d;
        for(int i=t;i!=s;i=prevv[i]){
            node &es=vec[prevv[i]][preve[i]];
            es.cap -=d;
            vec[i][es.rev].cap +=d;
        }
    }
    return ans;
}
void addedge(int s1,int t1,int cap,int cost){
    vec[s1].push_back(node(t1,cap,cost,vec[t1].size()));
    vec[t1].push_back(node(s1,0,-cost,vec[s1].size()-1));
}
int a[maxn];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,k;
        scanf("%d %d",&n,&k);
        for(int i=1;i<=2*n+5;i++){
            vec[i].clear();
        }
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        int sp=0,tp=2*n+1;
        for(int i=1;i<=n;i++){
            addedge(2*i-1,2*i,1,-a[i]);
            addedge(sp,2*i-1,1,0);
            addedge(2*i,tp,1,0);
            for(int j=i+1;j<=n;++j){
                if(a[j]>=a[i]){
                    addedge(2*i,2*j-1,1,0);
                }
            }
        }
        printf("%d\n",-min_cost_flow(2*n+2,sp,tp,k));
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值