-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.cs
56 lines (51 loc) · 1.34 KB
/
Solution.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
public class Graph {
private int n;
private int[][] g;
private readonly int inf = 1 << 29;
public Graph(int n, int[][] edges) {
this.n = n;
g = new int[n][];
for (int i = 0; i < n; i++)
{
g[i] = new int[n];
for (int j = 0; j < n; j++)
{
g[i][j] = inf;
}
}
foreach (int[] e in edges)
{
g[e[0]][e[1]] = e[2];
}
}
public void AddEdge(int[] edge) {
g[edge[0]][edge[1]] = edge[2];
}
public int ShortestPath(int node1, int node2) {
int[] dist = new int[n];
bool[] vis = new bool[n];
Array.Fill(dist, inf);
dist[node1] = 0;
for (int i = 0; i < n; i++)
{
int t = -1;
for (int j = 0; j < n; j++)
{
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
t = j;
}
vis[t] = true;
for (int j = 0; j < n; j++)
{
dist[j] = Math.Min(dist[j], dist[t] + g[t][j]);
}
}
return dist[node2] >= inf ? -1 : dist[node2];
}
}
/**
* Your Graph object will be instantiated and called as such:
* Graph obj = new Graph(n, edges);
* obj.AddEdge(edge);
* int param_2 = obj.ShortestPath(node1,node2);
*/