UVa 811 - The Fortified Forest

探讨了如何通过状态压缩和计算几何等方法解决砍树造围栏的问题,旨在找到砍伐树木价值最小且数量最少的方案。文章提供了一段完整的C++代码实现。

题目:在二维平面上有n棵树,每棵树有自己的高度和价值。从里面砍掉一些,做成围栏(围栏的长度都与被砍掉树的高度和),问砍掉的最小价值是多少,如果价值相同,取砍掉树数目最少的。

分析:状态压缩、计算几何、凸包。如果搜索显然后超时(15!),利用2进制状态表示树砍和不砍的状态,例如:5(10)=101(2)表示1、3被砍掉,则一共有1<<15种状态,然后计算每种状态下未被砍掉的树的凸包以及被砍掉树的高和,比较即可。

注意:线段的周长等于二倍的线段长。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>

using namespace std;

//点结构 
typedef struct pnode
{
	int x,y,v,l,d;
}point;
point I[16];
point P[16];

//叉乘ab*ac 
int crossproduct( point a, point b, point c )
{
	return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}

//点到点距离 
int dist( point a, point b )
{
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

//坐标排序 
bool cmp1( point a, point b )
{
	return (a.x==b.x)?(a.y<b.y):(a.x<b.x);
}

//级角排序 
bool cmp2( point a, point b )
{
	double cp = crossproduct( P[0], a, b );
	if ( !cp ) return a.d < b.d;
	else return cp > 0;
}

//计算凸包 
double Graham( int n )
{
	sort( P+0, P+n, cmp1 );
	for ( int i = 1 ; i < n ; ++ i )
		P[i].d = dist( P[0], P[i] );
	sort( P+1, P+n, cmp2 );
	
	int top = n-1;
	if ( n > 2 ) {
		top = 1;
		for ( int i = 2 ; i < n ; ++ i ) {
			while ( top > 0 && crossproduct( P[top-1], P[top], P[i] ) <= 0 ) -- top;
			P[++ top] = P[i];
		}
	}
	P[++ top] = P[0];
	
	double L = 0.0;
	for ( int i = 0 ; i < top ; ++ i )
		L += sqrt(dist( P[i], P[i+1] )+0.0);
	return L;
}

int main()
{
	int n,t = 0;
	while ( scanf("%d",&n) && n ) {
		for ( int i = 0 ; i < n ; ++ i )
			scanf("%d%d%d%d",&I[i].x,&I[i].y,&I[i].v,&I[i].l);
		
		double MinV = 0x7ffffff,MinL = 0.0;
		int E = (1<<n)-1,S = 0,C = n;
		for ( int i = 0 ; i < E ; ++ i ) {
			int count = 0,cut = 0;
			int SumL = 0,SumV = 0;
			for ( int j = 0 ; j < n ; ++ j )
				if ( i&(1<<j) ) {
					SumL += I[j].l;
					SumV += I[j].v;
					cut ++;
				}else P[count ++] = I[j];
			
			double V = Graham( count ); 
			if ( V<=SumL && (SumV<MinV || (SumV==MinV&&cut<C)) ) {
				MinV = SumV;
				MinL = SumL-V;
				C = cut;
				S = i;
			}
		}
		
		if ( t ++ ) printf("\n");
		printf("Forest %d\nCut these trees:",t);
		for ( int i = 0 ; i < n ; ++ i )
			if ( S&(1<<i) ) printf(" %d",i+1);
		printf("\nExtra wood: %.2lf\n",MinL);
		
	}
	return 0;
}

gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/gemm.c -o obj/gemm.o gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/utils.c -o obj/utils.o In file included from /usr/include/string.h:495, from ./src/utils.c:3: In function 'strncpy', inlined from 'copy_string' at ./src/utils.c:426:5: /usr/include/x86_64-linux-gnu/bits/string_fortified.h:106:10: warning: '__builtin_strncpy' specified bound depends on the length of the source argument [-Wstringop-overflow=] 106 | return __builtin___strncpy_chk (__dest, __src, __len, __bos (__dest)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./src/utils.c: In function 'copy_string': ./src/utils.c:426:22: note: length computed here 426 | strncpy(copy, s, strlen(s)+1); | ^~~~~~~~~ gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/cuda.c -o obj/cuda.o gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/deconvolutional_layer.c -o obj/deconvolutional_layer.o gcc -Iinclude/ -Isrc/ -DCUDNN -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -Ofast -DCUDNN -c ./src/convolutional_layer.c -o obj/convolutional_layer.o ./src/convolutional_layer.c: In function 'get_workspace_size': ./src/convolutional_layer.c:91:9: warning: implicit declaration of function 'cudnnGetConvolutionForwardWorkspaceSize' [-Wimplicit-function-declaration] 91 | cudnnGetConvolutionForwardWorkspaceSize(cudnn_handle(), | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ./src/convolutional_layer.c:91:49: warning: implicit declaration of function 'cudnn_handle' [-Wimplicit-function-declaration] 91 | 这个错误应该如何解决,请提供参考网址
最新发布
04-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值