[POJ 1873] The Fortified Forest【计算几何】

  • 2017-12-17
  • 0
  • 0

Problem:

Time Limit: 1000MS Memory Limit: 30000K

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

Solution:

本题思路较简单,但细节较复杂。

枚举砍掉的树木,对剩下的树木求凸包即可。要注意题目要求最小代价相同时,要取砍掉棵数最少的方案。

枚举时为保证字典序,使用dfs递归求解。可以加入一个最优性剪枝,即当目前代价及棵数不为最优时直接退出。但是退出前不要忘记回溯!

由于涉及的数组较多,其大小往往有不同,在for循环中确定上界时一定要仔细仔细再仔细!

代码中凸包由Graham Scan算法实现,每一步的目的基本都已标注在注释中。

Code: O(nlogn) [196K, 16MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define eps 1e-8
#define dmax 1e10
using namespace std;

int curT = 0, n;

inline int dsgn(double dvar){
	if(fabs(dvar) <= eps) return 0;
	return (dvar < 0) ? -1 : 1;
}

#define Vector Point
struct Point{
	double x, y, ang;
	
	Point() {}
	Point(double x, double y): x(x), y(y) {}
	
	inline Vector operator - (const Point p2) const {return Vector(x - p2.x, y - p2.y);}
};

inline double Cross(const Vector vec1, const Vector vec2) {return vec1.x * vec2.y - vec1.y * vec2.x;}

struct Tree{
	Point u;
	int v;
	double l;
} t[20];

bool iscut[20];
Point rem[20];  // The coordinate of the remaining trees
int toprem;

Point cv[20];  // Convex hull vertex
int topcv;

int minS[20], topS;
int minval, mincnt;
double extrawood;  // Final answers

#define sqr(x) ((x) * (x))
#define dis2(u, v) (sqr(u.x - v.x) + sqr(u.y - v.y))
inline bool polarAngleCmp(const Point p1, const Point p2){
	if(p1.ang < p2.ang) return true;
	if(dsgn(p1.ang - p2.ang) == 0 && dis2(p1, rem[0]) < dis2(p2, rem[0])) return true;
	return false;
}  // Sort by polar angle increasingly

inline void dfs(int stp, int id){
	iscut[id] = 1;
	int curval = 0, curcnt = 0;
	double totallen = 0;
	for(register int i = 1; i <= n; i++)
		if(iscut[i]) curval += t[i].v, curcnt++, totallen += t[i].l;
	if(curval > minval || (curval == minval && curcnt >= mincnt)){
		iscut[id] = 0;  // Backdate whenever the function ceases
		return;  // If this case costs more, give it up
	}
	toprem = 0;
	for(register int i = 1; i <= n; i++)
		if(!iscut[i]) rem[toprem++] = t[i].u;  // Get the remaining trees
	int poleId = 0;
	for(register int i = 1; i < toprem; i++)
		if(rem[i].y < rem[poleId].y || (dsgn(rem[i].y - rem[poleId].y) == 0 && rem[i].x < rem[poleId].x))
			poleId = i;  // Find the pole
	swap(rem[0], rem[poleId]);
	for(register int i = 1; i < toprem; i++) rem[i].ang = atan2(rem[i].y - rem[0].y, rem[i].x - rem[0].x);
	// Calculate the polar angle
	sort(rem + 1, rem + toprem, polarAngleCmp);
	topcv = 0;
	for(register int i = 0; i < toprem; i++){  // Beware of the upper range!!!
		while(topcv > 1 && Cross(rem[i] - cv[topcv - 2], cv[topcv - 1] - cv[topcv - 2]) >= 0) topcv--;
		cv[topcv++] = rem[i];
	}  // Calculate the convex hull using Graham Scan
	double cvperim = sqrt(dis2(cv[0], cv[topcv - 1]));
	for(register int i = 1; i < topcv; i++)  // Beware of the upper range!!!
		cvperim += sqrt(dis2(cv[i - 1], cv[i]));  // Calculate the perimeter of the convec hull
	if(cvperim <= totallen){
		minval = curval, mincnt = curcnt;
		topS = 0;
		for(register int i = 1; i <= n; i++)
			if(iscut[i]) minS[topS++] = i;
		extrawood = totallen - cvperim;
	}  // Update the answers
	for(register int i = id + 1; i <= n; i++) dfs(stp + 1, i);
	iscut[id] = 0;  // Backdate
}

int main(){
	while(scanf("%d", &n) && n){
		for(register int i = 1; i <= n; i++) scanf("%lf%lf%d%lf", &t[i].u.x, &t[i].u.y, &t[i].v, &t[i].l);
		minval = 0x3f3f3f3f, mincnt = 0x3f3f3f3f;
		for(register int i = 1; i <= n; i++) dfs(0, i);
		if(curT) puts("");
		printf("Forest %d\nCut these trees:", ++curT);
		for(register int i = 0; i < topS; i++) printf(" %d", minS[i]);
		printf("\nExtra wood: %.2f\n", extrawood);
	}
	return 0;
}

评论

还没有任何评论,你来说两句吧



常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
If the code cannot be displayed normally, please refresh the page.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai