forked from hamidmayeli/HackerRankSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cs
225 lines (175 loc) · 6.43 KB
/
Matrix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Matrix
// score: 0.875
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Solution {
// Complete the minTime function below.
static int minTime(int[][] rawRoads, int[] machines)
{
var calculator = new Calculator(machines);
var sum = 0;
var roads = new Road[rawRoads.Length];
for (int index = 0; index < rawRoads.Length; index++)
roads[index] = new Road
{
From = rawRoads[index][0],
To = rawRoads[index][1],
Weight = rawRoads[index][2]
};
Array.Sort(roads);
foreach (var road in roads)
{
var temp = calculator.AddRoadAndCheckIfCausedIssue(road.From, road.To, road.Weight);
if (temp.CausedIssue)
{
sum += temp.MinWeight;
calculator.Disconnect(temp.Left, temp.Right);
}
}
return sum;
}
class Calculator
{
readonly Dictionary<int, List<Road>> Connections = new Dictionary<int, List<Road>>();
readonly int[] Machines;
public Calculator(int[] machines)
{
Machines = machines;
Array.Sort(Machines);
}
public void Disconnect(int left, int right)
{
DisconnectOnSide(left, right);
DisconnectOnSide(right, left);
}
private void DisconnectOnSide(int from, int to)
{
if (Connections.TryGetValue(from, out var list))
list.Remove(list.First(x => x.To == to));
else
throw new InvalidOperationException("They are already disconnected.");
}
public AddRoadResult AddRoadAndCheckIfCausedIssue(
int left,
int right,
int weight)
{
AddToDic(left, right, weight);
AddToDic(right, left, weight);
var minWeightRoad = new SearchContext(this, left).GetMinWeightRoad();
var result = new AddRoadResult
{
CausedIssue = minWeightRoad.Weight != int.MaxValue,
MinWeight = minWeightRoad.Weight,
Left = minWeightRoad.From,
Right = minWeightRoad.To,
};
return result;
}
class SearchContext
{
readonly Calculator Calculator;
readonly Queue<int> Queue = new Queue<int>();
readonly Dictionary<int, bool> AddedToQueueState = new Dictionary<int, bool>();
readonly Dictionary<int, bool> VisitingState = new Dictionary<int, bool>();
public SearchContext(Calculator calculator, int root)
{
Calculator = calculator;
Queue.Enqueue(root);
AddedToQueueState.Add(root, true);
}
public Road GetMinWeightRoad()
{
var machines = new List<int>();
while (Queue.Count > 0)
{
var current = Queue.Dequeue();
if (Array.BinarySearch(Calculator.Machines, current) >= 0)
{
machines.Add(current);
if (machines.Count == 2)
break;
}
foreach (var road in Calculator.Connections[current])
{
if (!AddedToQueueState.TryGetValue(road.To, out var value))
{
Queue.Enqueue(road.To);
AddedToQueueState.Add(road.To, true);
}
}
}
if (machines.Count < 2) return new Road { Weight = int.MaxValue };
return GetShortestRoad(machines[0], machines[1]);
}
private Road GetShortestRoad(int from, int to)
{
VisitingState.Add(from, true);
var linkes = Calculator.Connections[from];
foreach (var link in linkes)
{
if (link.To == to) return link;
if(VisitingState.ContainsKey(link.To)) continue;
var temp = GetShortestRoad(link.To, to);
if (temp != null)
return link.Weight < temp.Weight ? link : temp;
}
return null;
}
}
private void AddToDic(int from, int to, int weight)
{
var item = new Road { From = from, To = to, Weight = weight };
if (Connections.TryGetValue(from, out var list))
list.Add(item);
else
Connections.Add(from, new List<Road> { item });
}
}
class AddRoadResult
{
public bool CausedIssue;
public int MinWeight;
public int Left;
public int Right;
public override string ToString() => $"{CausedIssue} => {Left} <=> {Right} ({MinWeight})";
}
class Road : IComparable<Road>
{
public int From;
public int To;
public int Weight;
public int CompareTo(Road other) => Weight.CompareTo(other.Weight);
public override string ToString() => $"{From} => {To} : {Weight}";
}
static void Main(string[] args) {
TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string[] nk = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(nk[0].Trim());
int k = Convert.ToInt32(nk[1].Trim());
int[][] roads = new int[n - 1][];
for (int i = 0; i < n - 1; i++) {
roads[i] = Array.ConvertAll(Console.ReadLine().Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries), roadsTemp => Convert.ToInt32(roadsTemp.Trim()));
}
int[] machines = new int [k];
for (int i = 0; i < k; i++) {
int machinesItem = Convert.ToInt32(Console.ReadLine());
machines[i] = machinesItem;
}
int result = minTime(roads, machines);
textWriter.WriteLine(result);
textWriter.Flush();
textWriter.Close();
}
}