poj 1873
哇实验室里正在吵架,爽死了!
wf水题。显然二进制枚举,注意剪枝,val>ans的时候剪一下,不然会tle。然后就没惹。
我老人家一开始写了个
感觉非常垃圾,wa了一发又t了一发。
感觉自己可以退役了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 #include <iomanip> 6 #include <algorithm> 7 #include <vector> 8 typedef double db; 9 #define pdd pair<db,db> 10 const db eps = 1e-6; 11 const db pi = acos(-1); 12 using namespace std; 13 int sign(db k){ 14 if (k>eps) return 1; else if (k<-eps) return -1; return 0; 15 } 16 int cmp(db k1,db k2){return sign(k1-k2);} 17 struct point{ 18 db x,y; 19 db v,l; 20 point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};} 21 point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};} 22 point operator * (db k1) const{return (point){x*k1,y*k1};} 23 point operator / (db k1) const{return (point){x/k1,y/k1};} 24 bool operator <(const point &k1)const { 25 int c=cmp(x,k1.x); 26 if(c)return c==-1; 27 return cmp(y,k1.y)==-1; 28 } 29 db abs(){ return sqrt(x*x+y*y);} 30 db dis(point k1){return ((*this)-k1).abs();} 31 }; 32 db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;} 33 db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;} 34 vector<point> convexHull(vector<point> ps){ 35 int n = ps.size();if(n<=1)return ps; 36 sort(ps.begin(),ps.end()); 37 vector<point> qs(n*2);int k=0; 38 for(int i=0;i<n;qs[k++]=ps[i++]) 39 while (k>1&&cross(qs[k-1]-qs[k-2],ps[i]-qs[k-2])<=0)--k; 40 for(int i=n-2,t=k;i>=0;qs[k++]=ps[i--]) 41 while (k>t&&cross(qs[k-1]-qs[k-2],ps[i]-qs[k-2])<=0)--k; 42 qs.resize(k-1); 43 return qs; 44 } 45 int n; 46 point p[19]; 47 vector<point> v; 48 db res=0,sum=1e9;int ans=-1; 49 void slove(int x){ 50 v.clear(); 51 db l1=0,val=0; 52 for(int i=0;i<n;i++){ 53 if(1&(x>>i)){//砍了 54 l1+=p[i].l; 55 val+=p[i].v; 56 } else{ 57 v.push_back(p[i]); 58 } 59 } 60 if(cmp(val,sum)>=0) 61 return; 62 v=convexHull(v); 63 db l2=0; 64 for(int i=0;i<v.size();i++){ 65 l2+=v[i].dis(v[(i+1)%v.size()]); 66 } 67 if(v.size()==1)l2=0; 68 if(cmp(l1,l2)>=0){//阔以 69 sum=val; 70 res=l1-l2; 71 ans=x; 72 } 73 } 74 int main(){ 75 int cas = 0; 76 while (scanf("%d",&n)&&n){ 77 cas++; 78 for(int i=0;i<n;i++){ 79 scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].v,&p[i].l); 80 } 81 for(int i=1;i<(1<<n)-1;i++){ 82 slove(i); 83 } 84 if(cas>=2)printf("\n"); 85 printf("Forest %d\n",cas); 86 printf("Cut these trees:"); 87 for(int i=0;i<n;i++){ 88 if((ans>>i)&1){ 89 printf(" %d",i+1); 90 } 91 } 92 printf("\nExtra wood: %.2f\n",res); 93 res=0,sum=1e9; 94 } 95 }