fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void Swap(long *a, long *b)
  5. {
  6. long temp = *a;
  7. *a = *b;
  8. *b = temp;
  9. }
  10.  
  11. long Partition(long *arr, long start, long end, long *u, long *v)
  12. {
  13. long pivot = arr[end];
  14. long pIndex = start;
  15. long i;
  16. for(i = start; i<end; i++) {
  17. if(arr[i] <= pivot) {
  18. Swap(arr+i, arr+pIndex);
  19. Swap(u+i, u+pIndex);
  20. Swap(v+i, v+pIndex);
  21. pIndex++;
  22. }
  23. }
  24. Swap(arr+pIndex, arr+end);
  25. Swap(u+pIndex, u+end);
  26. Swap(v+pIndex, v+end);
  27. return pIndex;
  28. }
  29.  
  30. void MSort(long *u, long *v, long *arr, long start, long end)
  31. {
  32. if(start < end) {
  33. long pIndex = Partition(arr, start, end, u, v);
  34. MSort(u, v, arr, start, pIndex-1);
  35. MSort(u, v, arr, pIndex+1, end);
  36. }
  37. }
  38.  
  39. int main()
  40. {
  41. long n, m, k;
  42. cin >> n >> m >> k;
  43. long i;
  44. long u[m], v[m], l[m];
  45. for(i = 0; i < m; i++) {
  46. cin >> u[i] >> v[i] >> l[i];
  47. }
  48. if(k == 0 || k == n) {
  49. cout << "-1";
  50. }
  51. else {
  52. long arr[k];
  53. long u1[m] = {0}, v1[m] = {0}, l1[m] = {-1};
  54. for(i = 0; i < k; i++)
  55. cin >> arr[i];
  56. long x = 0, j;
  57. for(j = 0; j < k; j++) {
  58. for(i = 0; i < m; i++) {
  59. if(arr[j] == u[i]) {
  60. u1[x] = u[i];
  61. v1[x] = v[i];
  62. l1[x] = l[i];
  63. x++;
  64. } else if(arr[j] == v[i]) {
  65. u1[x] = v[i];
  66. v1[x] = u[i];
  67. l1[x] = l[i];
  68. x++;
  69. }
  70. }
  71. }
  72. MSort(u1, v1, l1, 0, x-1);
  73.  
  74. long flag = 1;
  75. for(i = 0; i < x; i++) {
  76. flag = 0;
  77. for(j = 0; j < k; j++) {
  78. //cout << v1[i] << " " << arr[j] << " " << endl;
  79. if(v1[i] == arr[j]) {
  80. flag = 1;
  81. break;
  82. }
  83. }
  84. if(flag == 0) {
  85. //cout << i << " ";
  86. cout << l1[i];
  87. break;
  88. }
  89. }
  90. if(flag == 1)
  91. cout << -1;
  92. }
  93. return 0;
  94. }
Runtime error #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Standard output is empty