Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. class Solution
  2. {
  3. static Dictionary<int, List<Edge>> dict;
  4. static int[] visited;
  5. static int[] cost;
  6. static string answer;
  7. static void Main(string[] args)
  8. {
  9. answer = "";
  10. int t = Convert.ToInt32(Console.ReadLine());
  11. for (int a0 = 0; a0 < t; a0++)
  12. {
  13. dict = new Dictionary<int, List<Edge>>();
  14. string[] tokens_n = Console.ReadLine().Split(' ');
  15. int n = Convert.ToInt32(tokens_n[0]);
  16. int m = Convert.ToInt32(tokens_n[1]);
  17. visited = Enumerable.Repeat(-1, n).ToArray();
  18. cost = Enumerable.Repeat(int.MaxValue, n).ToArray();
  19. for (int a1 = 0; a1 < m; a1++)
  20. {
  21. string[] tokens_x = Console.ReadLine().Split(' ');
  22. int x = Convert.ToInt32(tokens_x[0]);
  23. int y = Convert.ToInt32(tokens_x[1]);
  24. int r = Convert.ToInt32(tokens_x[2]);
  25. if (y < x)
  26. {
  27. int temp = x;
  28. x = y;
  29. y = temp;
  30. }
  31.  
  32. if (dict.ContainsKey(x))
  33. {
  34. List<Edge> list = new List<Edge>();
  35. dict.TryGetValue(x, out list);
  36. Edge e = (from l in list where l.Dest == y select l).SingleOrDefault();
  37. if (e == null)
  38. {
  39. list.Add(new Edge(y, r));
  40. }
  41. else if (e.Cost > r)
  42. {
  43. e.Cost = r;
  44. }
  45. }
  46. else
  47. {
  48. List<Edge> list = new List<Edge>();
  49. list.Add(new Edge(y, r));
  50. dict.Add(x, list);
  51. }
  52. if (dict.ContainsKey(y))
  53. {
  54. List<Edge> list = new List<Edge>();
  55. dict.TryGetValue(y, out list);
  56. Edge e = (from l in list where l.Dest == x select l).SingleOrDefault();
  57. if (e == null)
  58. {
  59. list.Add(new Edge(x, r));
  60. }
  61. else if (e.Cost > r)
  62. {
  63. e.Cost = r;
  64. }
  65. }
  66. else
  67. {
  68. List<Edge> list = new List<Edge>();
  69. list.Add(new Edge(x, r));
  70. dict.Add(y, list);
  71. }
  72. }
  73. int s = Convert.ToInt32(Console.ReadLine());
  74. Dijkstra(s);
  75. foreach (int i in cost)
  76. {
  77. if (i == 0)
  78. {
  79. continue;
  80. }
  81. if (i == int.MaxValue)
  82. {
  83. answer += -1 + " ";
  84. }
  85. else
  86. {
  87. answer += i + " ";
  88. }
  89. }
  90. answer += "n";
  91. }
  92. Console.WriteLine(answer);
  93. }
  94.  
  95. static void Dijkstra(int source)
  96. {
  97. cost[source - 1] = 0;
  98. visited[source - 1] = 1;
  99. Queue<int> queue = new Queue<int>();
  100. queue.Enqueue(source);
  101. while (queue.Count != 0)
  102. {
  103. int curr_source = queue.Dequeue();
  104. if (dict.ContainsKey(curr_source))
  105. {
  106. List<Edge> list = new List<Edge>();
  107. dict.TryGetValue(curr_source, out list);
  108. foreach (Edge e in list)
  109. {
  110. int alternate_cost = e.Cost + cost[curr_source - 1];
  111. if (alternate_cost < cost[e.Dest - 1])
  112. {
  113. cost[e.Dest - 1] = alternate_cost;
  114. }
  115. if (visited[e.Dest - 1] == -1)
  116. {
  117. visited[e.Dest - 1] = 1;
  118. queue.Enqueue(e.Dest);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125.  
  126. class Edge
  127. {
  128. public Edge(int dest, int cost)
  129. {
  130. Dest = dest;
  131. Cost = cost;
  132. }
  133.  
  134. public int Dest { get; set; }
  135. public int Cost { get; set; }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement