Advertisement
regergr

Untitled

Dec 3rd, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 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 DoubleDijkstra
  9. {
  10. class Program
  11. {
  12. static int Min(int[] length, int N, bool[] used)
  13. {
  14. int min = int.MaxValue;
  15. int v = -1;
  16. for (int i = 1; i <= N; ++i)
  17. {
  18. if (length[i] <= min)
  19. {
  20.  
  21. if (used[i] == false)
  22. {
  23. min = length[i];
  24. v = i;
  25. }
  26. }
  27. }
  28. return v;
  29. }
  30.  
  31.  
  32. static void Main(string[] args)
  33. {
  34.  
  35. bool[] used;
  36. int[] length;
  37. int N;
  38. int S;
  39. int Edge;
  40. int[] SV;
  41. int[] EV;
  42. int[] weight;
  43.  
  44. using (StreamReader sr = new StreamReader("input.txt"))
  45. {
  46. string[] str = sr.ReadLine().Split(' ');
  47. N = int.Parse(str[0]);
  48. Edge = int.Parse(str[1]);
  49. S = int.Parse(str[2]);
  50. weight = new int[Edge + 1];
  51. EV = new int[Edge + 1];
  52. SV = new int[Edge + 1];
  53. length = new int[N + 1];
  54. used = new bool[N + 1];
  55. for (int i = 1; i <= Edge; ++i)
  56. {
  57. str = sr.ReadLine().Split(' ');
  58. SV[i] = int.Parse(str[0]);
  59. EV[i] = int.Parse(str[1]);
  60. weight[i] = int.Parse(str[2]);
  61. }
  62.  
  63. }
  64.  
  65. for (int i = 1; i <= N; ++i)
  66. {
  67. if (i == S)
  68. {
  69. length[i] = 0;
  70. }
  71. else
  72. {
  73. length[i] = int.MaxValue;
  74. }
  75. used[i] = false;
  76. }
  77. //int c = 0;
  78.  
  79.  
  80. for (int j = 1; j <= N; ++j)
  81. {
  82. int c = Min(length, N, used);
  83. for (int i = 1; i <= Edge; ++i)
  84.  
  85. {
  86. if (SV[i] == c)
  87. {
  88. int maybe = length[SV[i]] + weight[i];
  89. if (maybe >= 0)
  90. {
  91. if (maybe < length[EV[i]])
  92. {
  93. length[EV[i]] = maybe;
  94. }
  95.  
  96.  
  97. }
  98.  
  99. }
  100. }
  101. used[c] = true;
  102. }
  103. using (StreamWriter sr = new StreamWriter("output.txt"))
  104. {
  105. for (int i = 1; i <= N; ++i)
  106. {
  107. if (length[i] == int.MaxValue)
  108. {
  109. length[i] = -1;
  110. }
  111. sr.Write(length[i]);
  112. sr.Write(' ');
  113. }
  114. }
  115.  
  116.  
  117.  
  118.  
  119. }
  120.  
  121.  
  122.  
  123.  
  124. }
  125. }
  126.  
  127. /*using System;
  128. using System.Collections.Generic;
  129. using System.Linq;
  130. using System.Text;
  131. using System.Threading.Tasks;
  132. using System.IO;
  133.  
  134. namespace ConsoleApplication1
  135. {
  136. class Program
  137. {
  138. static void Main(string[] args)
  139. {
  140. int N;
  141. int M;
  142. int[] SV;
  143. int[] EV;
  144. int[] weigth;
  145. int Edge;
  146. int[,] d;
  147. using (StreamReader sr = new StreamReader("input.txt"))
  148. {
  149. string[] str = sr.ReadLine().Split(' ');
  150. N = int.Parse(str[0]);
  151. Edge = int.Parse(str[1]);
  152.  
  153. SV = new int[N+1];
  154. EV = new int[N + 1];
  155. weigth = new int[N + 1];
  156. d = new int[N+1,N+1];
  157. for (int i = 1; i <= Edge; ++i)
  158. {
  159. str = sr.ReadLine().Split(' ');
  160. SV[i] = int.Parse(str[0]);
  161. EV[i] = int.Parse(str[1]);
  162. weigth[i] = int.Parse(str[2]);
  163. }
  164.  
  165.  
  166. }
  167. for (int i = 1; i <= N; ++i)
  168. {
  169. for (int j = 1; j <= N; ++j)
  170. {
  171. d[i, j] = int.MaxValue;
  172. for (int s = 1; s <= Edge; ++s)
  173. {
  174. if (SV[s] == i)
  175. {
  176. d[SV[s], EV[s]] = weigth[s];
  177. }
  178. }
  179. }
  180. }
  181. for (int i = 1; i <= N; ++i)
  182. {
  183. for (int j = 1; j <= N; ++j)
  184. {
  185. using (StreamWriter sr = new StreamWriter("output.txt"))
  186. {
  187. sr.Write(d[i,j]);
  188. sr.Write(' ');
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement