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 |
AC了的请进啊!please!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#include<stdio.h> #include<malloc.h> #include<stdlib.h> #include<string.h> struct node{ int vertex1; int vertex2; int weight; struct node *next; }; typedef struct node *Edge; Edge head=NULL; int visted[10001],m; void kruskal(Edge edge,int n){ int edgenum=0,edgesum=0; while(edgenum<n-1){ if(visted[edge->vertex1]==0||visted[edge->vertex2]==0){ edgesum+=edge->weight; edgenum++; visted[edge->vertex1]=1; visted[edge->vertex2]=1; } edge=edge->next; if(edge=NULL) break; } printf("%d\n",edgesum); } Edge read(){ int v1,v2,w; Edge newnode=NULL,pointer=NULL; for(int k=1;k<=m;k++){ scanf("%d%d%d",&v1,&v2,&w); newnode=(Edge)malloc(sizeof(struct node)); newnode->vertex1=v1; newnode->vertex2=v2; newnode->weight=w; newnode->next=NULL; pointer=head; if(pointer==NULL){ head=newnode; } else{ if(pointer->weight>newnode->weight){ newnode->next=pointer; head=newnode; } else{ while(pointer!=NULL&&pointer->next!=NULL){ if(newnode->weight>=pointer->weight&&newnode->weight<=pointer->next->weight){ newnode->next=pointer->next; pointer->next=newnode; break; } pointer=pointer->next; } pointer->next=newnode; } } } return head; } int main(){ int numcontry; while(scanf("%d",&numcontry)!=EOF){ if(numcontry==0) break; memset(visted,0,sizeof(visted)); scanf("%d",&m); head=NULL; Edge edge=NULL; edge=read(); kruskal(edge,numcontry); } } //当输入的numcontry>=3的时候,就无法运行了。不知道啥原因?哪个大哥大牛大姐大美女大叔大好人阿姨来帮我看看吧、谢谢。谢谢了。愁死我了。整半天了。 Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator