Ejemplo n.º 1
0
        public void AddsEdges()
        {
            var graph = new WeightedSimpleGraph <int>(5);

            Assert.AreEqual(0, graph.Vertices[0].Degree);
            Assert.AreEqual(0, graph.Vertices[1].Degree);

            graph.AddEdge(0, 1, 99);
            Assert.AreEqual(1, graph.Vertices[0].Degree);
            Assert.AreEqual(1, graph.Vertices[1].Degree);
            Assert.AreEqual(1, graph.Vertices[0].Neighbors.Single().ID);
            Assert.AreEqual(0, graph.Vertices[1].Neighbors.Single().ID);
            Assert.AreEqual(99, graph.Vertices[0].Neighbors.Single().GetEdgeWeight(0));
            Assert.AreEqual(99, graph.Vertices[1].Neighbors.Single().GetEdgeWeight(1));

            graph.AddEdge(1, 4, 100);
            Assert.AreEqual(1, graph.Vertices[0].Degree);
            Assert.AreEqual(2, graph.Vertices[1].Degree);
            Assert.AreEqual(1, graph.Vertices[4].Degree);
            Assert.AreEqual(1, graph.Vertices[0].Neighbors.Single().ID);
            Assert.AreEqual(99, graph.Vertices[0].Neighbors.Single().GetEdgeWeight(0));
            CollectionAssert.AreEquivalent(new[] { 0, 4 }, graph.Vertices[1].Neighbors.Select(n => n.ID).ToArray());
            CollectionAssert.AreEquivalent(new[] { 99, 100 }, graph.Vertices[1].Neighbors.Select(n => n.GetEdgeWeight(1)).ToArray());
            Assert.AreEqual(1, graph.Vertices[4].Neighbors.Single().ID);
            Assert.AreEqual(100, graph.Vertices[4].Neighbors.Single().GetEdgeWeight(4));
        }
Ejemplo n.º 2
0
    private static void Main()
    {
        int remainingTestCases = FastIO.ReadNonNegativeInt();

        while (remainingTestCases-- > 0)
        {
            int cityCount      = FastIO.ReadNonNegativeInt();
            int highwayCount   = FastIO.ReadNonNegativeInt();
            int startCityIndex = FastIO.ReadNonNegativeInt() - 1;
            int endCityIndex   = FastIO.ReadNonNegativeInt() - 1;
            var cityGraph      = new WeightedSimpleGraph(cityCount);

            for (int h = 0; h < highwayCount; ++h)
            {
                cityGraph.AddEdge(
                    firstVertexID: FastIO.ReadNonNegativeInt() - 1,
                    secondVertexID: FastIO.ReadNonNegativeInt() - 1,
                    weight: FastIO.ReadNonNegativeInt());
            }

            int?totalMinutes = HIGHWAYS.Solve(cityGraph,
                                              startCity: cityGraph.Vertices[startCityIndex],
                                              endCity: cityGraph.Vertices[endCityIndex]);

            Console.WriteLine(totalMinutes?.ToString() ?? "NONE");
        }
    }
Ejemplo n.º 3
0
    // For example, an edge like (1, 2, 4) => there's an edge between vertices 0 and 1 with weight 4.
    public static WeightedSimpleGraph CreateFromOneBasedEdges(int vertexCount, int[,] edges)
    {
        var graph = new WeightedSimpleGraph(vertexCount);

        for (int i = 0; i < edges.GetLength(0); ++i)
        {
            graph.AddEdge(edges[i, 0] - 1, edges[i, 1] - 1, edges[i, 2]);
        }

        return(graph);
    }
Ejemplo n.º 4
0
        public void TryGetEdgeWeight()
        {
            var  graph = new WeightedSimpleGraph <int>(5);
            int  value;
            bool hasValue;

            hasValue = graph.Vertices[0].TryGetEdgeWeight(1, out value);
            Assert.AreEqual(0, value);
            Assert.IsFalse(hasValue);

            graph.AddEdge(0, 3, 99);
            hasValue = graph.Vertices[0].TryGetEdgeWeight(3, out value);
            Assert.AreEqual(99, value);
            Assert.IsTrue(hasValue);
            hasValue = graph.Vertices[3].TryGetEdgeWeight(0, out value);
            Assert.AreEqual(99, value);
            Assert.IsTrue(hasValue);
            hasValue = graph.Vertices[0].TryGetEdgeWeight(1, out value);
            Assert.AreEqual(0, value);
            Assert.IsFalse(hasValue);
        }
Ejemplo n.º 5
0
    private static void Main()
    {
        int intersectionCount;

        while ((intersectionCount = FastIO.ReadNonNegativeInt()) != 0)
        {
            int streetCount       = FastIO.ReadNonNegativeInt();
            var intersectionGraph = new WeightedSimpleGraph(intersectionCount);

            for (int i = 0; i < streetCount; ++i)
            {
                intersectionGraph.AddEdge(
                    firstVertexID: FastIO.ReadNonNegativeInt() - 1,
                    secondVertexID: FastIO.ReadNonNegativeInt() - 1,
                    weight: FastIO.ReadNonNegativeInt());
            }

            Console.WriteLine(
                $"{CHICAGO.Solve(intersectionGraph):F6} percent");
        }
    }