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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std;
struct Charm { int weight; int desirability; }; struct Node { int index; int weight; int desirability; int evaluation;
bool operator<(const Node a)const { return evaluation < a.evaluation; } }; vector<Charm>* v = NULL;
int Down(int n, int m) { int down = 0; for (int i = n - 1; i >= 0; i--) { if (m >= (*v)[i].weight) { m -= (*v)[i].weight; down += (*v)[i].desirability; } else { }
if (m == 0) { break; } } return down; } int Up(int n, int m) { int up = 0; for (int i = n - 1; i >= 0; i--) { if (m >= (*v)[i].weight) { m -= (*v)[i].weight; up += (*v)[i].desirability; } else { up += m * (*v)[i].desirability / (*v)[i].weight; break; } } return up; } void Evaluation(Node& node) { int evaluation = node.desirability; int weight = node.weight; for (int i = node.index; i >= 0; i--) { if (weight >= (*v)[i].weight) { weight -= (*v)[i].weight; evaluation += (*v)[i].desirability; } else { evaluation += weight * (*v)[i].desirability / (*v)[i].weight; break; } } node.evaluation = evaluation; } int BranchAndBound(int n,int m) { int down = Down(n,m); int up = Up(n,m);
if (down == up) { return down; }
priority_queue<Node> q; Node root; root.index = n - 1; root.weight = m; root.desirability = 0; Evaluation(root); q.push(root); Node c1; Node c2; while (!q.empty()) { Node top = q.top(); q.pop();
if (top.index < 0) { down = down > top.desirability ? down : top.desirability; break; }
if (top.weight >= (*v)[top.index].weight) { c1.index = top.index - 1; c1.weight = top.weight - (*v)[top.index].weight; c1.desirability = top.desirability + (*v)[top.index].desirability; Evaluation(c1); if (c1.evaluation > down) { q.push(c1); } }
c2.index = top.index - 1; c2.weight = top.weight; c2.desirability = top.desirability; Evaluation(c2); if (c2.evaluation > down) { q.push(c2); } }
return down;
} bool cmp(Charm c1, Charm c2) { if (c1.desirability * c2.weight == c2.desirability * c1.weight) { return c1.weight < c2.weight; } else { return 1.0f * c1.desirability / c1.weight < 1.0f * c2.desirability / c2.weight; } } int CharmBracelet(vector<Charm>* v, int n, int m) { sort(v->begin(), v->end(), cmp);
int ans = BranchAndBound(n, m); return ans; } int main() { int n, m; cin >> n >> m; v = new vector<Charm>(n); for (int i = 0; i < n; i++) { cin >> (*v)[i].weight; cin >> (*v)[i].desirability; } int result = CharmBracelet(v, n, m); delete v; v = NULL; cout << result << endl; return 0; }
|