【ACM】HDU 6611 K Subsequence 2019杭电多校第三场1009 网络流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6611

K Subsequence

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2024    Accepted Submission(s): 471


 

Problem Description

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
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?

 

 

Input

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

 

 

Output

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

 

 

Sample Input

 

1 9 2 5 3 2 1 4 2 1 4 6

 

 

Sample Output

 

22

 

 

Source

2019 Multi-University Training Contest 3

 

题目大意:

有一个n个数的数组,从中拿出k个不重复的不下降子序列,求这些序列的权值最大和

思路:

这道题和网络流的最长不下降子序列问题非常类似。建图的时候可以借鉴该题的思路。

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

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

3.对于i点和j点,如果有i

跑一遍最小费用最大流即可。

注意这题不能用spfa板子,会超时,从网上找了一个dijkstra版的最小费用最大流板子,仅供参考。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
typedef long long ll;
using namespace std;


#define MV 5000 //节点最大值
#define INF 0x3f3f3f3f


struct edge
{
    int t, cap, cost, rev;
    edge(int to = 0, int c = 0, int ct = 0, int r = 0): t(to), cap(c), cost(ct), rev(r) {};
};
vector  G[MV];//图的邻接表表示   注意!如果有多个样例输入 每个样例不要忘记G[i].clear();
int dis[MV];//单源点s到其它顶点的最短距离(本次搜索的最小费用)
int prevv[MV], preve[MV];//最短路中的前驱结点 对应边
int h[MV];

//最小费用流Dijkstra算法

//Dijkstra算法求最小费用流核心代码:

//h[MAX_V]:导入势保证所有边均为非负边 O(FElogV)
typedef pairP;
int min_cost_flow(int v, int s, int t, int f) //与main函数中的变量对应一致
{

    int i, ans = 0;

    memset(h,0,sizeof h);

    while(f > 0)
    {
        //Dijkstra算法:寻找最短路 O(ElogV)
        priority_queue, greater

> que; fill(dis, dis + v, INF); dis[s] = 0; que.push(P(0, s)); while(!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if(dis[v] < p.first) continue; int size = G[v].size(); for(i = 0; i < size; ++i) { edge es = G[v][i];//**** if(es.cap > 0 && dis[es.t] > dis[v] + es.cost + h[v] - h[es.t]) { dis[es.t] = dis[v] + es.cost + h[v] - h[es.t]; prevv[es.t] = v; preve[es.t] = i; que.push(P(dis[es.t], es.t)); } } } if(dis[t] == INF) return -1; //更新势 for(i = 0; i < v; ++i) h[i] += dis[i]; int d = f; for(i = t; i != s; i = prevv[i]) d = min(d, G[prevv[i]][preve[i]].cap); ans += d * h[t]; f -= d; for(i = t; i != s; i = prevv[i]) { edge &es = G[prevv[i]][preve[i]]; es.cap -= d; G[i][es.rev].cap += d; } } return ans; } void addedge(int s1,int t1,int cap,int cost) { G[s1].push_back(edge(t1, cap, cost, G[t1].size())); G[t1].push_back(edge(s1, 0, -cost, G[s1].size() - 1)); } int a[MV]; int main() { // int n, v, s, t, f;//n 边的条数 // // v 节点的数量(包括源点和汇点) // // s source t sink // // f 需要传送的流量 //freopen("in.txt","r",stdin); int t; scanf("%d",&t); while(t--) { int n,k; scanf("%d%d",&n,&k); for(int i=1;i<=2*n+5;i++) G[i].clear(); for(int i=1;i<=n;i++) scanf("%d",&a[i]); int sp=0,tp=2*n+1; //addedge(sp,sp+1,k,0); 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; }

 

ps:放一个最长不下降子序列的代码:

#include
#include
#include
#include
using namespace std;
const int Ni = 1010;
const int MAX = 1<<26;
int a[Ni],dp[Ni];
struct Edge
{
    int u,v,c;
    int next;
} edge[20*Ni];
int n,m;
int edn;//边数
int p[Ni];//父亲
int d[Ni];
int sp,tp;//原点,汇点

void addedge(int u,int v,int c)
{
    edge[edn].u=u;
    edge[edn].v=v;
    edge[edn].c=c;
    edge[edn].next=p[u];
    p[u]=edn++;

    edge[edn].u=v;
    edge[edn].v=u;
    edge[edn].c=0;
    edge[edn].next=p[v];
    p[v]=edn++;
}
int bfs()
{
    queue  q;
    memset(d,-1,sizeof(d));
    d[sp]=0;
    q.push(sp);
    while(!q.empty())
    {
        int cur=q.front();
        q.pop();
        for(int i=p[cur]; i!=-1; i=edge[i].next)
        {
            int u=edge[i].v;
            if(d[u]==-1 && edge[i].c>0)
            {
                d[u]=d[cur]+1;
                q.push(u);
            }
        }
    }
    return d[tp] != -1;
}
int dfs(int a,int b)
{
    int r=0;
    if(a==tp)return b;
    for(int i=p[a]; i!=-1 && r0 && d[u]==d[a]+1)
        {
            int x=min(edge[i].c,b-r);
            x=dfs(u,x);
            r+=x;
            edge[i].c-=x;
            edge[i^1].c+=x;
        }
    }
    if(!r)d[a]=-2;
    return r;
}

int dinic(int sp,int tp)
{
    int total=0,t;
    while(bfs())
    {
        while(t=dfs(sp,MAX))
            total+=t;
    }
    return total;
}
int main()
{
    int len=1;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
        dp[i]=1;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j

 

你可能感兴趣的:(【ACM】HDU 6611 K Subsequence 2019杭电多校第三场1009 网络流)