Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Prim`s Algorithm
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { class AdjacencyList { LinkedList<Tuple<int, int>>[] adjacencyList; // Constructor - creates an empty Adjacency List public AdjacencyList(int vertices) { adjacencyList = new LinkedList<Tuple<int, int>>[vertices]; for (int i = 0; i < adjacencyList.Length; ++i) { adjacencyList[i] = new LinkedList<Tuple<int, int>>(); } } // Appends a new Edge to the linked list public void addEdgeAtEnd(int startVertex, int endVertex, int weight) { adjacencyList[startVertex].AddLast(new Tuple<int, int>(endVertex, weight)); } // Returns number of vertices // Does not change for an object // Returns a copy of the Linked List of outward edges from a vertex public LinkedList<Tuple<int, int>> this[int index] { get { LinkedList<Tuple<int, int>> edgeList = new LinkedList<Tuple<int, int>>(adjacencyList[index]); return edgeList; } } // Prints the Adjacency List public void printAdjacencyList() { int i = 0; foreach (LinkedList<Tuple<int, int>> list in adjacencyList) { Console.Write("adjacencyList[" + i + "] -> "); foreach (Tuple<int, int> edge in list) { Console.Write(edge.Item1 + "(" + edge.Item2 + ")"); } ++i; Console.WriteLine(); } } // Removes the first occurence of an edge and returns true // if there was any change in the collection, else false public bool removeEdge(int startVertex, int endVertex, int weight) { Tuple<int, int> edge = new Tuple<int, int>(endVertex, weight); return adjacencyList[startVertex].Remove(edge); } } public class Program { public static void Main(string[] args) { //Your code goes here string[] vE = Console.ReadLine().Split(' '); int vertices = Int32.Parse(vE[0]); AdjacencyList adjacencyList = new AdjacencyList(vertices + 1); int edges = Int32.Parse(vE[1]); int startVertex, endVertex, weight; for (int i = 0; i < edges; ++i) { string[] input = Console.ReadLine().Split(' '); startVertex = Int32.Parse(input[0]); endVertex = Int32.Parse(input[1]); weight = Int32.Parse(input[2]); adjacencyList.addEdgeAtEnd(startVertex, endVertex, weight); } adjacencyList.printAdjacencyList(); // adjacencyList.removeEdge(1, 2, 1); // adjacencyList.printAdjacencyList(); //end code here } } }
run
|
edit
|
history
|
help
0
Floyd’s Warshall Algorithm
EpsilonComparer
1
length
Index.cshtml
recursive solution for subset sum
Prueba
Free Tiktok Fans Generator
Immutable
prime number experiment