uva11096(Nails)

求凸包周长。

RE,没找出哪儿出问题。。。


Problem C
Nails
Time Limit: 1 Second

Arash is tired of hard working, so he wants to surround some nails on the wall of his room by a rubber ribbon to make fun of it! Now, he wants to know what will be the final length of the rubber ribbon after surrounding the nails. You must assume that the radius of nails and rubber ribbon is negligible.

Input


The first line of input gives the number of cases, N. N test cases will follow. Each test case starts with a line containing two integers, the initial length of rubber ribbon and the number of nails0 respectively. Each of next n lines contains two integers denoting the location of a nail. There will be a blank line after each test-case.

Output


Your program mustoutput the final length of rubber ribbon precise to 5 decimal digits.


Sample Input
Output for Sample Input

2

2 4

0 0

0 1

1 0

1 1

5 4

0 0

0 1

1 0

1 1

4.00000
5.00000

 

粘一下小媛的code。
 
//正确的 code 凸包模板 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int MAX = 120000;
const double eps = 1e-6;
bool dy(double x,double y)	{	return x > y + eps;}	// x > y 
bool xy(double x,double y)	{	return x < y - eps;}	// x < y 
bool dyd(double x,double y)	{ 	return x > y - eps;}	// x >= y 
bool xyd(double x,double y)	{	return x < y + eps;} 	// x <= y 
bool dd(double x,double y) 	{	return fabs( x - y ) < eps;}  // x == y
struct point{	double x,y;		};
point c[MAX];
double disp2p(point a,point b) 
{
	return sqrt( ( a.x - b.x ) * ( a.x - b.x ) + ( a.y - b.y ) * ( a.y - b.y ) );
}
double crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向 
{
	return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
bool cmp(point a,point b)  // 排序   
{  
    double len = crossProduct(c[0],a,b);  
    if( dd(len,0.0) )  
        return xy(disp2p(c[0],a),disp2p(c[0],b));  
    return xy(len,0.0);  
}  
int stk[MAX];
int top;	
double sum = 0.0;
void Graham(int n)
{
    int tmp = 0;  
    for(int i=1; i= 1 )
			top--;
		stk[++top] = i;
	}
	//cout<<"top "<>t && t )
	{
		while(t--)
		{
		  cin>>len0>>n;
		  for(int i=0;i>c[i].x>>c[i].y;
          Graham(n);
        for(int i=0; i<=top; i++)
		sum += disp2p(c[stk[i]],c[stk[(i+1)%(top+1)]]);
          if(dyd(sum,len0)) cout<


 

 
自己的,RE
// 竟然会出来 RE ,真是扯。 
#include
#include  
#include  
using namespace std;

struct point 
{ 
    int x, y; 
}pp; 
point p[100005]; 
int stack[100005], top; 
double dis(point a, point b) 
{ 
    return ((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)+0.0); 
} 
int multi(point b, point c, point a) 
{ 
    return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x); 
} 
void swap(point p[], int s, int t) 
{ 
    point tmp; 
    tmp = p[s]; 
    p[s] = p[t]; 
    p[t] = tmp; 
} 
int cmp(const void *a, const void *b) 
{ 
    point *c = (point *)a; 
    point *d = (point *)b; 
    double k = multi(*c, *d, pp); 
    if(k < 0) return 1; 
    else if(k == 0 && dis(*c, pp) >= dis(*d, pp)) return 1; 
    else return -1; 
} 
void Graham(point p[], int n, int stack[], int &top) 
{ 
    int i, u; 
    u = 0; 
    for(i = 1;i < n;i++)
    {        
        if(p[i].y == p[u].y && p[i].x < p[u].x) u = i;  
        else if(p[i].y < p[u].y) u = i; 
    } 
    swap(p, 0, u); 
    pp = p[0]; 
    qsort(p + 1, n - 1, sizeof(p[0]), cmp); 
    stack[0] = 0; 
    stack[1] = 1; 
    top = 1; 
    for(i = 2;i < n;i++)
        { 
            while(multi(p[i], p[stack[top]], p[stack[top - 1]]) >= 0)
            { 
                if(top == 0) break; 
                top--; 
            } 
            top++; 
            stack[top] = i; 
       }   
} 
int main() 
{ 
    int ca, i, j, n; 
    double len,len0; 
    while(scanf("%d", &ca)&&ca)
 {
    while(ca--)
    { 
        scanf("%lf %d", &len0,&n); 
        for(j = 0;j < n;j++)
            scanf("%d%d", &p[j].x, &p[j].y);  
        Graham(p, n, stack, top); 
        p[stack[top+1]]=p[stack[0]];
        len=0.0;
        for(j = 0;j <= top ;j++)
         len +=dis(p[stack[j]], p[stack[j + 1]]); 
       if(len>len0) printf("%.5lf\n",len);
       else   printf("%.5lf\n",len0);
       printf("\n");
    } 
 }
 return 0; 
}


你可能感兴趣的:(计算几何-凸包)