• Source
    1. #include<bits/stdc++.h>
    2.  
    3. using namespace std;
    4.  
    5. bool mat[900][900];
    6. int par[100];
    7.  
    8. struct data
    9. {
    10. int a,b,cost;
    11. } arr[900];
    12.  
    13. int khoj_rep(int r)
    14. {
    15. if(par[r]==r)
    16. {
    17. return r;
    18. }
    19. else
    20. {
    21. return par[r]=khoj_rep(par[r]);
    22. }
    23. }
    24.  
    25. bool cmp(data lhs,data rhs)
    26. {
    27. if(lhs.cost==rhs.cost)
    28. {
    29. return lhs.a<rhs.a;
    30. }
    31. else
    32. {
    33. return lhs.cost<rhs.cost;
    34. }
    35. }
    36.  
    37.  
    38. int main()
    39. {
    40. int test,koyta_city,i,j,k,x,cost,u,v,counter;
    41. cin>>test;
    42. for(x=1; x<=test; x++)
    43. {
    44. cin>>koyta_city;
    45.  
    46. for(i=65; i<=koyta_city+64; i++)
    47. {
    48. par[i]=i;
    49. }
    50.  
    51. k=-1;
    52. for(i=1; i<=koyta_city; i++)
    53. {
    54. for(j=1; j<=koyta_city; j++)
    55. {
    56. if(j>=1 && j<koyta_city)
    57. {
    58. scanf("%d, ",&cost);
    59. }
    60. else if(j==koyta_city)
    61. {
    62. scanf("%d",&cost);
    63. }
    64. if(cost>0 && mat[i][j]==0)
    65. {
    66. arr[++k].a=i+64;
    67. arr[k].b=j+64;
    68. arr[k].cost=cost;
    69. mat[j][i]=1;
    70. }
    71.  
    72. }
    73. }
    74.  
    75. sort(arr,arr+k+1,cmp);
    76. counter=0;
    77.  
    78. printf("Case %d:\n",x);
    79.  
    80. for(i=0; i<k; i++)
    81. {
    82. u=khoj_rep(arr[i].a);
    83. v=khoj_rep(arr[i].b);
    84. if(u!=v)
    85. {
    86. par[u]=v;
    87. printf("%c-%c %d\n",arr[i].a,arr[i].b,arr[i].cost);
    88. counter++;
    89. if(counter==koyta_city-1)
    90. {
    91. break;
    92. }
    93. }
    94. }
    95. memset(par,0,sizeof(par));
    96. memset(mat,0,sizeof(mat));
    97. }
    98. return 0;
    99. }
    100.