Isti115

Truck delivery system

Oct 15th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Rextester
  6. {
  7.     public class Program
  8.     {
  9.         static List<Road> roads = new List<Road>();
  10.         static int maxDurability;
  11.         static string bestRoute;
  12.        
  13.         public static void Main(string[] args)
  14.         {
  15.             string[] splittedInput = Console.ReadLine().Split(' ');
  16.             int cityCount = Convert.ToInt32(splittedInput[0]);
  17.             int roadCount = Convert.ToInt32(splittedInput[1]);
  18.             int start = Convert.ToInt32(splittedInput[2]);
  19.             int end = Convert.ToInt32(splittedInput[3]);
  20.            
  21.             for(int i = 0;i < roadCount;i++)
  22.             {
  23.                 Road currentRoad = new Road(Console.ReadLine());
  24.                 roads.Add(currentRoad);
  25.             }
  26.            
  27.             maxDurability = 0;
  28.             bestRoute = "No route found!";
  29.            
  30.             int startMaxDurability = roads.Max(road => road.durability);
  31.            
  32.             findMax(startMaxDurability, new List<int>(){}, start, startMaxDurability, end, 1);
  33.            
  34.             Console.WriteLine(maxDurability + " ::: " + bestRoute);
  35.         }
  36.        
  37.         static void findMax(int currentMax, List<int> visitedCities, int cityNum, int currentDurability, int destination, int depth)
  38.         {
  39.             visitedCities.Add(cityNum);
  40.             currentMax = currentDurability < currentMax ? currentDurability : currentMax;
  41.            
  42.             //var cityNum = visitedCities[visitedCities.Count - 1];
  43.            
  44.             Console.WriteLine("-------------------");
  45.             Console.WriteLine("D: " + depth + "  C: " + cityNum + "  M: " + currentMax);
  46.            
  47.             if(cityNum == destination)
  48.             {                
  49.                 Console.WriteLine("Found route: " + maxDurability + " : " + string.Join(" ", visitedCities.ToArray()));
  50.                
  51.                 if(currentMax > maxDurability)
  52.                 {
  53.                     maxDurability = currentMax;
  54.                     bestRoute = string.Join(" ", visitedCities.ToArray());
  55.                 }
  56.                                  
  57.                 return;
  58.             }
  59.            
  60.             var roadQueryA = from road in roads where road.A == cityNum && !visitedCities.Contains(road.B) select road;
  61.            
  62.             foreach(Road currentRoad in roadQueryA)
  63.             {
  64.                             Console.WriteLine("A: " + string.Join(" ", visitedCities.ToArray()));
  65.                             //Console.WriteLine("A: " + currentRoad.A + " - " + currentRoad.B);
  66.                 //currentMax = currentRoad.durability < currentMax ? currentRoad.durability : currentMax;
  67.                 //visitedCities.Add(currentRoad.B);
  68.                 findMax(currentMax, new List<int>(visitedCities), currentRoad.B, currentRoad.durability, destination, depth + 1);
  69.             }
  70.            
  71.             var roadQueryB = from road in roads where road.B == cityNum && !visitedCities.Contains(road.A) select road;
  72.            
  73.             foreach(Road currentRoad in roadQueryB)
  74.             {
  75.                             Console.WriteLine("B: " + string.Join(" ", visitedCities.ToArray()));
  76.                             //Console.WriteLine("B: " + currentRoad.A + " - " + currentRoad.B);
  77.                 //currentMax = currentRoad.durability < currentMax ? currentRoad.durability : currentMax;
  78.                 //visitedCities.Add(currentRoad.A);
  79.                 findMax(currentMax, new List<int>(visitedCities), currentRoad.A, currentRoad.durability, destination, depth + 1);
  80.             }
  81.         }
  82.        
  83.         class Road
  84.         {
  85.             public int A, B;
  86.             public int durability;
  87.            
  88.             public Road(string roadProperties)
  89.             {
  90.                 string[] splittedProperties = roadProperties.Split(' ');
  91.                 A = Convert.ToInt32(splittedProperties[0]);
  92.                 B = Convert.ToInt32(splittedProperties[1]);
  93.                 durability = Convert.ToInt32(splittedProperties[2]);
  94.             }
  95.         }
  96.     }
  97. }
Add Comment
Please, Sign In to add comment