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
Inputcopy | Outputcopy |
---|---|
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 | Forest 1 Cut these trees: 2 4 5 Extra wood: 3.16 Forest 2 Cut these trees: 2 Extra wood: 15.00 |
思路:
深搜,记录每次砍的树的数,价值, li和,还有没砍树的编号。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define per(i,a,b) for(int i=a;i<=b;i++)
#define ber(i,a,b) for(int i=a;i>=b;i--)
const int N = 1e4 + 1000;
const double eps = 1e-8;
double PI = (double)acos(-1 * 1.0);
struct Point
{
double x;
double y;
double vau;
double li;
}p[N],s[N],a[N],b[N];
int c[N],an[N];
int n,to;
double exw, cnum=1e9, cvau=1e9;
bool cmp(struct Point& a, struct Point& b)
{
if (a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
double cross(struct Point& a, struct Point& b, struct Point& c)
{
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
double dis(struct Point& a, struct Point& b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double Andrew(int n)
{
if (n <= 1) return 0;
if (n == 2) return 2 * dis(p[1], p[2]);
sort(p + 1, p + 1 + n, cmp);
int top = 0;
per(i, 1, n)
{
while (top > 1 && cross(s[top - 1], s[top], p[i]) <= 0) top--;
s[++top] = p[i];
}
int t = top;
ber(i, n - 1, 1)
{
while (top > t && cross(s[top - 1], s[top], p[i]) <= 0)top--;
s[++top] = p[i];
}
double ans = 0;
per(i, 1, top - 1)
ans += dis(s[i], s[i + 1]);
return ans;
}
void dfs(int no, int num, double v, double li)
{
if (v > cvau || (v == cvau && num > cnum)) return;
if (no == n + 1)
{
per(i, 1, to)
p[i] = b[i];
double lon = Andrew(to);
if (li >= lon&&(v<=cvau||v==cvau&&num<=cnum))
{
cnum = num;
cvau = v;
exw = li - lon;
for (int i = 1; i <= num; i++)
an[i] = c[i];
}
return;
}
to++;
b[to] = a[no];
dfs(no + 1, num, v, li);
to--;
c[num + 1] = no;
dfs(no + 1, num + 1, v + a[no].vau, li + a[no].li);
}
int main()
{
int cn = 0;
while (cin >> n)
{
if (n == 0)
break;
cn++;
to = 0;
cnum = 1e9;
cvau = 1e9;
per(i, 1, n)
cin >> a[i].x >> a[i].y >> a[i].vau >> a[i].li;
dfs(1, 0, 0, 0);
printf("Forest %d\n", cn);
printf("Cut these trees:");
per(i, 1, cnum)
cout << " " << an[i];
cout << endl;
printf("Extra wood: %.2lf\n", exw);
cout << endl;
}
return 0;
}