Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 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.  
  7. namespace ShortestPathDemo
  8. {
  9. class Vertex
  10. {
  11. public int Id { get; set; }
  12. public List<Vertex> Adj { get; set; }
  13. public Vertex Parent { get; set; }
  14. public bool Discovered { get; set; }
  15.  
  16. public Vertex(int id)
  17. {
  18. this.Id = id;
  19. this.Adj = new List<Vertex>();
  20. Discovered = false;
  21. Parent = null;
  22. }
  23.  
  24. public override string ToString()
  25. {
  26. string ps = Parent == null ? "-" : Parent.Id.ToString();
  27. string ds = Discovered ? "D" : "U";
  28. string s = string.Format("{0}:{1} P:{2}::", this.Id, ds, ps);
  29. foreach(Vertex nb in Adj)
  30. {
  31. s += string.Format(" {0}", nb.Id);
  32. }
  33. return s;
  34. }
  35. }
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42. #define LOCAL
  43. using System;
  44. using System.Collections.Generic;
  45. using System.Linq;
  46. using System.Text;
  47. using System.Threading.Tasks;
  48. using System.IO;
  49.  
  50. namespace ShortestPathDemo
  51. {
  52. class Program
  53. {
  54. static void Main(string[] args)
  55. {
  56. #if LOCAL
  57. TextReader stdin = Console.In;
  58. Console.SetIn(new StreamReader("graph.txt"));
  59. #endif
  60. int nV = int.Parse(Console.ReadLine());
  61. List<Vertex> vertices = new List<Vertex>();
  62. for(int v = 0; v < nV; v++)
  63. {
  64. vertices.Add(new Vertex(v));
  65. }
  66.  
  67. string line = Console.ReadLine();
  68. string[] parts = line.Split(' ');
  69. int nStart = int.Parse(parts[0]);
  70. int nFin = int.Parse(parts[1]);
  71.  
  72. line = Console.ReadLine();
  73. while (line != null)
  74. {
  75. parts = line.Split(' ');
  76. int n1 = int.Parse(parts[0]);
  77. int n2 = int.Parse(parts[1]);
  78. Vertex v1 = vertices[n1];
  79. Vertex v2 = vertices[n2];
  80. v1.Adj.Add(v2);
  81. line = Console.ReadLine();
  82. }
  83. Vertex start = vertices[nStart];
  84. start.Discovered = true;
  85. Queue<Vertex> q = new Queue<Vertex>();
  86. q.Enqueue(start);
  87. while (q.Count > 0)//while Vertices in Queue
  88. {
  89. Vertex vtx = q.Dequeue();
  90. foreach(Vertex nb in vtx.Adj)
  91. {
  92. if (!nb.Discovered)
  93. {
  94. nb.Discovered = true;
  95. nb.Parent = vtx;
  96. q.Enqueue(nb);
  97. }
  98. }
  99. }
  100. foreach (Vertex vtx in vertices) Console.WriteLine(vtx);
  101. Vertex tmp = vertices[nFin];
  102. Stack<int> stk = new Stack<int>();
  103. while (tmp.Parent != null)
  104. {
  105. stk.Push(tmp.Id);
  106. tmp = tmp.Parent;
  107. }
  108. Console.Write(nStart);
  109. while (stk.Count > 0) Console.Write(" " + stk.Pop());
  110. Console.WriteLine();
  111.  
  112. #if LOCAL
  113. Console.SetIn(stdin);
  114. Console.ReadLine();
  115. #endif
  116. }
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement