Jacob_Thomas

Untitled

Oct 1st, 2019
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. Number Triples
  2. (IARCS OPC Archive, K Narayan Kumar, CMI)
  3.  
  4. In this problem you will be given a sequence of triples of positive integers. For example:
  5.  
  6. 1 2 5
  7. 5 2 6
  8. 1 3 8
  9. 8 1 11
  10. 1 1 6
  11. 10 1 6
  12. 11 3 6
  13. 10 4 8
  14. 10 1 11
  15. Given a pair of numbers A and B, a chain connecting A and B is a sequence of triples
  16.  
  17. A0 W0 A1, A1 W1 A2, A2 W2 A3, ... Ak-2 Wk-2 Ak-1, Ak-1 Wk-1 Ak
  18.  
  19. such that
  20.  
  21. A0 = A
  22. Ak = B
  23. For each i, 0 ≤ i < k, either the triple Ai Wi Ai+1 or the triple Ai+1 Wi Ai is in the given set of triples.
  24. The value of such a chain is the sum of the Wis in the chain. For example, here is a chain connecting 1 and 11 using the triples listed above:
  25.  
  26. 1 1 6, 6 3 11
  27. The value of this chain is 1+3 = 4.
  28.  
  29. Here is another chain connecting 1 and 11.
  30.  
  31. 1 1 6, 6 1 10, 10 1 11
  32. The value of this chain is 1+1+1 = 3. You can verify that among all chains connecting 1 and 11 this is the one with least value.
  33.  
  34. Sometimes there may be no chains connecting the given pair of numbers. For example, there is no chain connecting 1 and 2.
  35.  
  36. You will be given a sequence of triples and a pair of numbers. Your task is to find the value of the least value chain connecting the two numbers.
  37.  
  38. Solution hint
  39. Construct a graph from the triples and use Dijkstra's algorithm for single-source shortest paths.
  40.  
  41. Full solution
  42.  
  43. .
  44. Input format
  45. The first line of the input has three numbers M, A and B. M is the number of triples. The next M lines (lines 2,3,...,M+1) describe the triples. Line i+1 contains the three positive integers Xi, Yi and Zi that make up the ith triple. Your task is to find the value of the least value chain connecting A and B.
  46.  
  47. Output format
  48. If there is at least one chain connecting A and B the first line of the output must consist of a single word YES. In that case the second line must contain a single integer value indicating the value of the least valued chain connecting A and B. If there are no chains connecting A and B the output should contain a single line with the word NO on it.
  49.  
  50. Test Data:
  51. You may assume that 1 ≤ Xi, Yi, Zi ≤ 2000 and M ≤ 4000000.
  52.  
  53. Example:
  54. We illustrate the input and output format using the above example:
  55.  
  56. Sample Input 1:
  57. 9 1 11
  58. 1 2 5
  59. 5 2 6
  60. 1 3 8
  61. 8 1 11
  62. 1 1 6
  63. 10 1 6
  64. 11 3 6
  65. 10 4 8
  66. 10 1 11
  67. Sample Output 1:
  68. YES
  69. 3
  70. Sample Input 2:
  71. 9 1 2
  72. 1 2 5
  73. 5 2 6
  74. 1 3 8
  75. 8 1 11
  76. 1 1 6
  77. 10 1 6
  78. 11 3 6
  79. 10 4 8
  80. 10 1 11
  81. Sample Output 2:
  82. NO
Add Comment
Please, Sign In to add comment