-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
45 lines (38 loc) · 1.11 KB
/
Copy pathvector.cpp
File metadata and controls
45 lines (38 loc) · 1.11 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
#include <iostream>
#include <vector>
using namespace std;
struct Edge{
int endNode;
int value;
};
int main(){
// 이차원 벡터 초기화
vector<vector<Edge>> A;
int N, E;
cin >> N >> E;
A.resize(N + 1);
// A[i] = { {endNode, value}, {endNode, value}, ... }
// 그래프 데이터 저장하기
for (int i = 0; i < E; i++){
int s, e, v = 0;
cin >> s >> e >> v;
A[s].push_back({e, v});
}
// 그래프 데이터 가져오기
for (int i = 1; i <= N; i++){
if (A[i].empty())
continue;
cout << "출발노드 " << i << ":\n";
// 중요! A[i]안의 Edge하나씩 꺼내서 edge라는 이름으로 반복 처리
for (Edge edge : A[i]){
cout << " -> 도착노드: " << edge.endNode
<< ", 가중치: " << edge.value << '\n';
}
// for (int j = 0; j < A[i].size(); j++){
// Edge edge = A[i][j];
// cout << " -> 도착노드: " << edge.endNode
// << ", 가중치: " << edge.value << '\n';
// }
cout << "\n";
}
}