poj 1873 The Fortified Forest(매거진 + 플랜지 길이 계산)

The Fortified Forest
Time Limit: 1000MS
 
Memory Limit: 30000K
Total Submissions: 4056
 
Accepted: 1161
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
Source
World Finals 1999
제목:http://poj.org/problem?id=1873
제목: n그루의 나무를 드리겠습니다. 지금 n그루의 나무에서 나무를 베어내라고 합니다. 그리고 이 나무로 담을 만들고 남은 나무를 둘러싸세요. 나무마다 가치가 있습니다. 베어낸 나무의 가치와 최소를 만들 수 있습니다. 만약에 같은 방안이 있다면 베어낸 나무의 수량을 최소로 할 수 있습니다.
분석: 이 문제의 데이터 범위는 상당히 작다. 어떤 나무를 잘라내는지 직접 일일이 열거한 다음에 이런 방안의 비용을 계산하고 가장 좋은 것을 취하면 된다. 매번 볼록한 가장자리의 길이를 구하고 베어낸 나무의 길이보다 작은지 보고 이런 비용이 더 작은지 판단한다.
코드:
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef int mType;
struct Tpoint
{
    mType x,y;
    Tpoint(){}
    Tpoint(mType _x,mType _y):x(_x),y(_y){}
}g[55],q[55],o[55];
Tpoint MakeVector(Tpoint P,Tpoint Q)
{
    return Tpoint(Q.x-P.x,Q.y-P.y);
}
mType CrossProduct(Tpoint P,Tpoint Q)
{
    return P.x*Q.y-P.y*Q.x;
}
mType MultiCross(Tpoint P,Tpoint Q,Tpoint R)
{
    return CrossProduct(MakeVector(Q,P),MakeVector(Q,R));
}
long long SqrDis(Tpoint P,Tpoint Q)
{
    return 1LL*(P.x-Q.x)*(P.x-Q.x)+1LL*(P.y-Q.y)*(P.y-Q.y);
}
bool cmp(Tpoint P,Tpoint Q)
{
    mType tmp=MultiCross(P,Q,g[0]);
    if(tmp<0)return 1;
    if(tmp>0)return 0;
    return SqrDis(P,g[0])>SqrDis(Q,g[0]);
}
void Graham(int n,int &m)
{
    int i,j;
    for(j=i=0;i<n;++i)
        if(g[i].x<g[j].x||(g[i].x==g[j].x&&g[i].y<g[j].y))j=i;
    swap(g[0],g[j]);
    sort(g+1,g+n,cmp);
    q[m=0]=g[0];
    for(i=1;i<n;++i)
    {
        while(m&&MultiCross(q[m-1],q[m],g[i])>0)--m;
        q[++m]=g[i];
    }
}
int v[55],l[55];
int i,n,ans,mcost,state,cs=0;
double ex;
double Dis(Tpoint P,Tpoint Q)
{
    return sqrt(1.0*(P.x-Q.x)*(P.x-Q.x)+1.0*(P.y-Q.y)*(P.y-Q.y));
}
void solve(int s)
{
    int i,cost=0,len=0,num=0,m;
    double need=0;
    for(i=0;i<n;++i)
        if(s&(1<<i))
        {
            cost+=v[i];
            len+=l[i];
        }
        else g[num++]=o[i];
    Graham(num,m);
    q[++m]=q[0];
    for(i=0;i<m;++i)
        need+=Dis(q[i],q[i+1]);
    if(len>=need)
    {
        if(cost<mcost||(cost==mcost&&n-num<ans))
        {
            mcost=cost;
            ans=n-num;
            state=s;
            ex=len-need;
        }
    }
}
int main()
{
    while(scanf("%d",&n),n)
    {
        if(cs)puts("");
        for(i=0;i<n;++i)
            scanf("%d%d%d%d",&o[i].x,&o[i].y,&v[i],&l[i]);
        mcost=1e9;
        for(i=0;i<(1<<n)-1;++i)solve(i);
        printf("Forest %d
Cut these trees: ",++cs); for(i=0;i<n;++i) if(state&(1<<i))printf("%d ",i+1); printf("
Extra wood: %.2lf
",ex); } return 0; }

좋은 웹페이지 즐겨찾기