TZOJ 2569 Wooden Fence(凸包求周长)

描述

Did you ever wonder what happens to your money when you deposit them to a bank account? All banks hold such deposits in various assets, such as gold, stocks, obligations, deposits in other banks, loans, bonds, and many others. Due to the financial crisis and instability of the stock exchanges, many banks find out that stocks are not very reliable and their possession may be too risky.

Therefore, the banks now prefer other assets, especially gold. The main trouble with gold is that there is only a limited amount of it in the whole world. And it is not enough to cover all money held by all banks. (Wait, isn't this the real reason of the crisis?)

If there is not enough gold, other commodities must be exploited instead. The International Bank of Monetania (IBM) has recently come up with an idea of using very old and valuable trees as their assets. They bought a piece of land with several such trees and now expect their value to grow. Literally, of course.

Unfortunately, the trees are threatened by wildlife, because animals do not understand their value and nibble them. Moreover, there is a permanent danger of theft. As a result, it is absolutely necessary to build a good solid fence around the trees.

The IBM quickly realized that the only suitable material available to build the fence is the wood from the trees themselves. In other words, it is necessary to cut down some trees in order to build a fence around the remaining ones. Of course, to keep the maximum value, we want to minimize the value of the trees that had to be cut. You are to write a program that solves this problem.

输入

The input contains several test cases, each of which describes one piece of land. Each test case begins with a line containing a single integer N, 2 ≤ N≤ 16, the total number of trees. Each of the subsequent N lines contains 4 integers Xi, Yi, Vi, Li separated by at least one space.

The four numbers 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. You may assume that 0 ≤ Vi, Li ≤ 10000 and -10000 ≤ Xi, Yi ≤ 10000. No two trees in a test case will grow at the same position.

The input ends with a line containing zero in place of N.

输出

扫描二维码关注公众号,回复: 5167699 查看本文章

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 continuous fence. Find the subset with the minimal total value. For simplicity, regard the trees as having zero diameter.

Output one line with the sentence "The lost value is T.", where T is the minimal value of the trees that must be cut.

样例输入

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 3
5 -3 20 25
7 -3 30 32
2
100 0 5 4
0 100 4 5
5
0 0 10 10
0 1 10 10
1 0 10 10
1 1 10 10
50 50 8 4
0

样例输出

The lost value is 9.
The lost value is 20.
The lost value is 4.
The lost value is 8.

题意

N棵树位置(X,Y)价值V长度L,要求砍伐的树价值最小并且总长度能围住剩下的树

题解

暴力凸包求周长,复杂度O(2^N*NlogN)

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 struct Point{
 5     double x,y,V,L;
 6 }d[17],p[17];
 7 double dis(Point p1,Point p2){return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}
 8 double xmulti(Point p1,Point p2,Point p0){return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}
 9 double graham(int n)
10 {
11     int pl[17],t=1,num=1;
12     for(int i=1;i<=n;i++)if(p[i].y<p[t].y)t=i;
13     pl[1]=t;
14     do
15     {
16         num++;
17         t=pl[num-1]+1;
18         if(t>n)t=1;
19         for(int i=1;i<=n;i++)
20         {
21             double x=xmulti(p[i],p[t],p[pl[num-1]]);
22             if(x<0)t=i;
23         }
24         pl[num]=t;
25     }while(pl[num]!=pl[1]);
26     double sum=0;
27     for(int i=1;i<num;i++)
28         sum+=dis(p[pl[i]],p[pl[i+1]]);
29     return sum;
30 }
31 int main()
32 {
33     int n;
34     while(scanf("%d",&n)!=EOF,n)
35     {
36         double minn=0;
37         for(int i=0;i<n;i++)scanf("%lf%lf%lf%lf",&d[i].x,&d[i].y,&d[i].V,&d[i].L),minn+=d[i].V;
38         int state=1<<n;
39         for(int i=0;i<state;i++)
40         {
41             int sxV=0,sxL=0,pos=0;
42             for(int j=0;j<n;j++)
43             {
44                 if((i&(1<<j))==0)sxV+=d[j].V,sxL+=d[j].L;
45                 else p[++pos]=d[j];
46             }
47             if(minn>sxV&&graham(pos)<=sxL)minn=sxV;
48         }
49         printf("The lost value is %.0f.\n",minn);
50     }
51     return 0;
52 }

猜你喜欢

转载自www.cnblogs.com/taozi1115402474/p/10374131.html