Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace ConsoleApplication8
  9. {
  10. class Program
  11. {
  12. public static int[] distmin;
  13. public static int[] tata;
  14. static void Main(string[] args)
  15. {
  16. StreamReader sr = new StreamReader("c:\\users\\ivn\\documents\\visual studio 2015\\Projects\\ConsoleApplication8\\ConsoleApplication8\\in.txt");
  17. //la asta fisierul e asa : prima linie numarul de teste,apoi pe urmatoare linie numarul de planete, numarul de legaturi intre ele, si apoi cele doua sate in care vrei sa afli distanta
  18. // apoi ai cate doua planete si ce cost este intre ele
  19. string[] nr = sr.ReadLine().Split(' ');
  20. int nrteste = Int32.Parse(nr[0]);
  21. for(int teste =0;teste<nrteste;teste++)
  22. {
  23. nr = sr.ReadLine().Split(' ');
  24. int n = Int32.Parse(nr[0]);
  25. int m = Int32.Parse(nr[1]);
  26. int nodStart = Int32.Parse(nr[2])-1;
  27. int nodFinal = Int32.Parse(nr[3])-1;
  28. int[,] c = new int[n, n];
  29.  
  30. for(int i=0;i<n;i++)
  31. for (int j = 0; j < n; j++)
  32. {
  33.  
  34. c[i, j] = 99999;
  35. }
  36. for (int j = 0; j < m; j++)
  37. {
  38. nr = sr.ReadLine().Split(' ');
  39. int x = Int32.Parse(nr[0])-1;
  40. int y = Int32.Parse(nr[1])-1;
  41. int cost = Int32.Parse(nr[2]);
  42. c[x, y] = cost;
  43. c[y, x] = cost;
  44.  
  45.  
  46. }
  47.  
  48.  
  49.  
  50.  
  51.  
  52. int costmin;
  53. jack(c, nodStart, out distmin, out tata);
  54. AfiseazaDrum(nodStart, nodFinal, tata, c, out costmin);
  55. if (costmin == 199998)
  56. costmin = 2000;
  57. Console.Write(costmin);
  58. Console.WriteLine();
  59. }
  60. Console.Read();
  61. }
  62.  
  63.  
  64.  
  65. static void jack(int[,] c, int nod, out int[] distmin, out int[] tata)
  66. {
  67.  
  68. int infinit = 99999;
  69. int n = c.GetLength(0);
  70. int[] vizitat = new int[n];
  71. Array.Clear(vizitat, 0, vizitat.Length);
  72. distmin = new int[n];
  73. tata = new int[n];
  74.  
  75.  
  76. for (int i = 0; i < n; i++)
  77. {
  78. distmin[i] = c[nod, i];
  79. tata[i] = (c[nod, i] == 99999 ? nod : -1);
  80.  
  81. }
  82.  
  83. tata[nod] = -1;
  84. vizitat[nod] = 1;
  85.  
  86. for (int i = 0; i < n - 1; i++)
  87. {
  88. int min = int.MaxValue;
  89. int indexmin = -1;
  90. for (int j = 0; j < n; j++)
  91. {
  92. if (vizitat[j] == 0)
  93. {
  94. if (min > distmin[j])
  95. {
  96. min = distmin[j];
  97. indexmin = j;
  98. }
  99.  
  100. }
  101.  
  102.  
  103. }
  104. vizitat[indexmin] = 1;
  105.  
  106.  
  107. for (int j = 0; j < n; j++)
  108. {
  109. if (vizitat[j] == 0)
  110. {
  111. if (distmin[j] > distmin[indexmin] + c[indexmin, j])
  112. {
  113.  
  114. distmin[j] = distmin[indexmin] + c[indexmin, j];
  115. tata[j] = indexmin;
  116. }
  117.  
  118. }
  119. }
  120. }
  121. }
  122.  
  123.  
  124. static List<int> AfiseazaDrum(int nodStart, int nodFinal, int[] tata, int[,]c, out int costmin)
  125.  
  126. {
  127. List<int> drum = new List<int>();
  128. costmin = 0;
  129. int x = nodFinal;
  130. while(x!=-1)
  131. {
  132. drum.Add(x);
  133. x = tata[x];
  134. }
  135. drum.Reverse();
  136. foreach(int nod in drum)
  137. {
  138.  
  139. costmin = costmin + Convert.ToInt32(c[nodStart, nod] == 9999 ? nod : c[nodStart, nod]);
  140. nodStart = nod;
  141. }
  142. return drum;
  143.  
  144.  
  145. }
  146.  
  147.  
  148.  
  149.  
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement