11.20

import java.util.*;

// 1. 边类:存储目标顶点和权重
class Edge {
int targetVertex; // 目标顶点的索引
int weight; // 边的权重(无权图可设为1)

public Edge(int targetVertex, int weight) {
    this.targetVertex = targetVertex;
    this.weight = weight;
}

}

// 2. 图类(邻接表实现)
class Graph {
private List vertices; // 存储顶点数据(如 "A", "B")
private List<List> adjacencyList; // 邻接表:index=顶点索引,value=该顶点的所有边

// 初始化空图
public Graph() {
    vertices = new ArrayList<>();
    adjacencyList = new ArrayList<>();
}
posted @ 2025-11-21 01:02  喜欢写轻小说的日央  阅读(2)  评论(0)    收藏  举报