public static IEnumerable <Tuple <int, int> > ReadArray(StreamReader textreader) { if (textreader == null) { yield break; } while (!textreader.EndOfStream) { var values = textreader.ReadLine().Split(' ', '\t'); yield return(Tuple.Create(LoadHelper.Parse(values[0]), LoadHelper.Parse(values[1]))); } }
public int CalculateKnapSackProblem(StreamReader textreader) { var counts = textreader.ReadLine().Split(' ', '\t'); var maxSize = LoadHelper.Parse(counts[0]); var numberOfItems = LoadHelper.Parse(counts[1]); _data = ReadArray(textreader).Take(numberOfItems).OrderByDescending(c => c.Item2).ToArray(); var sw = new Stopwatch(); sw.Start(); var result = Calculate(numberOfItems - 1, maxSize); sw.Stop(); Console.WriteLine(sw.Elapsed); return(result); }
public int CalculateMinimum(StreamReader textreader) { var counts = textreader.ReadLine().Split(' ', '\t').Select(c => LoadHelper.Parse(c)).ToArray(); var edges = ReadArray(textreader); var vertices = edges.GroupBy(c => c.Item1, c => Tuple.Create(c.Item2, c.Item3)) .ToDictionary(c => c.Key, c => c.ToArray()); var aprevious = new int[counts[0], counts[0]]; var anext = new int[counts[0], counts[0]]; FillAWithIntMax(aprevious, counts[0]); FillBaseCase(counts, vertices, aprevious); for (int k = 0; k < counts[0]; k++) { for (int i = 0; i < counts[0]; i++) { for (int j = 0; j < counts[0]; j++) { var i1 = aprevious[i, k]; var i2 = aprevious[k, j]; long val2 = (i1 + i2); if (val2 > int.MaxValue) { val2 = int.MaxValue; } var val1 = aprevious[i, j]; if (val1 > int.MaxValue) { val1 = int.MaxValue; } anext[i, j] = Math.Min(val1, (int)val2); } } var atemp = aprevious; aprevious = anext; anext = atemp; } CheckNegativeCycle(aprevious, counts[0]); var result = GetPairs(aprevious, counts[0]).ToDictionary(c => c.Key, c => c.Value).Min(c => c.Value); return(result); }