1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int arr[28][28]; 5 int judge[28]; 6 7 int main() 8 { 9 10 int n; 11 while(cin >> n&&n) 12 { 13 int i,j,pri,count; 14 char C; 15 memset(arr,0,sizeof(arr)); 16 memset(judge,0,sizeof(judge)); 17 for(i=0;i<n-1;i++) 18 { 19 cin >> C >> count; 20 for(j=0;j<count;j++) 21 { 22 cin >> C >> pri; 23 arr[i][C-‘A‘]=pri; 24 arr[C-‘A‘][i]=pri; 25 } 26 } 27 int temp[28]; 28 int sum=0,min=100000; 29 judge[0]=1; 30 temp[0]=0; 31 int k=0,h,s; 32 for(i=0,count=1;count!=n;) 33 { 34 min=100000; 35 for(h=0;h<=k;h++) 36 { 37 i=temp[h]; 38 for(j=0;j<n;j++) 39 { 40 if(arr[i][j]<min && arr[i][j]!=0 && judge[j]==0) 41 { 42 min=arr[i][j]; 43 s=j; 44 } 45 } 46 } 47 if(min!=100000) 48 { 49 sum+=min; 50 judge[s]=1; 51 count++; 52 temp[++k]=s; 53 } 54 } 55 cout << sum << endl; 56 } 57 return 0; 58 59 }
时间: 2024-10-19 20:52:23