poj 1873 The Fortified Forest

题目衔接:http://poj.org/problem?id=1873

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 8124 Accepted: 2232

Description

Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been gathered by his ancestors on their travels. To protect his trees from thieves, the king ordered that a high fence be built around them. His wizard was put in charge of the operation.
Alas, the wizard quickly noticed that the only suitable material available to build the fence was the wood from the trees themselves. In other words, it was necessary to cut down some trees in order to build a fence around the remaining trees. Of course, to prevent his head from being chopped off, the wizard wanted to minimize the value of the trees that had to be cut. The wizard went to his tower and stayed there until he had found the best possible solution to the problem. The fence was then built and everyone lived happily ever after.

You are to write a program that solves the problem the wizard faced.

Input

The input contains several test cases, each of which describes a hypothetical forest. Each test case begins with a line containing a single integer n, 2 <= n <= 15, the number of trees in the forest. The trees are identified by consecutive integers 1 to n. Each of the subsequent n lines contains 4 integers xi, yi, vi, li that describe a single tree. (xi, yi) is the position of the tree in the plane, vi is its value, and li is the length of fence that can be built using the wood of the tree. vi and li are between 0 and 10,000.
The input ends with an empty test case (n = 0).

Output

For each test case, compute a subset of the trees such that, using the wood from that subset, the remaining trees can be enclosed in a single fence. Find the subset with minimum value. If more than one such minimum-value subset exists, choose one with the smallest number of trees. For simplicity, regard the trees as having zero diameter.
Display, as shown below, the test case numbers (1, 2, ...), the identity of each tree to be cut, and the length of the excess fencing (accurate to two fractional digits).

Display a blank line between test cases.

 

Sample Input

6
 0  0  8  3
 1  4  3  2
 2  1  7  1
 4  1  2  3
 3  5  4  6
 2  3  9  8
3
 3  0 10  2
 5  5 20 25
 7 -3 30 32
0

Sample Output

Forest 1
Cut these trees: 2 4 5 
Extra wood: 3.16

Forest 2
Cut these trees: 2 
Extra wood: 15.00

题目大意:给你一些树的坐标,现在要将这些树围起来,围的栅栏刚好需要砍伐一些树,现在问你,需要砍哪些树使得砍掉的树的价值最小

思路:注意只有一棵树两棵树的情况,剩下注意点:
第一点:要围住这些树
第二点:价值要最小

价值相同时取砍的树最少的方案
因为最多15棵树所以我们暴力枚举每一颗树的状态即可,最后注意换行和用c++交即可 

代码:

/*
题目大意:给你一些树的坐标,现在要将这些树围起来,围的栅栏刚好需要砍伐一些树,现在问你,需要砍哪些树使得砍掉的树的价值最小

思路:注意只有一棵树两棵树的情况,剩下注意点:
第一点:要围住这些树
第二点:价值要最小
因为最多15棵树所以我们暴力枚举每一颗树的状态
*/
#include<set>
#include<map>
#include<ctime>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f
#define bug  printf("bug\n")
const int maxn=1e2+10;
const double pi=acos(-1.0);
const double esp=1e-6;
const int N=2e2+10;
int sign(double x)
{
    if(x==0)
        return 0;
    return x>esp?1:-1;
}

