凸包+二进制枚举——poj1873
Posted zsben991126
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了凸包+二进制枚举——poj1873相关的知识,希望对你有一定的参考价值。
注意剪枝一下,不然会t
#include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> using namespace std; #define N 20 typedef double db; const db eps=1e-6; const db pi=acos(-1); int sign(db k){if (k>eps) return 1; else if (k<-eps) return -1; return 0;} int cmp(db k1,db k2){return sign(k1-k2);} int inmid(db k1,db k2,db k3){return sign(k1-k3)*sign(k2-k3)<=0;}// k3 在 [k1,k2] 内 struct point{ db x,y; int v,l; point operator + (const point &k1) const{return (point){k1.x+x,k1.y+y};} point operator - (const point &k1) const{return (point){x-k1.x,y-k1.y};} point operator * (db k1) const{return (point){x*k1,y*k1};} point operator / (db k1) const{return (point){x/k1,y/k1};} bool operator < (const point k1) const{ int a=cmp(x,k1.x); if (a==-1) return 1; else if (a==1) return 0; else return cmp(y,k1.y)==-1; } db abs(){return sqrt(x*x+y*y);} db abs2(){return x*x+y*y;} db dis(point k1){return ((*this)-k1).abs();} }; int inmid(point k1,point k2,point k3){return inmid(k1.x,k2.x,k3.x)&&inmid(k1.y,k2.y,k3.y);} db cross(point k1,point k2){return k1.x*k2.y-k1.y*k2.x;} db dot(point k1,point k2){return k1.x*k2.x+k1.y*k2.y;} vector<point> ConvexHull(vector<point> A, int flag){ int n=A.size();vector<point> ans(n*2); sort(A.begin(),A.end()); int now=-1; for(int i=0;i<A.size();i++){//下凸包 while(now>0 && sign(cross(ans[now]-ans[now-1],A[i]-ans[now-1]))<flag) now--; ans[++now]=A[i]; } int pre=now; for(int i=n-2;i>=0;i--){//上凸包 while(now>pre && sign(cross(ans[now]-ans[now-1],A[i]-ans[now-1]))<flag) now--; ans[++now]=A[i]; } if(n)ans.resize(now); return ans; } vector<point>p,v; int n; int Min,ans; void solve(int S){ v.clear(); int sum=0,tot=0;//可用篱笆长度 for(int i=0;i<n;i++){ if((S>>i) & 1)v.push_back(p[i]); else tot+=p[i].l,sum+=p[i].v; } if(sum>=Min)return; vector<point>res=ConvexHull(v,1); db len=0;//求凸包周长 for(int i=0;i<res.size();i++) len+=res[i].dis(res[(i+1)%v.size()]); if(sign(tot-len)>=0){ if(sum<Min){ Min=sum;ans=S; } } } int main(){ int tt=0; while(cin>>n && n){ Min=0x3f3f3f3f; tt++; p.clear(); for(int i=1;i<=n;i++){ point pp; scanf("%lf%lf%d%d",&pp.x,&pp.y,&pp.v,&pp.l); p.push_back(pp); } for(int S=1;S<(1<<n)-1;S++) solve(S); v.clear(); db tot=0; for(int i=0;i<n;i++){ if((ans>>i) & 1)v.push_back(p[i]); else tot+=p[i].l; } vector<point>res=ConvexHull(v,1); db len=0;//求凸包周长 for(int i=0;i<res.size();i++) len+=res[i].dis(res[(i+1)%v.size()]); if(tt>=2)puts(""); printf("Forest %d ",tt); printf("Cut these trees:"); for(int i=0;i<n;i++) if(!(ans>>i & 1))cout<<" "<<i+1; puts(""); printf("Extra wood: %.2f ",tot-len); } }
以上是关于凸包+二进制枚举——poj1873的主要内容,如果未能解决你的问题,请参考以下文章
Uva5211/POJ1873 The Fortified Forest 凸包