HDU 1115 Lifting the Stone(多邊形重心)

來源:互聯網
上載者:User

Lifting the Stone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4373    Accepted Submission(s): 1803


Problem DescriptionThere are many secret openings in the floor which are covered by a big heavy stone. When the stone is lifted up, a special mechanism detects this and activates poisoned arrows that are shot near the opening. The only possibility is to lift the stone very slowly
and carefully. The ACM team must connect a rope to the stone and then lift it using a pulley. Moreover, the stone must be lifted all at once; no side can rise before another. So it is very important to find the centre of gravity and connect the rope exactly
to that point. The stone has a polygonal shape and its height is the same throughout the whole polygonal area. Your task is to find the centre of gravity for the given polygon.  


InputThe input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing a single integer N (3 <= N <= 1000000) indicating the number of points that form the polygon. This is followed
by N lines, each containing two integers Xi and Yi (|Xi|, |Yi| <= 20000). These numbers are the coordinates of the i-th point. When we connect the points in the given order, we get a polygon. You may assume that the edges never touch each other (except the
neighboring ones) and that they never cross. The area of the polygon is never zero, i.e. it cannot collapse into a single line.  


OutputPrint exactly one line for each test case. The line should contain exactly two numbers separated by one space. These numbers are the coordinates of the centre of gravity. Round the coordinates to the nearest number with exactly two digits after the decimal
point (0.005 rounds up to 0.01). Note that the centre of gravity may be outside the polygon, if its shape is not convex. If there is such a case in the input data, print the centre anyway.  


Sample Input

245 00 5-5 00 -541 111 111 111 11
 


Sample Output

0.00 0.006.00 6.00
 


SourceCentral Europe 1999 


RecommendEddy 

這個題目浙大的模板好像不對,可能考慮的問題多了吧!

#include <iostream>#include <string.h>#include <stdio.h>#include <cmath>#include <algorithm>using namespace std;#define eps 1e-8struct point{    double x;    double y;}po[10000];struct line{    point a;    point b;};double xmult(point p0, point p1, point p2){   return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}point intersection(line u,line v){point ret=u.a;double t=((u.a.x-v.a.x)*(v.a.y-v.b.y) - (u.a.y-v.a.y)*(v.a.x-v.b.x))/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));ret.x+=(u.b.x-u.a.x)*t;ret.y+=(u.b.y-u.a.y)*t;return ret;}point barycenter(point a,point b,point c){    line u,v;    u.a.x=(a.x+b.x)/2;    u.a.y=(a.y+b.y)/2;    u.b=c;    v.a.x=(a.x+c.x)/2;    v.a.y=(a.y+c.y)/2;    v.b=b;    return intersection(u,v);}point barycenter(int n,point *p){    point ret,t;    double t1=0,t2;    int i;    ret.x=ret.y=0;    for(i=1;i<n-1;i++)    if(fabs(t2=xmult(p[0],p[i],p[i+1])) > eps)    {        t=barycenter(p[0],p[i],p[i+1]);        ret.x+=t.x*t2;        ret.y+=t.y*t2;        t1+=t2;    }    if(fabs(t1) > eps)    ret.x/=t1,ret.y/=t1;    return ret;}point gravity(point *p, int n){double area = 0;point center;center.x = 0;center.y = 0;for (int i = 0; i < n-1; i++){   area += (p[i].x*p[i+1].y - p[i+1].x*p[i].y)/2;   center.x += (p[i].x*p[i+1].y - p[i+1].x*p[i].y) * (p[i].x + p[i+1].x);   center.y += (p[i].x*p[i+1].y - p[i+1].x*p[i].y) * (p[i].y + p[i+1].y);}area += (p[n-1].x*p[0].y - p[0].x*p[n-1].y)/2;center.x += (p[n-1].x*p[0].y - p[0].x*p[n-1].y) * (p[n-1].x + p[0].x);center.y += (p[n-1].x*p[0].y - p[0].x*p[n-1].y) * (p[n-1].y + p[0].y);center.x /= 6*area;center.y /= 6*area;return center;}int main(){    int t,n,i,j,k;    point temp;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        if(n==0) {printf("0.00 0.00\n");continue;}        for(i=0;i<n;i++)        scanf("%lf%lf",&po[i].x,&po[i].y);        if(n==1)        { printf("%.2lf %.2lf\n",po[0].x,po[0].y); continue;}        if(n==2)        { printf("%.2lf %.2lf\n",(po[0].x+po[1].x)/2,(po[0].y+po[1].y)/2); continue;}        temp=gravity(po,n);        printf("%.2lf %.2lf\n",temp.x,temp.y);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.