struct point
{
    double x,y;
    double val,len;
    point () {}
    point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    point operator -(const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
    double operator *(const point &b)const
    {
        return x*b.x+y*b.y;
    }
    double operator ^(const point &b)const
    {
        return x*b.y-y*b.y;
    }
};
double dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
struct line
{
    point st,ed;
    double k,b;
    line() {}
    line(point _s,point _e)
    {
        st=_s,ed=_e;
        k=atan2(ed.y-st.y,ed.x-st.x);
    }
    point operator&(const line &b)const
    {
        point res=st;
        double t=((st-b.st)^(b.st-b.ed))/((st-ed)^(b.st-b.ed));
        res.x+=(ed.x-st.x)*t;
        res.y+=(ed.y-st.y)*t;
        return res;
    }
};
double cmult(point a,point b,point c)
{
    return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
}
point p[maxn],p1[maxn];
int Stack[maxn],top;
bool cmp(point a,point b)
{
    int ju=cmult(p[0],a,b);
    if(ju>0)
        return 1;
    else if(ju==0&&dis(a,p[0])<dis(b,p[0]))
        return 1;
    return 0;
}
double tb(int n)
{
    point p0;
    int k=0;
    p0=p[0];
    for(int i=1; i<n; i++)
    {
        if(p0.y>p[i].y||(p0.x==p[i].x&&p0.y==p[i].y))
        {
            p0=p[i];
            k=i;
        }
    }
    swap(p[k],p[0]);
    sort(p+1,p+n,cmp);
    for(int i=0; i<2; i++)
        Stack[i]=i;
    top=1;
    for(int i=2; i<n; i++)
    {
        while(top>0&&cmult(p[Stack[top-1]],p[Stack[top]],p[i])<=0)
            top--;
        top++;
        Stack[top]=i;
    }
    double len=0;
    for(int i=0; i<top; i++)
    {
        len+=dis(p[Stack[i]],p[Stack[i+1]]);
    }
    len+=dis(p[Stack[0]],p[Stack[top]]);
    //printf("%.2lf\n",len);
    return len;
}
double minlen,minval,lastlen;
int a[maxn],b[maxn];
double nowlen,nowval;
int nowcut,lastcut;
int n;

void dfs(int now)
{
    if(now==n)
    {
        nowcut=0;
        nowlen=0;
        nowval=0;
        int cnt=0;
        for(int i=0; i<n; i++)
        {
            if(!a[i])
            {
                p[cnt++]=p1[i];
            }
            else
            {
                nowlen+=p1[i].len;
                nowval+=p1[i].val;
            }
        }
        nowcut=n-cnt;
        if(cnt==0||cnt==n)
            return ;
        if(cnt==1)///只剩一颗树
        {
            if(minval>nowval)
            {
                minval=nowval;
                minlen=nowlen;
                lastcut=nowcut;
                for(int i=0; i<n; i++)
                {
                    b[i]=a[i];
                }
            }
            else if(minval==nowval)
            {
                if(nowcut<lastcut)
                {
                    minval=nowval;
                    minlen=nowlen;
                    lastcut=nowcut;
                    for(int i=0; i<n; i++)
                    {
                        b[i]=a[i];
                    }
                }
            }
            return;
        }
        double len=0;
        if(cnt==2)
        {
            len+=dis(p[0],p[1]);
            len*=2;
        }
        else
        {
            len=tb(cnt);
        }
//        printf("%.2lf %.2lf\n",nowlen,minlen);
        if(len<=nowlen)
        {
            if(minval>nowval)
            {
                minval=nowval;
                minlen=nowlen-len;
                lastcut=nowcut;
                for(int i=0; i<n; i++)
                    b[i]=a[i];
            }
            else if(minval==nowval)
            {
                if(lastcut>nowcut)
                {
                    minval=nowval;
                    minlen=nowlen-len;
                    nowcut=lastcut;
                    for(int i=0; i<n; i++)
                    {
                        b[i]=a[i];
                    }
                }
            }
        }
        return ;
    }
    a[now]=0;
    dfs(now+1);
    a[now]=1;
    dfs(now+1);
}
int main()
{
    //freopen("F:\\test.out","w",stdout);
    int q=1;
    while(scanf("%d",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        if(n==0)
            break;
        for(int i=0; i<n; i++)
        {
            scanf("%lf %lf %lf %lf",&p1[i].x,&p1[i].y,&p1[i].val,&p1[i].len);
        }
        minlen=inf;
        minval=inf;
        dfs(0);
        printf("Forest %d\n",q++);
        printf("Cut these trees: ");
        for(int i=0; i<n; i++)
        {
            if(b[i])
                printf("%d ",i+1);
        }
        printf("\nExtra wood: %.2lf\n\n",minlen);
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值