Repository navigation
Expand file tree
/
Copy pathWeightedGraph.java
More file actions
89 lines (73 loc) · 2.07 KB
/
Copy pathWeightedGraph.java
File metadata and controls
89 lines (73 loc) · 2.07 KB
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
package Graph;
import java.util.*;
/**
* Algorithm: Weighted Graph Template
* Time Complexity: O(V + E) for most operations
* Space Complexity: O(V + E)
* Use case: Foundation for weighted graph algorithms (Dijkstra, Bellman-Ford, etc.)
*/
public class WeightedGraph {
// Constants
static final int MAXN = 100005;
static final long INF = (long)1e18;
// Edge class
static class Edge {
int to;
long weight;
Edge(int to, long weight) {
this.to = to;
this.weight = weight;
}
}
// Data structures
List<Edge>[] adj;
int n; // number of nodes
boolean directed;
// Constructor
@SuppressWarnings("unchecked")
public WeightedGraph(int n, boolean directed) {
this.n = n;
this.directed = directed;
adj = new ArrayList[n + 1];
for (int i = 0; i <= n; i++) {
adj[i] = new ArrayList<>();
}
}
// Add edge method
void addEdge(int u, int v, long weight) {
adj[u].add(new Edge(v, weight));
if (!directed) {
adj[v].add(new Edge(u, weight));
}
}
// Get neighbors
List<Edge> getNeighbors(int u) {
return adj[u];
}
// Print graph
void printGraph() {
for (int i = 1; i <= n; i++) {
System.out.print(i + " -> ");
for (Edge e : adj[i]) {
System.out.print("(" + e.to + ", " + e.weight + ") ");
}
System.out.println();
}
}
// Main method with test case
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// TODO: Implement your test case here
int n = sc.nextInt(); // number of nodes
int m = sc.nextInt(); // number of edges
WeightedGraph graph = new WeightedGraph(n, false);
for (int i = 0; i < m; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
long w = sc.nextLong();
graph.addEdge(u, v, w);
}
graph.printGraph();
sc.close();
}
}