Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

很简单的算法,怎么会Runtime Error呢(c/c++)

Posted by solidco2 at 2006-10-06 13:07:30 on Problem 1741
本地测试基本数据都没问题啊,是溢出了吗,提交环境有能分配多大的数组阿

#include<stdio.h>
#include<malloc.h>

struct node{
	long length,power;	/*距离父节点长度,层值*/
	struct node *next;
}*Cur,*Tree,*tr1,*tr2;
long iNodeCount=0;
long sum,number,t1,t2,t3,n,i,j,gk;

int main(){
	while(scanf("%d%d",&n,&gk) && n){
		Tree=(struct node *)malloc(sizeof(struct node)*(n+2));
		(Tree+1)->power=1;
		(Tree+1)->length=0;
		(Tree+1)->next=NULL;
		for(i=1;i<n;i++){
			scanf("%ld%ld%ld",&t1,&t2,&t3);
			Cur=Tree+t2;
			Cur->next=Tree+t1;
			Cur->power=Cur->next->power+1;
			Cur->length=t3;
		}
		number=0;
		for(i=1;i<=n;i++){
			for(j=i+1;j<=n;j++){
				tr1=Tree+i;
				tr2=Tree+j;
				sum=0;
				{            /*先判断要查找的两个节点的层值*/
					while(tr1->power > tr2->power) {
						sum+=tr1->length;
						tr1=tr1->next;
					}
					while(tr1->power < tr2->power) {
						sum+=tr2->length;
						tr2=tr2->next;
					} 
					while(tr1!=tr2 && tr1!=NULL && tr2!=NULL){
						sum+=tr1->length+tr2->length;
						tr1=tr1->next;
						tr2=tr2->next;
					}
				}
				if(sum<=gk) number++;
			}
		}
		printf("%ld\n",number);
		free(Tree);
	}
	return 0;  
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator