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

我就用scanf和printf啊,照样AC,应该是自己写的code的问题吧

Posted by atopoxo at 2011-06-03 09:26:32 on Problem 1251
我就用scanf和printf啊,照样AC,应该是自己写的code的问题吧
code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define len 30
const int INF=1000000000;
int maze[len][len];
int v;
bool init(){
	int r,k,i,x,y;
	char s[2],ss[2];
	memset(maze,0,sizeof(maze));
	if(scanf("%d",&v)==1&&v){
		for(i=1;i<v;i++){
			scanf("%s%d",s,&r);
			while(r--){
				scanf("%s%d",ss,&k);
				x=s[0]-'A';    y=ss[0]-'A';
				maze[x][y]=maze[y][x]=k;
			}
		}
		return true;
	}
	else     return false;
}
int prim(int mat[][len]){
	int i,j,k,ret=0,min[len];
	bool V[len];
	for(i=0;i<v;i++)
		min[i]=INF,V[i]=false;
	for(min[j=0]=0;j<v;j++){
		for(k=-1,i=0;i<v;i++)
			if(!V[i]&&(k==-1||min[i]<min[k]))
				k=i;
		for(V[k]=true,ret+=min[k],i=0;i<v;i++)
			if(!V[i]&&mat[k][i]<min[i]&&mat[k][i]!=0)
				min[i]=mat[k][i];
	}
	return ret;
}
int main(void){
	int ans;
	while(init()){
		ans=prim(maze);
		printf("%d\n",ans);
	}
	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