C - The Fortified Forest(凸包)

2023-11-25 22:45
文章标签 forest 凸包 fortified

本文主要是介绍C - The Fortified Forest(凸包),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

InputcopyOutputcopy
60  0  8  31  4  3  22  1  7  14  1  2  33  5  4  62  3  9  8
33  0 10  25  5 20 257 -3 30 32
0
Forest 1
Cut these trees: 2 4 5 
Extra wood: 3.16Forest 2
Cut these trees: 2 
Extra wood: 15.00

思路:

深搜,记录每次砍的树的数,价值, li和,还有没砍树的编号。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define per(i,a,b) for(int i=a;i<=b;i++)
#define ber(i,a,b) for(int i=a;i>=b;i--)
const int N = 1e4 + 1000;
const double eps = 1e-8;
double PI = (double)acos(-1 * 1.0);
struct Point
{
    double x;
    double y;
    double vau;
    double li;

}p[N],s[N],a[N],b[N];
int c[N],an[N];
int n,to;
double exw, cnum=1e9, cvau=1e9;
bool cmp(struct Point& a, struct Point& b)
{
    if (a.x != b.x)
        return a.x < b.x;
    return a.y < b.y;
}
double cross(struct Point& a, struct Point& b, struct Point& c)
{
    return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
double dis(struct Point& a, struct Point& b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double Andrew(int n)
{
    if (n <= 1) return 0;
    if (n == 2) return 2 * dis(p[1], p[2]);
    sort(p + 1, p + 1 + n, cmp);
    int top = 0;
    per(i, 1, n)
    {
        while (top > 1 && cross(s[top - 1], s[top], p[i]) <= 0) top--;
        s[++top] = p[i];
    }
    int t = top;
    ber(i, n - 1, 1)
    {
        while (top > t && cross(s[top - 1], s[top], p[i]) <= 0)top--;
        s[++top] = p[i];
    }
    double ans = 0;
    per(i, 1, top - 1)
        ans += dis(s[i], s[i + 1]);
    return ans;
}
void dfs(int no, int num, double v, double li)
{
    if (v > cvau || (v == cvau && num > cnum)) return;
    if (no == n + 1)
    {
           per(i, 1, to)
            p[i] = b[i];
        double lon = Andrew(to);
        if (li >= lon&&(v<=cvau||v==cvau&&num<=cnum))
        {
            cnum = num;
            cvau = v;
            exw = li - lon;
            for (int i = 1; i <= num; i++)
                an[i] = c[i];
        }
        return;
    }
    to++;
    b[to] = a[no];
    dfs(no + 1, num, v, li);
    to--;
    c[num + 1] = no;
    dfs(no + 1, num + 1, v + a[no].vau, li + a[no].li);
}
int main()
{
    int cn = 0;
    while (cin >> n)
    {
        if (n == 0)
            break;
        cn++;
        to = 0;
        cnum = 1e9;
        cvau = 1e9;
        per(i, 1, n) 
            cin >> a[i].x >> a[i].y >> a[i].vau >> a[i].li;
        dfs(1, 0, 0, 0);
        printf("Forest %d\n", cn);
        printf("Cut these trees:");
        per(i, 1, cnum)
            cout << " " << an[i];
        cout << endl;
        printf("Extra wood: %.2lf\n", exw);
        cout << endl;

    }
    return 0;
}

这篇关于C - The Fortified Forest(凸包)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/424746

相关文章

poj 1113 凸包+简单几何计算

题意: 给N个平面上的点,现在要在离点外L米处建城墙,使得城墙把所有点都包含进去且城墙的长度最短。 解析: 韬哥出的某次训练赛上A出的第一道计算几何,算是大水题吧。 用convexhull算法把凸包求出来,然后加加减减就A了。 计算见下图: 好久没玩画图了啊好开心。 代码: #include <iostream>#include <cstdio>#inclu

poj 2187 凸包or旋转qia壳法

题意: 给n(50000)个点,求这些点与点之间距离最大的距离。 解析: 先求凸包然后暴力。 或者旋转卡壳大法。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <s

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

HDU 1392 HDU 1348 凸包

求凸包的周长,  注意n=1 , 2时特殊情况 int cmp(double x){if(fabs(x) < 1e-8) return 0 ;if(x > 0) return 1 ;return -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}frien

【UVA】10652-Board Wrapping(凸包问题)

又增加了2个模板。 #include <iostream>#include <cstdlib>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <vector>#include <queue>#include <stack>#include <algorithm>usi

百度之星初赛1006(计算几何:能包含凸包的最小矩形面积)

矩形面积    Accepts: 717    Submissions: 1619  Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description 小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些

OpenCV结构分析与形状描述符(8)点集凸包计算函数convexHull()的使用

操作系统:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 编程语言:C++11 算法描述 查找一个点集的凸包。 函数 cv::convexHull 使用斯克拉斯基算法(Sklansky’s algorithm)来查找一个二维点集的凸包,在当前实现中该算法的时间复杂度为 O(N logN)。 函数 cv::convexHull 是

OpenCV结构分析与形状描述符(9)检测轮廓相对于其凸包的凹陷缺陷函数convexityDefects()的使用

操作系统:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 编程语言:C++11 算法描述 查找一个轮廓的凸性缺陷。 下图显示了一个手部轮廓的凸性缺陷: convexityDefects 是 OpenCV 库中的一个函数,用于检测轮廓相对于其凸包的凹陷缺陷。这个函数可以帮助识别轮廓中的凹进去的部分,通常被用来分析手部或其他物体的形状

分治算法与凸包问题

1. 什么是凸包问题? 凸包问题是计算几何中的经典问题。给定二维平面上的点集,凸包是一个最小的凸多边形,它包含了点集中所有的点。你可以把凸包想象成一根松紧带将所有点紧紧包裹住的样子,凸包的边缘仅沿着最外面的点延伸。 2. 分治法简介 分治算法是解决复杂问题的强大策略,它的思想是将问题分解为多个子问题,分别解决这些子问题后再合并得到最终解。凸包问题可以通过分治算法高效地解决,时间复杂度可以达到

多边形快速凸包算法(Melkman‘s Algorithm)

前言 平面点集的凸包算法一文介绍了如何计算平面点集或者任意多边形的凸包。对于随机的平面点集,Graham scan和Andraw's 单调链算法已经是最快的算法了。但是对于没有自相交的封闭的简单多边形,存在线性复杂度的算法。下面介绍这一优雅高效的算法。 一般的2D凸包算法,首先将点进行排序(时间复杂度),然后利用栈操作在O(n)的时间复杂度内计算凸包。初始的排序决定了最终的时间复杂度。但是本文