static Ostov Algoritm(List <List <int> > matrix, List <Top> tops) { var near = new Dictionary <Top, Top>(); var w = new List <Top>(tops); var distance = new Dictionary <Top, int>(); var top = tops[0]; w.Remove(top); var ostov = new Ostov(Enumerable.Range(0, tops.Count).Select(x => new LinkedList <int>()).ToList(), 0); for (int i = 0; i < tops.Count; i++) { near.Add(tops[i], top); distance.Add(tops[i], matrix[i][0]); } while (w.Count != 0) { var v = distance.Where(x => x.Value != 0 && w.Contains(x.Key)).OrderBy(x => x.Value).First(); var u = near[v.Key]; ostov.Graph[v.Key.Number].AddLast(u.Number + 1); ostov.Graph[u.Number].AddLast(v.Key.Number + 1); ostov.Weight += v.Value; w.Remove(v.Key); foreach (var versh in w) { if (distance[versh] > matrix[versh.Number][v.Key.Number]) { near[versh] = v.Key; distance[versh] = matrix[versh.Number][v.Key.Number]; } } } return(ostov); }
static Ostov Algoritm(List<List<int>> matrix, List<Top> tops) { var near = new Dictionary<Top, Top>(); var w = new List<Top>(tops); var distance = new Dictionary<Top, int>(); var top = tops[0]; w.Remove(top); var ostov = new Ostov(Enumerable.Range(0, tops.Count).Select(x => new LinkedList<int>()).ToList(), 0); for (int i = 0; i < tops.Count; i++) { near.Add(tops[i], top); distance.Add(tops[i], matrix[i][0]); } while (w.Count != 0) { var v = distance.Where(x => x.Value != 0 && w.Contains(x.Key)).OrderBy(x => x.Value).First(); var u = near[v.Key]; ostov.Graph[v.Key.Number].AddLast(u.Number + 1); ostov.Graph[u.Number].AddLast(v.Key.Number + 1); ostov.Weight += v.Value; w.Remove(v.Key); foreach (var versh in w) { if (distance[versh] > matrix[versh.Number][v.Key.Number]) { near[versh] = v.Key; distance[versh] = matrix[versh.Number][v.Key.Number]; } } } return ostov; }
static void WriteOutput(Ostov ostov) { using (var writer = new StreamWriter("out.txt")) { for (int i = 0; i < ostov.Graph.Count; i++) { for (int j = 0; j < ostov.Graph[i].Count; j++) { writer.Write(ostov.Graph[i].ElementAt(j)); writer.Write(' '); } writer.WriteLine(); } writer.Write(ostov.Weight); } }
static void WriteOutput(Ostov ostov) { using (var writer = new StreamWriter("out.txt")) { for (int i=0;i<ostov.Graph.Count;i++) { for (int j = 0; j < ostov.Graph[i].Count; j++) { writer.Write(ostov.Graph[i].ElementAt(j)); writer.Write(' '); } writer.WriteLine(); } writer.Write(ostov.Weight); } }