• Source
    1. // INOI1402
    2.  
    3. #include <iostream>
    4. //#include <limits>
    5.  
    6. using namespace std;
    7.  
    8. unsigned long computemaxcost(unsigned long arr[][231],unsigned long c, unsigned long int f);
    9. unsigned long selectminburntime(unsigned long [], unsigned long[], unsigned long);
    10.  
    11. int main()
    12. {
    13. unsigned long c,f;
    14. cin>>c>>f;
    15.  
    16. unsigned long arr[231][231];
    17.  
    18. for(unsigned long i = 1; i<= c; i++)
    19. for(unsigned long j = 1; j<=c; j++)
    20. arr[i][j] = 0;
    21.  
    22. unsigned long k,l;
    23.  
    24. for(unsigned long i = 1; i<=f; i++)
    25. {
    26. cin>>k>>l;
    27. cin>>arr[k][l];
    28. arr[l][k] = arr[k][l];
    29. }
    30.  
    31. unsigned long max = computemaxcost(arr,c,f);
    32.  
    33. cout<<max;
    34.  
    35. return 0;
    36. }
    37.  
    38. unsigned long computemaxcost(unsigned long arr[231][231], unsigned long int c,unsigned long f)
    39. {
    40. unsigned long expectedburntime[231];
    41. unsigned long burnt[231];
    42. unsigned long max = 0, k = 1;
    43.  
    44. while(k<=c){
    45. for(unsigned long i = 1; i<=c; i++)
    46. {
    47. burnt[i] = 0;
    48. expectedburntime[i] = 100002;
    49. }
    50.  
    51. expectedburntime[k] = 0;
    52.  
    53. unsigned long i = selectminburntime(burnt,expectedburntime,c);
    54.  
    55. while(i)
    56. {
    57. burnt[i] = 1;
    58. for(unsigned long j = 1; j<= c; j++)
    59. {
    60. if(arr[i][j] != 0 && burnt[j]!=1 && expectedburntime[i]+arr[i][j] < expectedburntime[j])
    61. {
    62. expectedburntime[j] = expectedburntime[i]+arr[i][j];
    63. }
    64. }
    65. i = selectminburntime(burnt,expectedburntime,c);
    66. }
    67.  
    68. for(unsigned long ip = 1; ip<=c; ip++)
    69. {
    70. if(expectedburntime[ip]>max)
    71. {
    72. max = expectedburntime[ip];
    73. }
    74. }
    75.  
    76. k++;
    77. }
    78.  
    79. return max;
    80. }
    81.  
    82. unsigned long selectminburntime(unsigned long burnt[], unsigned long expectedburntime[],unsigned long c)
    83. {
    84. unsigned long pos = 0;
    85. unsigned long minburn = 32767;
    86.  
    87. for(unsigned long i = 1; i<=c; i++)
    88. {
    89. if((!burnt[i]) && (expectedburntime[i] < minburn))
    90. {
    91. minburn = expectedburntime[i];
    92. pos = i;
    93. }
    94. }
    95.  
    96. return pos;
    97. }