1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include<cstring> #include<cstdio> #include<algorithm> #include<iostream> #include<unordered_map> #include<queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 100;
int n, c,ans; bool st[N]; bool used[N]; double w[N], v[N]; double a[N], b[N];
struct node { double w, v; int idx; }z[N];
void dfs(int u, int ww, int vv) { if (ww + w[u] <= ans) return;
if (u > n) { int t = ans; ans = max(ans, ww); if(ans!=t)memcpy(used, st, n); return; }
if (vv + b[u] <= c) { st[z[u].idx] = true; dfs(u + 1, ww + a[u], vv + b[u]); }
st[z[u].idx] = false; dfs(u + 1, ww, vv);
}
bool cmp(node a, node b) { return a.w/a.v > b.w/a.v; }
int main() { cin >> n >> c;
for (int i = 0; i < n; i++) { cin >> z[i].w; z[i].idx = i; }
for (int i = 0; i < n; i++) cin >> z[i].v; sort(z, z + n, cmp);
for (int i = 0; i < n; i++) { w[i] = a[i] = z[i].w; v[i] = b[i] = z[i].v; }
for (int i = n - 2; i >= 0; i--) { w[i] += w[i + 1]; v[i] += v[i + 1]; }
cout << "Optimal value is" << endl;
dfs(0,0,0);
cout << ans << endl;
for (int i = 0; i < n; i++) cout << used[i] << " ";
return 0; }
|