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
| #include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 20;
struct Vector { long long x = 0, y = 0, value = 0, len = 0;
Vector() = default; Vector(long long _x, long long _y, long long _v, long long _l) : x(_x), y(_y), value(_v), len(_l) {}
friend inline istream& operator>>(istream& is, Vector& obj) { is >> obj.x >> obj.y >> obj.value >> obj.len; return is; }
friend inline bool operator<(const Vector& a, const Vector& b) { return a.x == b.x ? a.y < b.y : a.x < b.x; }
friend inline Vector operator-(const Vector& a, const Vector& b) { return Vector(a.x - b.x, a.y - b.y, 0, 0); }
friend inline long long operator*(const Vector& a, const Vector& b) { return a.x * b.y - a.y * b.x; } } vec[MAXN], vec2[MAXN], vec3[MAXN];
int n, testCase, ansSize, tmpSize; double ansLen, ansValue; vector<int> vecTmp, vecAns;
inline void convex() { int stk[MAXN] = {0}, used[MAXN] = {0}, top(0); double res(0), value(0); for (int i = 1; i <= tmpSize; i++) vec2[i] = vec3[i]; sort(vec2 + 1, vec2 + 1 + tmpSize); for (auto &i : vecTmp) value += vec[i].value, res += vec[i].len; stk[++top] = 1; for (int i = 2; i <= tmpSize; i++) { while (top > 1 && (vec2[stk[top]] - vec2[stk[top - 1]]) * (vec2[i] - vec2[stk[top]]) <= 0) used[stk[top--]] = false; used[i] = true; stk[++top] = i; } int tmp = top; for (int i = tmpSize - 1; i > 0; i--) { if (!used[i]) { while (top > tmp && (vec2[stk[top]] - vec2[stk[top - 1]]) * (vec2[i] - vec2[stk[top]]) <= 0) used[stk[top--]] = false; used[i] = true; stk[++top] = i; } } for (int i = 1; i < top; i++) { Vector tmp = vec2[stk[i + 1]] - vec2[stk[i]]; res -= sqrt(tmp.x * tmp.x + tmp.y * tmp.y); }
if (res >= 0 && (value < ansValue || (value == ansValue && ansSize > vecTmp.size()))) { ansLen = res; ansValue = value; ansSize = vecTmp.size(); vecAns = vecTmp; } }
void dfs(int cur) { if (cur == n + 1) { convex(); return; } vecTmp.emplace_back(cur); dfs(cur + 1); vecTmp.pop_back();
vec3[++tmpSize] = vec[cur]; dfs(cur + 1); --tmpSize; }
int main() { std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int cnt(0); while (cin >> n && n) { cnt++; if (cnt > 1) cout << endl; ansLen = 1e15, ansValue = 1e15, ansSize = 20, tmpSize = 0; vecTmp.clear(), vecAns.clear(), vecTmp.clear(); for (int i = 1; i <= n; i++) cin >> vec[i]; dfs(1); cout.unsetf(ios_base::fixed); cout << "Forest " << cnt << endl; cout << "Cut these trees:"; for (auto &i : vecAns) cout << " " << i; cout << endl; cout << "Extra wood: " << setprecision(2) << fixed << ansLen << endl; } return 0; }
|