Advertisement
Lusien_Lashans

Lab3 minmax

Oct 23rd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace Lab3
  9. {
  10.     public class Node
  11.     {
  12.         public Dictionary<int, int> edges = new Dictionary<int, int>();
  13.     }
  14.  
  15.     public static class DictionaryExtensions
  16.     {
  17.         public static void AddOrChange(this Dictionary<int, int> dic, int key, int value)
  18.         {
  19.             if (dic.ContainsKey(key))
  20.                 dic[key] = value;
  21.             else
  22.                 dic.Add(key, value);
  23.         }
  24.     }
  25.  
  26.     class Program
  27.     {
  28.         static void Main(string[] args)
  29.         {
  30.             var text = File.ReadAllText("in.txt").Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  31.             File.Delete("out.txt");
  32.  
  33.             int it = 1;
  34.             int n = int.Parse(text[0]);
  35.             var nodes = new List<Node>();
  36.  
  37.             for (int y = 0; y < n; y++)
  38.             {
  39.                 nodes.Add(new Node());
  40.                 for (int x = 0; x < n; x++)
  41.                 {
  42.                     it++;
  43.  
  44.                     if (int.Parse(text[it - 1]) < 0)
  45.                         continue;
  46.  
  47.                     nodes[y].edges.Add(x, int.Parse(text[it - 1]));
  48.                 }
  49.             }
  50.  
  51.             int startNode = int.Parse(text[it]) - 1;
  52.             int endNode = int.Parse(text[it + 1]) - 1;
  53.  
  54.             /*********************/
  55.  
  56.             var weight = new Dictionary<Node, int>();
  57.             var pathNodes = new Dictionary<int, int>();
  58.             var visited = new Dictionary<Node, bool>();
  59.  
  60.             foreach (var v in nodes)
  61.             {
  62.                 weight.Add(v, int.MaxValue);
  63.                 visited.Add(v, false);
  64.             }
  65.  
  66.             weight[nodes[startNode]] = 0;
  67.  
  68.             foreach (var v in nodes)
  69.             {
  70.                 Node currentNode = null;
  71.                 foreach (var minNode in nodes)
  72.                     if (!visited[minNode] && (currentNode == null || weight[minNode] < weight[currentNode]))
  73.                         currentNode = minNode;
  74.  
  75.                 if (weight[currentNode] == int.MaxValue)
  76.                     break;
  77.  
  78.                 visited[currentNode] = true;
  79.  
  80.                 foreach (var nextEdge in currentNode.edges)
  81.                 {
  82.                     if (Math.Max(weight[currentNode], nextEdge.Value) < weight[nodes[nextEdge.Key]])
  83.                     {
  84.                         weight[nodes[nextEdge.Key]] = Math.Max(weight[currentNode], nextEdge.Value);
  85.                         pathNodes.AddOrChange(nextEdge.Key, nodes.IndexOf(currentNode));
  86.                     }
  87.                 }
  88.             }
  89.  
  90.             /***********************/
  91.  
  92.             if (weight[nodes[endNode]] == int.MaxValue)
  93.             {
  94.                 File.WriteAllText("out.txt", "N");
  95.                 return;
  96.             }
  97.             else
  98.             {
  99.                 File.AppendAllText("out.txt", "Y\r\n");
  100.  
  101.                 int currentNode = endNode;
  102.                 var path = new List<int>();
  103.                 while (currentNode != startNode)
  104.                 {
  105.                     path.Add(currentNode);
  106.                     currentNode = pathNodes[currentNode];
  107.                 }
  108.                 path.Add(startNode);
  109.  
  110.                 for (var i = 0; i < path.Count; i++)
  111.                     path[i]++;
  112.  
  113.                 path.Reverse();
  114.  
  115.                 File.AppendAllText("out.txt", string.Join(" ", path) + "\r\n");
  116.                 File.AppendAllText("out.txt", weight[nodes[endNode]].ToString());
  117.             }
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement