Skip to content

Commit

Permalink
연결리스트를 이용해 그래프를 구현하고, 노드들끼리 직접 연결된 경우 간선의 가중치를 출력할 수 있도록 함
Browse files Browse the repository at this point in the history
  • Loading branch information
2dongyeop committed Jul 7, 2022
1 parent a594cac commit 449a034
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions src/shortestPath/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,26 @@

public class Main {
private static BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
private static List<Node> nodeList = new ArrayList<>();
private static List<Node> graph = new ArrayList<>();
private static int nodeCount;
private static int linkCount;

public static void main(String[] args) throws IOException {
//System.out.println("정점의 개수와 간선의 개수를 입력");
String s = bufferedReader.readLine();

StringTokenizer st1 = new StringTokenizer(s);

int nodeCount = Integer.parseInt(st1.nextToken());
int linkCount = Integer.parseInt(st1.nextToken());
nodeCount = Integer.parseInt(st1.nextToken());
linkCount = Integer.parseInt(st1.nextToken());

createNode(nodeCount);

// System.out.println("시작 노드를 입력");
String n = bufferedReader.readLine();
String startNodeNum = bufferedReader.readLine();

for (int i = 0; i < linkCount; i++) {
// System.out.println("시작 노드, 끝 노드, 가중치를 입력");
// System.out.println("간선 정보 : 시작 노드, 끝 노드, 가중치를 입력");
String input = bufferedReader.readLine();

StringTokenizer st2 = new StringTokenizer(input);
Expand All @@ -38,32 +40,66 @@ public static void main(String[] args) throws IOException {
int endInt = Integer.parseInt(end);
int weightInt = Integer.parseInt(weight);

linkingNode(startInt, endInt, weightInt);
createLinking(startInt, endInt, weightInt);
}



//시작 정점을 이용한 거리 출력문 필요
printDistance(Integer.parseInt(startNodeNum));
}

private static void createNode(int N) {
for (int i = 1; i <= N; i++) {//1번 노드부터 ~ N번 노드 까지 생성
nodeList.add(new Node(i));
graph.add(new Node(i));
}
}

private static void linkingNode(int start, int end, int weight) {

private static void createLinking(int start, int end, int weight) {
for (int i = 0; i < graph.size(); i++)
if (graph.get(i).number == start)
graph.set(i, new Node(start, new Edge(end, weight)));
}

private static void printDistance(int startNodeNum) {
Node startNode = null;
for (Node n : graph) {
if (n.number == startNodeNum) {
startNode = n; //시작 노드 지정
}
}

//직접 연결되어 있을 경우
for (int i = 0; i < nodeCount; i++) {
if (i == startNodeNum) {
System.out.println("0");
} else if (startNode.edgeList.get(i).linkedNode == i) { //직접 연결이 된 경우
System.out.println(startNode.edgeList.get(i).weight);
} else { //연결이 안된 경우
System.out.println("INF");
}
}
}
}

class Node {
final int number;
List<Integer> linkedNode = new ArrayList<>(); //연결된 노드들
List<Integer> linkedWeight = new ArrayList<>(); //간선들 가중치
List<Edge> edgeList = new ArrayList<>();

Node(int number) {
this.number = number;
}

Node(int number, Edge edge) {
this.number = number;
edgeList.add(edge);
}
}

class Edge {
int linkedNode; //연결된 상대 노드 번호를 저장
int weight; //간선의 가중치

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

0 comments on commit 449a034

Please sign in to comment.