码迷,mamicode.com
首页 > 其他好文 > 详细

凸包+二进制枚举——poj1873

时间:2020-02-25 00:27:24      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:sign   math   opera   int   namespace   amp   put   ble   const   

注意剪枝一下,不然会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\n",tt);
        printf("Cut these trees:");
        for(int i=0;i<n;i++)
            if(!(ans>>i & 1))cout<<" "<<i+1;
        puts("");
        printf("Extra wood: %.2f\n",tot-len);
    }
}

 

凸包+二进制枚举——poj1873

标签:sign   math   opera   int   namespace   amp   put   ble   const   

原文地址:https://www.cnblogs.com/zsben991126/p/12359437.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!