The Fortified Forest
Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been gathered by his ancestors on their travels. To protect his trees from thieves, the king ordered that a high fence be built around them. His wizard was put in charge of the operation.
Alas, the wizard quickly noticed that the only suitable material available to build the fence was the wood from the trees themselves. In other words, it was necessary to cut down some trees in order to build a fence around the remaining trees. Of course, to prevent his head from being chopped off, the wizard wanted to minimize the value of the trees that had to be cut. The wizard went to his tower and stayed there until he had found the best possible solution to the problem. The fence was then built and everyone lived happily ever after.
You are to write a program that solves the problem the wizard faced.
Input
The input contains several test cases, each of which describes a hypothetical forest. Each test case begins with a line containing a single integer n, 2 <= n <= 15, the number of trees in the forest. The trees are identified by consecutive integers 1 to n. Each of the subsequent n lines contains 4 integers xi, yi, vi, li that describe a single tree. (xi, yi) is the position of the tree in the plane, vi is its value, and li is the length of fence that can be built using the wood of the tree. vi and li are between 0 and 10,000.
The input ends with an empty test case (n = 0).
Output
For each test case, compute a subset of the trees such that, using the wood from that subset, the remaining trees can be enclosed in a single fence. Find the subset with minimum value. If more than one such minimum-value subset exists, choose one with the smallest number of trees. For simplicity, regard the trees as having zero diameter.
Display, as shown below, the test case numbers (1, 2, ...), the identity of each tree to be cut, and the length of the excess fencing (accurate to two fractional digits).
Display a blank line between test cases.
Sample Input
6
0 0 8 3
1 4 3 2
2 1 7 1
4 1 2 3
3 5 4 6
2 3 9 8
3
3 0 10 2
5 5 20 25
7 -3 30 32
0
Sample Output
Forest 1
Cut these trees: 2 4 5
Extra wood: 3.16
Forest 2
Cut these trees: 2
Extra wood: 15.00
Source
题目类型:二进制枚举+凸包
算法分析:由于n比较小,所以可以使用二进制枚举子集的方法来删点,然后求剩下点的凸包,并按照val值和len的限制更新答案。注意若最后只剩下一个点,则此时凸包周长为0!!!
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
/************************************************* Author :supermaker Created Time :2016/1/27 13:44:04 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 66 + 66; int sgn (double val) { if (fabs (val) < EPS) return 0; else if (val < 0) return -1; return 1; } struct Point { double x, y; Point () {} Point (double xx, double yy) : x (xx), y (yy) {} Point operator - (const Point &a) const { return Point (x - a.x, y - a.y); } double operator ^ (const Point &a) const { return x * a.y - y * a.x; } double operator * (const Point &a) const { return x * a.x + y * a.y; } }; Point p[maxn], tp[maxn]; int val[maxn], llen[maxn], n, tp_len; int cur_val, cur_len; int fin_val, fin_num, fin_res; double fin_len; stack <int> sta; double Dist (Point p1, Point p2) { return sqrt ((p2 - p1) * (p2 - p1)); } bool polarcmp (Point p1, Point p2) { double tt = (p1 - tp[1]) ^ (p2 - tp[1]); if (sgn (tt) == 1) return true; else if (sgn (tt) == 0 && sgn (Dist (p1, tp[1]) - Dist (p2, tp[1])) <= 0) return true; return false; } double Garham () { Point start = tp[1]; int start_pos = 1; for (int i = 1; i <= tp_len; i++) if (sgn (tp[i].x - start.x) == -1 || (sgn (tp[i].x - start.x) == 0 && sgn (tp[i].y - start.y) == -1)) start = tp[i], start_pos = i; swap (tp[start_pos], tp[1]); sort (tp + 2, tp + 1 + tp_len, polarcmp); while (!sta.empty ()) sta.pop (); if (tp_len == 0) return 0; else if (tp_len == 1) return 0; else if (tp_len == 2) return 2.0 * Dist (tp[1], tp[2]); else { sta.push (1), sta.push (2); for (int i = 3; i <= tp_len; i++) { while (sta.size () >= 2) { int tt1 = sta.top (); sta.pop (); int tt2 = sta.top (); sta.pop (); if (sgn ((tp[tt1] - tp[tt2]) ^ (tp[i] - tp[tt2])) <= 0) sta.push (tt2); else { sta.push (tt2), sta.push (tt1); break; } } sta.push (i); } double res = 0; int per = sta.top (); sta.pop (); int cur = per, beg = per; while (!sta.empty ()) { cur = sta.top (); sta.pop (); res += Dist (tp[per], tp[cur]); per = cur; } res += Dist (tp[cur], tp[beg]); return res; } } int main() { //CFF; //CPPFF; int flag = 1; while (scanf ("%d", &n) != EOF) { if (n == 0) break; for (int i = 1; i <= n; i++) scanf ("%lf%lf%d%d", &p[i].x, &p[i].y, &val[i], &llen[i]); fin_val = fin_num = INF; fin_res = 0; fin_len = INF; for (int i = 0; i < (1 << n); i++) { cur_val = cur_len = tp_len = 0; for (int j = 1; j <= n; j++) { if (i & (1 << (j - 1))) cur_val += val[j], cur_len += llen[j]; else tp[++tp_len] = p[j]; } if (cur_val > fin_val) continue; double tt = Garham (); if (sgn (cur_len - tt) == 1) { if (cur_val < fin_val || (cur_val == fin_val && n - tp_len < fin_num)) { fin_val = cur_val; fin_num = n - tp_len; fin_res = i; fin_len = cur_len - tt; } } } if (flag > 1) puts (""); printf ("Forest %d\n", flag++); printf ("Cut these trees:"); for (int i = 1; i <= n; i++) if (fin_res & (1 << (i - 1))) printf (" %d", i); puts (""); printf ("Extra wood: %.2f\n", fin_len); } return 0; } |
- « 上一篇:poj2007
- poj2187:下一篇 »