poj 1873 The Fortified Forest 搜索+凸包
2016-08-31 13:00
519 查看
链接
题意:
给出一些树(2<=n<=15),每个树有一个二维坐标,现在要砍下一些树,去做成围栏把其它树围起来。每棵树都有价值,还有砍了后能做成多少米的围栏。为了节约材料,尽量使围栏的周长小。
现在要求使砍树的价值和最小,在此情况下要求砍树的数目最小。
要求输出砍掉哪些树,并且做完围栏后多余的材料可以围多少米。
解法:
枚举所有状态(选中哪些树,不选哪些树),然后进行凸包。代码:
#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<vector> using namespace std; #define all(x) (x).begin(), (x).end() #define for0(a, n) for (int (a) = 0; (a) < (n); (a)++) #define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++) #define mes(a,x,s) memset(a,x,(s)*sizeof a[0]) #define mem(a,x) memset(a,x,sizeof a) #define ysk(x) (1<<(x)) #define sqr(x) ((x)*(x)) typedef long long ll; typedef pair<int, int> pii; const int INF =0x3f3f3f3f; const int maxn= 15 ; const double PI=acos(-1.0); const double eps=1e-10; int dcmp(double x) { if(fabs(x)<eps) return 0; else return x<0?-1:1; } struct Point { double x,y; int val,len,id; Point(double x=0,double y=0):x(x),y(y) {}; bool operator ==(const Point B)const {return dcmp(x-B.x)==0&&dcmp(y-B.y)==0;} bool operator<(const Point& b)const { return dcmp(x-b.x)<0|| dcmp(x-b.x)==0 &&dcmp(y-b.y)<0; } }; typedef Point Vector; Vector operator -(Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y); } double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;} double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;} Vector operator +(Vector A,Vector B) {return Vector(A.x+B.x,A.y+B.y); } Vector operator *(Vector A,double p) {return Vector(A.x*p,A.y*p); } Vector operator /(Vector A,double p) {return Vector(A.x/p,A.y/p); } Vector operator -(Vector A) {return Vector(-A.x,-A.y);} double dis(Point A,Point B) { return sqrt(sqr(A.x-B.x)+sqr(A.y-B.y)); } Point po[maxn+10],chp[maxn+10]; bool use[maxn+4]; int n; double ConvexHell(Point *p,int n,Point *ch,int rea) { int m=0; for(int i=0;i<n;i++) if(use[i]) { while(m>1&&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-1] )<=0 ) m--; ch[m++]=p[i]; } int k=m; for(int i=n-2;i>=0;i--) if(use[i]) { while(m>k &&Cross(ch[m-1]-ch[m-2],p[i]-ch[m-1] )<=0) m--; ch[m++]=p[i]; } if(rea>1) m--; // if(m<=1) return 0; // if(m==2) return dis(ch[0],ch[1]); double D=0; for0(i,m) { D+=dis(ch[i],ch[ (i+1)%m ]); } // cout<<"Convex "<<m<<endl; return D; } void solve() { int ed=ysk(n)-1; int minVal=-1,num=-1,best=-1; double extr=-1; for0(s,ed+1) { int v=0,cnt=0,L=0; for0(i,n) { if(s&ysk(i)) use[i]=1; else {use[i]=0;L+=po[i].len;v+=po[i].val,cnt++;} } double D=ConvexHell(po,n,chp,n-cnt); // cout<<s<<" "<<D<<endl; if(dcmp(L-D)<0) continue; if(minVal<0||minVal>v||minVal==v&&num>cnt ) {minVal=v;num=cnt;best=s;extr=L-D;} } printf("Cut these trees:"); vector<int >ve; for0(i,n) if( !(ysk(i)&best) ) ve.push_back(po[i].id+1); sort(all(ve)); for0(i,ve.size()) printf(" %d",ve[i]); putchar('\n'); printf("Extra wood: %.2f\n\n",extr); } int main() { std::ios::sync_with_stdio(false); int kase=0; while(cin>>n&&n) { for0(i,n) {cin>>po[i].x>>po[i].y>>po[i].val>>po[i].len;po[i].id=i;} sort(po,po+n); printf("Forest %d\n",++kase); solve(); } return 0; }
相关文章推荐
- poj 1873 The Fortified Forest(凸包)
- POJ 1873 The Fortified Forest(凸包)
- POJ 1873 The Fortified Forest(凸包+枚举)
- POJ 1873 The Fortified Forest 暴力凸包
- POJ 1873 The Fortified Forest [凸包 枚举]
- POJ 1873 The Fortified Forest 二进制枚举 + 凸包 (final水题)
- POJ 1873 The Fortified Forest 凸包 二进制枚举
- POJ 1873 The Fortified Forest(凸包+枚举)
- POJ 1873 The Fortified Forest (凸包,状态压缩枚举)
- POJ 1873 The Fortified Forest 二进制枚举 + 凸包 (final水题)
- poj 1873 The Fortified Forest 凸包+位运算枚举 world final 水题
- poj 1873 The Fortified Forest(凸包)
- poj 1873 The Fortified Forest (位运算枚举 + 凸包周长)
- POJ 1873 The Fortified Forest 暴力凸包
- The Fortified Forest - POJ 1873(状态枚举+求凸包周长)
- POJ 1873 The Fortified Forest(凸包+状态枚举)
- poj 1873 The Fortified Forest (凸包)
- POJ 1873 The Fortified Forest (计算几何,枚举+凸包)
- 简单几何(凸包+枚举) POJ 1873 The Fortified Forest
- POJ 1873 The Fortified Forest (状态压缩+凸包 WF 推荐)