POJ 1873 The Fortified Forest (convex hull + 压状 dp)

topic

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

The meaning of problems

Each point coordinates is known, and given the length of the value, which is selected from the circumference of the convex hull points consisting of a portion of the length greater than or equal to the remaining points, and is selected so that the sum of a small set of points selected point values, If the same number of points on the choice of a small set of points.
Dp enumeration with a press-shaped in each case, a representative of the selected points to 0 for dots of the convex hull. The following judge function (selected so that the point value of the selected set point a small sum, if the same number of points on the choice of a small set of points.) To determine whether to update the answer. Here there is a pit, the final output% .2f,%. 2lf be wrong. The reason is that the printf function is not to use% lf provisions,% lf with printf belong to undefined behavior. Some compilers will be converted to% f, while others will not. But scanf is to support and% f% lf read.

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#define ll long long
const int maxn = 17;
const double inf = 0x3f3f3f3f;
using namespace std;
int n;
struct point{
    double x, y, vl, len;
    int id;
    point (){}
    point (double a, double b):x(a),y(b){}
    point operator - (const point &b) const{
        return point(x-b.x, y-b.y);
    }
}p[maxn], c[maxn], s[maxn], res[maxn];
double cross(point a, point b){
    return a.x*b.y-a.y*b.x;
}
int cmp(point a, point b){
    return a.x < b.x ||(a.x==b.x && a.y < b.y);
}
int andrew( point a[], int n){
    sort(a, a+n, cmp);
    int m = 0, i;
    for(i = 0; i < n; i++){
        while(m > 1 && cross(res[m-1]-res[m-2], a[i] - res[m-2])<0)
            m--;
        res[m++] = a[i];
    }
    int k = m;
    for(i = n-2; i >= 0; i--){
        while(m > k && cross(res[m-1]-res[m-2], a[i] - res[m-2])<0)
            m--;
        res[m++] = a[i];
    }
    if(m>1) m--;
    return m;
}
double dis(point a, point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double arealen(point res[], int k){
    double ans = 0;
    int i;
    for(i = 0; i < k; i++){
        ans += dis(res[i], res[(i+1)%k]);
    }
    return ans;
}
int judge(double vsum,double vans, int lenc, int cans){
    if(vsum < vans) return 1;
    else if(vsum == vans){
        if(lenc < cans)
            return 1;
        else
            return 0;
    }
    return 0;
}
int main(){
    int n, i, m, j, cont = 1;
    int lenc, lens, cans;
    double vsum, lsum;
    double vans, andlen;
    double wood;
    int id[20];
    while(~scanf("%d", &n) && n){
        cans = 20;vans = inf;
        for(i = 0; i < n; i++){
            scanf("%lf%lf%lf%lf", &p[i].x, &p[i].y, &p[i].vl, &p[i].len);
            p[i].id = i+1;
        }
        m = (1<<n)-1;
        for(i = 0; i <= m; i++){
            memset(c, 0, sizeof(c));
            memset(s, 0, sizeof(s));
            vsum = lsum = 0;
            lenc = lens = 0;
            for(j = 0; j < n; j++){
                if( i&(1<<j) ){
                    vsum += p[j].vl;
                    lsum += p[j].len;
                    c[lenc++] = p[j];
                }
                else
                    s[lens++] = p[j];
            }
            int k = andrew(s, lens);
            andlen = arealen(res, k);
            if(andlen <= lsum && judge(vsum, vans, lenc, cans)){
                vans = vsum;
                wood = lsum-andlen;
                cans = lenc;
                for(int q = 0; q < lenc; q++)
                    id[q] = c[q].id;
            }

        }
        printf("Forest %d\nCut these trees:", cont++);
        for(int q = 0; q < cans; q++){
            printf(" %d", id[q]);
        }
        printf("\nExtra wood: %.2f\n\n", wood);
    }
	return 0;
}
Published 52 original articles · won praise 2 · Views 892

Guess you like

Origin blog.csdn.net/qq_44714572/article/details/98349468