Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Rextester
- {
- public class Program
- {
- static List<Road> roads = new List<Road>();
- static int maxDurability;
- static string bestRoute;
- public static void Main(string[] args)
- {
- string[] splittedInput = Console.ReadLine().Split(' ');
- int cityCount = Convert.ToInt32(splittedInput[0]);
- int roadCount = Convert.ToInt32(splittedInput[1]);
- int start = Convert.ToInt32(splittedInput[2]);
- int end = Convert.ToInt32(splittedInput[3]);
- for(int i = 0;i < roadCount;i++)
- {
- Road currentRoad = new Road(Console.ReadLine());
- roads.Add(currentRoad);
- }
- maxDurability = 0;
- bestRoute = "No route found!";
- int startMaxDurability = roads.Max(road => road.durability);
- findMax(startMaxDurability, new List<int>(){}, start, startMaxDurability, end, 1);
- Console.WriteLine(maxDurability + " ::: " + bestRoute);
- }
- static void findMax(int currentMax, List<int> visitedCities, int cityNum, int currentDurability, int destination, int depth)
- {
- visitedCities.Add(cityNum);
- currentMax = currentDurability < currentMax ? currentDurability : currentMax;
- //var cityNum = visitedCities[visitedCities.Count - 1];
- Console.WriteLine("-------------------");
- Console.WriteLine("D: " + depth + " C: " + cityNum + " M: " + currentMax);
- if(cityNum == destination)
- {
- Console.WriteLine("Found route: " + maxDurability + " : " + string.Join(" ", visitedCities.ToArray()));
- if(currentMax > maxDurability)
- {
- maxDurability = currentMax;
- bestRoute = string.Join(" ", visitedCities.ToArray());
- }
- return;
- }
- var roadQueryA = from road in roads where road.A == cityNum && !visitedCities.Contains(road.B) select road;
- foreach(Road currentRoad in roadQueryA)
- {
- Console.WriteLine("A: " + string.Join(" ", visitedCities.ToArray()));
- //Console.WriteLine("A: " + currentRoad.A + " - " + currentRoad.B);
- //currentMax = currentRoad.durability < currentMax ? currentRoad.durability : currentMax;
- //visitedCities.Add(currentRoad.B);
- findMax(currentMax, new List<int>(visitedCities), currentRoad.B, currentRoad.durability, destination, depth + 1);
- }
- var roadQueryB = from road in roads where road.B == cityNum && !visitedCities.Contains(road.A) select road;
- foreach(Road currentRoad in roadQueryB)
- {
- Console.WriteLine("B: " + string.Join(" ", visitedCities.ToArray()));
- //Console.WriteLine("B: " + currentRoad.A + " - " + currentRoad.B);
- //currentMax = currentRoad.durability < currentMax ? currentRoad.durability : currentMax;
- //visitedCities.Add(currentRoad.A);
- findMax(currentMax, new List<int>(visitedCities), currentRoad.A, currentRoad.durability, destination, depth + 1);
- }
- }
- class Road
- {
- public int A, B;
- public int durability;
- public Road(string roadProperties)
- {
- string[] splittedProperties = roadProperties.Split(' ');
- A = Convert.ToInt32(splittedProperties[0]);
- B = Convert.ToInt32(splittedProperties[1]);
- durability = Convert.ToInt32(splittedProperties[2]);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment