求助,过了#1#2

P1064 [NOIP2006 提高组] 金明的预算方案

Lily_White @ 2020-09-22 19:03:41

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
struct Item {
  int w, v;
  Item(int w = 0, int v = 0) : w(w), v(v) {}
};
const int N = 302010;
int n, m;
vector<Item> a, in;
vector<int> son[N];
int dp[N];
bool dependent[N];
namespace debug {
void print() {}
}  // namespace debug
int main() {
  cin >> n >> m;               // NOTE: n is NOT the number of items!
  in.push_back(Item(-1, -1));  // placeholder
  for (int i = 1; i <= m; i++) {
    int v, p, q;
    cin >> v >> p >> q;
    in.push_back(Item(v, v * p));
    if (q) {
      son[q].push_back(i);
      dependent[i] = true;
    }
  }
  a.push_back(Item(-1, -1));  // placeholder
  for (int i = 1; i <= m; i++) {
    if (son[i].empty()) {
      if (not dependent[i]) a.push_back(in[i]);
    } else if (son[i].size() == 1) {
      int j = son[i][0];
      a.push_back(in[i]);
      a.push_back(Item(in[i].w + in[j].w, in[i].v + in[j].v));
    } else if (son[i].size() == 2) {
      int j = son[i][0], k = son[i][1];
      a.push_back(in[i]);  
      a.push_back(Item(in[i].w + in[j].w,
                       in[i].v + in[j].v));  
      a.push_back(Item(in[i].w + in[k].w,
                       in[i].v + in[k].v)); 
      a.push_back(
          Item(in[i].w + in[j].w + in[k].w, in[i].v + in[j].v + in[k].v));
    }
  }
  debug::print();
  m = a.size();
  for (int i = 1; i <= m; i++)
    for (int j = n; j >= a[i].w; j--)
      dp[j] = max(dp[j], dp[j - a[i].w] + a[i].v);
  cout << dp[n] << endl;
  return 0;
}

by Megatherium @ 2020-10-28 21:17:03

1000 3

200 5 0

800 1 1

800 2 1


|