forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
51 lines (47 loc) · 1.33 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
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, n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
g[i, j] = inf;
}
}
foreach (var e in edges) {
int f = e[0], t = e[1], c = e[2];
g[f, t] = c;
}
}
public void AddEdge(int[] edge) {
int f = edge[0], t = edge[1], c = edge[2];
g[f, t] = c;
}
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);
*/