Advertisement
bullit3189

Travel Map

Jun 10th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _04TravelMap
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. Dictionary<string, Dictionary<string, int>> countryTownCost = new Dictionary<string, Dictionary<string, int>>();
  12.  
  13. while (true)
  14. {
  15. string command = Console.ReadLine();
  16.  
  17. if(command == "END")
  18. {
  19. break;
  20. }
  21.  
  22. string[] tokens = command.Split(" > ");
  23. string country = tokens[0];
  24. string town = tokens[1];
  25. int price = int.Parse(tokens[2]);
  26.  
  27. if(!countryTownCost.ContainsKey(country))
  28. {
  29. countryTownCost.Add(country, new Dictionary<string, int>());
  30. countryTownCost[country].Add(town, price);
  31. }
  32. else if (countryTownCost.ContainsKey(country)==true && countryTownCost[country].ContainsKey(town)==false)
  33. {
  34. countryTownCost[country].Add(town, price);
  35. }
  36. else if (countryTownCost.ContainsKey(country) == true && countryTownCost[country].ContainsKey(town) == true)
  37. {
  38. if(price< countryTownCost[country][town])
  39. {
  40. countryTownCost[country][town] = price;
  41. }
  42. }
  43. }
  44.  
  45. foreach (var kvp in countryTownCost.OrderBy(x=>x.Key))
  46. {
  47. string countryName = kvp.Key;
  48.  
  49. Console.Write(countryName + " -> ");
  50.  
  51. foreach (var inner in kvp.Value.OrderBy(x=>x.Value))
  52. {
  53. Console.Write($"{inner.Key} -> {inner.Value} ");
  54. }
  55. Console.WriteLine();
  56. }
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement