Skip to content

Commit b362834

Browse files
authored
feat: add cs solution to lc problem: No.2642 (doocs#1953)
* feat: add solution.cs to lc problems: No.2642 * Update README_EN.md to lc problems: No.2642 * Update README.md to lc problems: No.2642 * style: format code and docs with prettier --------- Co-authored-by: dev-mauli <dev-mauli@users.noreply.github.com>
1 parent 54ac9e5 commit b362834

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

solution/2600-2699/2642.Design Graph With Shortest Path Calculator/README.md

+61
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,67 @@ class Graph {
323323
*/
324324
```
325325

326+
### **C#**
327+
328+
```cs
329+
public class Graph {
330+
private int n;
331+
private int[][] g;
332+
private readonly int inf = 1 << 29;
333+
334+
public Graph(int n, int[][] edges) {
335+
this.n = n;
336+
g = new int[n][];
337+
for (int i = 0; i < n; i++)
338+
{
339+
g[i] = new int[n];
340+
for (int j = 0; j < n; j++)
341+
{
342+
g[i][j] = inf;
343+
}
344+
}
345+
foreach (int[] e in edges)
346+
{
347+
g[e[0]][e[1]] = e[2];
348+
}
349+
}
350+
351+
public void AddEdge(int[] edge) {
352+
g[edge[0]][edge[1]] = edge[2];
353+
}
354+
355+
public int ShortestPath(int node1, int node2) {
356+
int[] dist = new int[n];
357+
bool[] vis = new bool[n];
358+
Array.Fill(dist, inf);
359+
dist[node1] = 0;
360+
361+
for (int i = 0; i < n; i++)
362+
{
363+
int t = -1;
364+
for (int j = 0; j < n; j++)
365+
{
366+
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
367+
t = j;
368+
}
369+
vis[t] = true;
370+
for (int j = 0; j < n; j++)
371+
{
372+
dist[j] = Math.Min(dist[j], dist[t] + g[t][j]);
373+
}
374+
}
375+
return dist[node2] >= inf ? -1 : dist[node2];
376+
}
377+
}
378+
379+
/**
380+
* Your Graph object will be instantiated and called as such:
381+
* Graph obj = new Graph(n, edges);
382+
* obj.AddEdge(edge);
383+
* int param_2 = obj.ShortestPath(node1,node2);
384+
*/
385+
```
386+
326387
### **...**
327388

328389
```

solution/2600-2699/2642.Design Graph With Shortest Path Calculator/README_EN.md

+61
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,67 @@ class Graph {
303303
*/
304304
```
305305

306+
### **C#**
307+
308+
```cs
309+
public class Graph {
310+
private int n;
311+
private int[][] g;
312+
private readonly int inf = 1 << 29;
313+
314+
public Graph(int n, int[][] edges) {
315+
this.n = n;
316+
g = new int[n][];
317+
for (int i = 0; i < n; i++)
318+
{
319+
g[i] = new int[n];
320+
for (int j = 0; j < n; j++)
321+
{
322+
g[i][j] = inf;
323+
}
324+
}
325+
foreach (int[] e in edges)
326+
{
327+
g[e[0]][e[1]] = e[2];
328+
}
329+
}
330+
331+
public void AddEdge(int[] edge) {
332+
g[edge[0]][edge[1]] = edge[2];
333+
}
334+
335+
public int ShortestPath(int node1, int node2) {
336+
int[] dist = new int[n];
337+
bool[] vis = new bool[n];
338+
Array.Fill(dist, inf);
339+
dist[node1] = 0;
340+
341+
for (int i = 0; i < n; i++)
342+
{
343+
int t = -1;
344+
for (int j = 0; j < n; j++)
345+
{
346+
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
347+
t = j;
348+
}
349+
vis[t] = true;
350+
for (int j = 0; j < n; j++)
351+
{
352+
dist[j] = Math.Min(dist[j], dist[t] + g[t][j]);
353+
}
354+
}
355+
return dist[node2] >= inf ? -1 : dist[node2];
356+
}
357+
}
358+
359+
/**
360+
* Your Graph object will be instantiated and called as such:
361+
* Graph obj = new Graph(n, edges);
362+
* obj.AddEdge(edge);
363+
* int param_2 = obj.ShortestPath(node1,node2);
364+
*/
365+
```
366+
306367
### **...**
307368

308369
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
public class Graph {
2+
private int n;
3+
private int[][] g;
4+
private readonly int inf = 1 << 29;
5+
6+
public Graph(int n, int[][] edges) {
7+
this.n = n;
8+
g = new int[n][];
9+
for (int i = 0; i < n; i++)
10+
{
11+
g[i] = new int[n];
12+
for (int j = 0; j < n; j++)
13+
{
14+
g[i][j] = inf;
15+
}
16+
}
17+
foreach (int[] e in edges)
18+
{
19+
g[e[0]][e[1]] = e[2];
20+
}
21+
}
22+
23+
public void AddEdge(int[] edge) {
24+
g[edge[0]][edge[1]] = edge[2];
25+
}
26+
27+
public int ShortestPath(int node1, int node2) {
28+
int[] dist = new int[n];
29+
bool[] vis = new bool[n];
30+
Array.Fill(dist, inf);
31+
dist[node1] = 0;
32+
33+
for (int i = 0; i < n; i++)
34+
{
35+
int t = -1;
36+
for (int j = 0; j < n; j++)
37+
{
38+
if (!vis[j] && (t == -1 || dist[t] > dist[j]))
39+
t = j;
40+
}
41+
vis[t] = true;
42+
for (int j = 0; j < n; j++)
43+
{
44+
dist[j] = Math.Min(dist[j], dist[t] + g[t][j]);
45+
}
46+
}
47+
return dist[node2] >= inf ? -1 : dist[node2];
48+
}
49+
}
50+
51+
/**
52+
* Your Graph object will be instantiated and called as such:
53+
* Graph obj = new Graph(n, edges);
54+
* obj.AddEdge(edge);
55+
* int param_2 = obj.ShortestPath(node1,node2);
56+
*/

0 commit comments

Comments
 (0)