POJ 1873 The Fortified Forest

//World Final的水题,由于数据很小,我用的位运算枚举凸包。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=1e8;
const double EP=1e-8;
struct Point{int x, y, v, len;}p[16],pp[16];
struct plan{
 int value, num, treeid;
 double wood;
}ans, tmp;
double x_mult(Point sp, Point ep, Point op){
    return (sp.x-op.x)*(ep.y-op.y)-(sp.y-op.y)*(ep.x-op.x);
}
double dist(Point p1, Point p2){
    return sqrt(double((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));
}
bool cmp(const Point p1, const Point p2){
    return p1.y<p2.y ||(p1.y==p2.y&&p1.x<p2.x);
}

double graham_scan(int sum){
    Point res[16];
    int i, len1, k=0, top=1;
    double cir=0;
    sort(pp,pp+sum,cmp);
    res[0]=pp[0];
    res[1]=pp[1];
    if(sum==2)
    return (dist(pp[0],pp[1])*2);
    res[2]=pp[2];
    for(i=0; i<sum; i++){
        while(top&&x_mult(pp[i],res[top],res[top-1])>=0)
            top--;
        res[++top]=pp[i];
    }
    len1=top; res[++top]=pp[sum-2];
    for(i=sum-3; i>=0; i--){
        while(top!=len1&&x_mult(pp[i],res[top],res[top-1])>=0)
            top--;
            res[++top]=pp[i];
    }
    for(i=1; i<top; i++)
        cir+=dist(res[i],res[i-1]);
    cir+=dist(res[top-1],pp[0]);
    return cir;
}
int main(){
    //freopen("1.txt", "r", stdin);
    int i,  k, n, t, sum, T=0;
    double need;
    while(scanf("%d", &n)!=EOF&&n){
        T++;
        for(i=0; i<n; i++)
        scanf("%d %d %d %d", &p[i].x, &p[i].y, &p[i].v, &p[i].len);
        k=(1<<n)-2;
  ans.value=INF;
        while(k>0){
            t=k;
            sum=0;
            tmp.value=0;tmp.wood=0;
            for(i=0; i<n; t>>=1,i++){  //0-cut
                if(t&1){
                    pp[sum++]=p[i];}
                else {
                    tmp.value+=p[i].v;
                    tmp.wood+=p[i].len;
                }
            }
            tmp.treeid=k;
            tmp.num=sum;
            if(sum==1)
            need=0;
            else need=graham_scan(sum);
   //cout<<k<<" "<<need<<endl;
            if(tmp.wood-need>-EP){
                tmp.wood-=need;
                if(tmp.value<ans.value||(tmp.value==ans.value&&tmp.num<ans.num))
                ans=tmp;
            }
            k--;
        }
  if(T>1)
   printf("/n");
        printf("Forest %d/n",T);
        printf("Cut these trees:");
        for(i=0; i<n; i++,ans.treeid>>=1)
            if(!(ans.treeid&1))
            printf(" %d", i+1);
        printf("/nExtra wood: %.2f/n", ans.wood);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值