POJ - 1873 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

给出一些树(植物),每棵树有坐标,高度,以及价值,要求砍掉一些树,用那些木材,将其它树围起来,要求花最小的代价,代价相同,要求砍掉最少的树。

我们需要枚举哪些树要砍掉,不过不要用暴力枚举法,而是利用二进制来做。

对于n棵树,一共有1<<n种状态,我们枚举每一种状态i。对于每一种状态i,我们枚举每一棵树j,如果符合!(1<<j)&i==0,说明当前树被选中了,放入凸包当中,如果没有,我们就累加一下长度和价值。枚举完毕后,我们有一个剪枝,那就是如果当前我们累加的价值大于我们之前循环中求到的最低价值了(题目要找最低值嘛),那么我们continue即可,无论后面能不能包住凸包,我们都不会选择这个的,因为有更好的了。 如果小于等于,那么我们就求一下凸包长度,如果凸包长度小于等于累加的长度,那么我们就要对比一下了,如果累加价值小于当前得到的最低价值,或者等于但是数量更少。那就更新一下数据。

最后输出即可。他要输出哪些树被选出来了,对于二进制表示的最佳状态i来说,我们枚举每一棵树j,((1<<j)&i)!=0,那么我们就输出这个j。

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<string> #include<algorithm> #include<queue> #define LL long long #define eps 1e-7 #define N 2000000 #define MOD 1000000007 #define inf 1<<30 #define zero(a) (fabs((double)(a))<eps) using namespace std; struct Point{     int x,y,v,l; }p[15]; int n; //存放没有被砍的树 vector<Point>a; //叉积 int xmul(Point p0,Point p1,Point p2){     return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x); } //计算距离 double dist(Point p1,Point p2){     return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y)); } //极角排序 bool cmp(Point p1,Point p2){     if(xmul(a[0],p1,p2)>0) return true;     else if(zero(xmul(a[0],p1,p2))&&dist(a[0],p1)<dist(a[0],p2)) return true;     return false; } double Grham_scan(int len){     //如果只剩下一棵树就不用围了     if(a.size()==1) return len;     //如果只剩下两棵树,那就是二者距离和的2倍,注意是2倍,可以从样例中看出来     else if(a.size()==2) return len-dist(a[0],a[1])*2;     for(int i=1;i<a.size();i++)         if(a[i].y<a[0].y||(a[i].y==a[0].y&&a[i].x<a[0].x))             swap(a[0],a[i]);     sort(a.begin()+1,a.end(),cmp);     vector<Point>s;     s.push_back(a[0]);s.push_back(a[1]);s.push_back(a[2]);     for(int i=3;i<a.size();i++){         while(s.size()>=2&&xmul(s[s.size()-2],s[s.size()-1],a[i])<eps)             s.pop_back();         s.push_back(a[i]);     }     s.push_back(s[0]);     double ans=0;     //求凸包周长     for(int i=0;i<s.size()-1;i++)         ans+=dist(s[i],s[i+1]);     return len-ans; } int main(){     int cas=0;     while(scanf("%d",&n)!=EOF&&n){         for(int i=0;i<n;i++)             scanf("%d%d%d%d",&p[i].x,&p[i].y,&p[i].v,&p[i].l);         //最优解的代价,砍掉树目的数量,最优状态         int best_val=inf,best_num,best_state;         //最优解剩下的木材         double best_extra;         //枚举         for(int i=1;i<(1<<n)-1;i++){             int tmp_val=0,tmp_len=0;             a.clear();             for(int j=0;j<n;j++){                 if(!((1<<j)&i))                     a.push_back(p[j]);                 else{                     tmp_len+=p[j].l;                     tmp_val+=p[j].v;                 }             }             //小小剪枝             if(tmp_val>best_val)  continue;             double extra=Grham_scan(tmp_len);             //如果extra<0说明不够用             if(extra>=0){                 if(tmp_val<best_val){                     best_val=tmp_val;                     best_num=n-a.size();                     best_state=i;                     best_extra=extra;                 }                 else if(tmp_val==best_val&&n-a.size()<best_num){                     best_num=n-a.size();                     best_state=i;                     best_extra=extra;                 }             }         }         printf("Forest %d\nCut these trees:",++cas);         for(int i=0;i<n;i++)             if((1<<i)&best_state)  printf(" %d",i+1);         printf("\nExtra wood: %.2f\n\n",best_extra);     }     return 0; }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值