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

这题输入格式好难处理,谁能进来看看我哪里处理错了造成TLE?

Posted by Acsaga at 2006-11-27 19:29:34 on Problem 1251
#include <stdio.h>
#include <stdlib.h>
#define E 80
#define V 30 
int e[E][3];
int d[V];
int cmp(const void* a, const void* b) {
	int* c = (int*)a;
	int* d = (int*)b; 
	return c[2] - d[2];
}

int kruskal(int n, int k) {
	int p=0;
	int max = 0;
	while(p < n-1) {
		int i;
		for(i=0; i<k; i++) {
			int t1 = e[i][0];
			while(d[t1] != 0)
				t1 = d[t1];
			int t2 = e[i][1];
			while(d[t2] != 0)
				t2 = d[t2];
			if(t1 != t2) {	
				p++;
				max += e[i][2];
				d[t1] = t2;
			}
		}	
	}
	return max;
}

int main() {
	while(1) {
		int n;
		char a,b;                  //吸收空格和回车
		scanf("%d", &n);
		if(n == 0)
			break;
		scanf("%c", &a);
		int i,top=0,j;
		for(i=0; i<n-1; i++) {
			d[i] = 0;
			int t;
			char c;
			scanf("%c%c%d", &c, &a, &t);
			for(j=0; j<t; j++) {
				char p;
				int q; 
				scanf("%c%c%c%d", &a, &p, &b, &q);
				e[top][0] = c-'A';
				e[top][1] = p-'A';
				e[top][2] = q;
				top++;
			}
			scanf("%c", &a);
		}
		d[n-1] = 0;
		qsort(e, top, sizeof(e[0]), cmp);
		printf("%d\n", kruskal(n, top));
	} 
	
	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