zoukankan      html  css  js  c++  java
  • 牛客网暑期ACM多校训练营(第三场)2018 A,PACM Team ( 01背包裸题 )

    题目描述
    Eddy was a contestant participating in ACM ICPC contests. ACM is short for Algorithm, Coding, Math. Since in the ACM contest, the most important knowledge is about algorithm, followed by coding(implementation ability), then math. However, in the ACM ICPC World Finals 2018, Eddy failed to solve a physics equation, which pushed him away from a potential medal.

    Since then on, Eddy found that physics is actually the most important thing in the contest. Thus, he wants to form a team to guide the following contestants to conquer the PACM contests(PACM is short for Physics, Algorithm, Coding, Math).

    There are N candidate groups each composed of pi physics experts, ai algorithm experts, ci coding experts, mi math experts. For each group, Eddy can either invite all of them or none of them. If i-th team is invited, they will bring gi knowledge points which is calculated by Eddy’s magic formula. Eddy believes that the higher the total knowledge points is, the better a team could place in a contest. But, Eddy doesn’t want too many experts in the same area in the invited groups. Thus, the number of invited physics experts should not exceed P, and A for algorithm experts, C for coding experts, M for math experts.

    Eddy is still busy in studying Physics. You come to help him to figure out which groups should be invited such that they doesn’t exceed the constraint and will bring the most knowledge points in total.
    输入描述:
    The first line contains a positive integer N indicating the number of candidate groups.
    Each of following N lines contains five space-separated integer pi, ai, ci, mi, gi indicating that i-th team consists of pi physics experts, ai algorithm experts, ci coding experts, mi math experts, and will bring gi knowledge points.
    The last line contains four space-separated integer P, A, C, M indicating the maximum possible number of physics experts, algorithm experts, coding experts, and math experts, respectively.

    1 ≤ N ≤ 36
    0 ≤ pi,ai,ci,mi,gi ≤ 36
    0 ≤ P, A, C, M ≤ 36
    输出描述:
    The first line should contain a non-negative integer K indicating the number of invited groups.
    The second line should contain K space-separated integer indicating the index of invited groups(groups are indexed from 0).

    You can output index in any order as long as each index appears at most once. If there are multiple way to reach the most total knowledge points, you can output any one of them. If none of the groups will be invited, you could either output one line or output a blank line in the second line.
    示例1
    输入
    2
    1 0 2 1 10
    1 0 2 1 21
    1 0 2 1
    输出
    1
    1
    示例2
    输入
    1
    2 1 1 0 31
    1 0 2 1
    输出
    0
    题意:
    相当于有4个权值的背包分别为 p[ i ] , a[ i ] , c[ i ] , m[ i ] .
    思路:
    开一个4维的背包,但是这个题还要求路径回溯 ( 不会点 )

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int maxn = 38;
    
    int dp[maxn][maxn][maxn][maxn];
    bool vis[maxn][maxn][maxn][maxn][maxn];
    int p[maxn],a[maxn],c[maxn],m[maxn],g[maxn];
    int P,A,C,M;
    
    int main(){
        memset(vis,false,sizeof(vis)); memset(dp,0,sizeof(dp));
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        int t; cin>>t;
        for (int i = 1;i<=t;i++) {
            cin>>p[i]>>a[i]>>c[i]>>m[i]>>g[i];
        }
        cin>>P>>A>>C>>M;
        for (int i = 1;i<=t;i++) {
            for ( int j = P;j>=p[i];j--) {
                for (int k = A;k>=a[i];k--) {
                    for (int l = C;l>=c[i];l--) {
                        for (int q = M;q >= m[i];q--) {
                            if ( dp[j-p[i]][k-a[i]][l-c[i]][q-m[i]]+g[i]>dp[j][k][l][q] ) {
                                vis[i][j][k][l][q] = true;
                                dp[j][k][l][q] = dp[j-p[i]][k-a[i]][l-c[i]][q-m[i]]+g[i];
                            }
                        }
                    }
                }
            }
        }
        int ans[maxn] ; ans[0] = 0;//路径回溯,值得细看
        for (int i = t;i>=1;i--) {
            if(vis[i][P][A][C][M]){
                ans[++ans[0]] = i;
                P -= p[i], A -= a[i], C -= c[i], M -= m[i];
            //  if(!P & !A & !C & !M) break;
            }
        }
        if ( !ans[0] ){
            printf("0
    ");
        } else {
            printf("%d
    ",ans[0]);
            for (int i = 1;i<=ans[0];i++) {
                printf("%d ",ans[i]-1);
            }
        }
        return 0;
    }
  • 相关阅读:
    CodeForces 1105D-暴力BFS
    POJ 1258 Agri-Net (最小生成树)
    POJ 1251 Jungle Roads (最小生成树)
    hiho 1097 最小生成树一·Prim算法 (最小生成树)
    Codeforces 544E K Balanced Teams (DP)
    Codeforces 408D Long Path (DP)
    PTA L1题目合集(更新至2019.3)
    HDOJ 3308 LCIS (线段树)
    HDOJ 1698 Just a Hook (线段树)
    HDOJ 4267 A Simple Problem with Integers (线段树)
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745977.html
Copyright © 2011-2022 走看看