Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
runtime error问题,一直没解决,请大神帮忙看看。#include<iostream> #include<algorithm> #include<cmath> using namespace std; const double zero=0.000001; int stack[20]; double sum; int top; struct point{ double x,y; int vi,li; }; point p[20],temp[20]; double dis(point p1,point p2) { return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); } double mult(point p1,point p2,point p0) { return ((p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y)); } int cmp(const point &p1,const point &p2) { if(mult(p1,p2,p[0])>0)return 1; else if(fabs(mult(p1,p2,p[0]))<zero&&(dis(p1,p[0])<dis(p2,p[0])))return 1; else return 0; } void gransan(point p1[],int n1) { if(n1==1) { stack[0]=0; top=0; } if(n1==2) { stack[0]=0; stack[1]=1; top=1; } if(n1>2) { int low=0; for(int i=0;i<n1;i++) { if(p1[i].y<p1[low].y) low=i; else if((p1[i].y==p1[low].y)&&(p1[i].x<p1[low].x)) low=i; } point temp1; temp1=p1[low]; p1[low]=p1[0]; p1[0]=p1[low]; sort(p1+1,p1+n1,cmp); stack[0]=0; stack[1]=1; top=1; for(int i=2;i<n1;i++) { if((mult(p1[i],p1[stack[top]],p1[stack[top-1]])<0)||fabs(mult(p1[i],p1[stack[top]],p1[stack[top-1]]))<zero) { top++; stack[top]=i; } else{ do{ top--; }while(mult(p1[i],p1[stack[top]],p1[stack[top-1]])>0); top++; stack[top]=i; } } } } int main() { int n,u,e; int sumvi,sumli,num; int sign[20],sign1[20]; int minvi,min; double extra; int cases; cases=0; while(scanf("%d",&n)&&n) { minvi=9999999; cases++; for(int i=0;i<n;i++) scanf("%lf%lf%d%d",&p[i].x,&p[i].y,&p[i].vi,&p[i].li); for(int i=0;i<1<<n;i++) { u=i; sumvi=0; sumli=0; num=0; memset(sign,0,sizeof(sign)); for(int j=0;j<n;j++) { if(u&1) { sumvi+=p[j].vi; sumli+=p[j].li; sign[j]=1; } else { temp[num]=p[j]; num++; } u>>=1; } /* for(int i=0;i<n;i++) { if(sign[i]==1) { printf(" %d\n",i); } } printf("sumvi:%d minvi:%d\n",sumvi,minvi);*/ if(sumvi>minvi)continue; /* for(int k=0;k<num;k++) printf("凸包 x:%lf y:%lf\n",temp[k].x,temp[k].y); */ gransan(temp,num); /* printf("top %d\n",top); for(int k=0;k<=top;k++) printf("stack %d\n",stack[k]);*/ sum=0; for(int i=0;i<top;i++) { sum+=dis(temp[stack[i]],temp[stack[i+1]]); // printf("dis %lf\n",dis(temp[stack[i]],temp[stack[i+1]])); } sum+=dis(temp[stack[top]],temp[stack[0]]); // printf("dis %lf\n",dis(temp[stack[top]],temp[stack[0]])); if(sum<=sumli) { if((sumvi<minvi)||((sumvi==minvi)&&(n-num<min))) { extra=(double)sumli-sum; min=n-num; minvi=sumvi; e=0; for(int k=0;k<n;k++) { if(sign[k]==1) { sign1[e++]=k; // printf("k:%d\n",k+1); } } } } } printf("Forest %d\n",cases); printf("Cut these trees:"); for(int i=0;i<e;i++) { sign1[i]++; printf(" %d",sign1[i]); } printf("\n"); printf("Extra wood: %.2lf\n",extra); } system("pause"); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator