| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.ModifyGraphEdgeWeights; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class ModifyGraphEdgeWeightsDijkstra : IModifyGraphEdgeWeights |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O((n + m) log n) |
| | 19 | | /// Space complexity - O(n + m) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="n"></param> |
| | 22 | | /// <param name="edges"></param> |
| | 23 | | /// <param name="source"></param> |
| | 24 | | /// <param name="destination"></param> |
| | 25 | | /// <param name="target"></param> |
| | 26 | | /// <returns></returns> |
| | 27 | | public int[][] ModifiedGraphEdges(int n, int[][] edges, int source, int destination, int target) |
| 3 | 28 | | { |
| 3 | 29 | | var adjacencyEdges = new List<Edge>[n]; |
| | 30 | |
|
| 30 | 31 | | for (var i = 0; i < n; i++) |
| 12 | 32 | | { |
| 12 | 33 | | adjacencyEdges[i] = []; |
| 12 | 34 | | } |
| | 35 | |
|
| 26 | 36 | | for (var i = 0; i < edges.Length; i++) |
| 10 | 37 | | { |
| 10 | 38 | | adjacencyEdges[edges[i][0]].Add(new Edge(edges[i][1], i)); |
| 10 | 39 | | adjacencyEdges[edges[i][1]].Add(new Edge(edges[i][0], i)); |
| 10 | 40 | | } |
| | 41 | |
|
| 3 | 42 | | var distances = new int[n, 2]; |
| | 43 | |
|
| 30 | 44 | | for (var i = 0; i < n; i++) |
| 12 | 45 | | { |
| 12 | 46 | | distances[i, 0] = distances[i, 1] = i == source ? 0 : int.MaxValue; |
| 12 | 47 | | } |
| | 48 | |
|
| 3 | 49 | | Dijkstra(adjacencyEdges, edges, distances, source, 0, 0); |
| | 50 | |
|
| 3 | 51 | | var difference = target - distances[destination, 0]; |
| | 52 | |
|
| 3 | 53 | | if (difference < 0) |
| 0 | 54 | | { |
| 0 | 55 | | return []; |
| | 56 | | } |
| | 57 | |
|
| 3 | 58 | | Dijkstra(adjacencyEdges, edges, distances, source, difference, 1); |
| | 59 | |
|
| 3 | 60 | | if (distances[destination, 1] < target) |
| 1 | 61 | | { |
| 1 | 62 | | return []; |
| | 63 | | } |
| | 64 | |
|
| 22 | 65 | | foreach (var edge in edges) |
| 8 | 66 | | { |
| 8 | 67 | | if (edge[2] == -1) |
| 3 | 68 | | { |
| 3 | 69 | | edge[2] = 1; |
| 3 | 70 | | } |
| 8 | 71 | | } |
| | 72 | |
|
| 2 | 73 | | return edges; |
| 3 | 74 | | } |
| | 75 | |
|
| | 76 | | private static void Dijkstra(List<Edge>[] adjacencyEdges, int[][] edges, int[,] distances, int source, |
| | 77 | | int difference, int run) |
| 6 | 78 | | { |
| 6 | 79 | | var edgeDistancesPriorityQueue = new PriorityQueue<EdgeDistance, int>(); |
| | 80 | |
|
| 6 | 81 | | edgeDistancesPriorityQueue.Enqueue(new EdgeDistance(new Edge(source, -1), 0), 0); |
| 6 | 82 | | distances[source, run] = 0; |
| | 83 | |
|
| 30 | 84 | | while (edgeDistancesPriorityQueue.Count > 0) |
| 24 | 85 | | { |
| 24 | 86 | | var current = edgeDistancesPriorityQueue.Dequeue(); |
| | 87 | |
|
| 24 | 88 | | if (current.Distance > distances[current.Edge.Node, run]) |
| 0 | 89 | | { |
| 0 | 90 | | continue; |
| | 91 | | } |
| | 92 | |
|
| 152 | 93 | | foreach (var edge in adjacencyEdges[current.Edge.Node]) |
| 40 | 94 | | { |
| 40 | 95 | | var weight = edges[edge.Index][2]; |
| | 96 | |
|
| 40 | 97 | | if (weight == -1) |
| 21 | 98 | | { |
| 21 | 99 | | weight = 1; |
| 21 | 100 | | } |
| | 101 | |
|
| 40 | 102 | | if (run == 1 && edges[edge.Index][2] == -1) |
| 9 | 103 | | { |
| 9 | 104 | | var newWeight = difference + distances[edge.Node, 0] - distances[current.Edge.Node, 1]; |
| | 105 | |
|
| 9 | 106 | | if (newWeight > weight) |
| 3 | 107 | | { |
| 3 | 108 | | edges[edge.Index][2] = weight = newWeight; |
| 3 | 109 | | } |
| 9 | 110 | | } |
| | 111 | |
|
| 40 | 112 | | var newDistance = distances[current.Edge.Node, run] + weight; |
| | 113 | |
|
| 40 | 114 | | if (distances[edge.Node, run] <= newDistance) |
| 22 | 115 | | { |
| 22 | 116 | | continue; |
| | 117 | | } |
| | 118 | |
|
| 18 | 119 | | distances[edge.Node, run] = newDistance; |
| 18 | 120 | | edgeDistancesPriorityQueue.Enqueue(new EdgeDistance(edge, newDistance), newDistance); |
| 18 | 121 | | } |
| 24 | 122 | | } |
| 6 | 123 | | } |
| | 124 | |
|
| 253 | 125 | | private record Edge(int Node, int Index); |
| | 126 | |
|
| 145 | 127 | | private record EdgeDistance(Edge Edge, int Distance); |
| | 128 | | } |