STUDY 35

백준/1504번(최단 경로)/C++

필수로 지나야 하는 정점이 두 개 주어지는 최단 경로 문제. 주의할 점: 필수 정점의 순서는 주어지지 않는다. v1을 먼저 가도 되고, v2를 먼저 가도 된다. #include #include #include #include #define MAX 805 #define INF 98765432 using namespace std; int N, E, v1, v2; vector graph[MAX]; int d[MAX]; void init() { for (int i = 1; i > N >> E; for (int i = 0; i > a >> b >> c; graph[a].push_back(make_pair(b, c)); graph[b].push_back(make..

백준/1753번(최단 경로)/C++

Dijkstra 알고리즘을 활용해볼 수 있는 간단한 문제 주의할 점: 1. 가중치가 가장 작은 간선을 구할 때, 직접 비교하면 시간이 초과하므로 우선순위큐를 활용한다. 2. cin,cout은 scanf,printf보다 오래 걸린다. #include #include #include #define MAX 20010 #define INF 987654321 using namespace std; int V, E, start; vector a[MAX]; int d[MAX]; void dijkstra() { priority_queue pq; d[start] = 0; pq.push(make_pair(0, start)); while (pq.empty() == 0) { int current = pq.top().second..

백준/11729번(하노이타워 재귀)/C

재귀함수 구현 연습을 해볼 수 있는 간단한 문제 #include #include void hanoi_rec(int from, int mid, int to, int x) {//하노이 함수 if (x == 1) { printf("%d %d\n", from, to); } else { hanoi_rec(from, to, mid, x - 1); printf("%d %d\n", from, to); hanoi_rec(mid, from, to, x - 1); } } int main(void) { int n; scanf("%d", &n); int execution; execution = pow(2, n) - 1;//시행 횟수 출력 printf("%d\n", execution); hanoi_rec(1, 2, 3, n); ..

백준/ 1260번(DFS와 BFS)/ C++ ; Vector와 sort함수 사용

DFS와 BFS를 구현하는 연습을 해볼 수 있는 기본 문제. 주의해야 할 점 1. 방문할 수 있는 정점이 여러 개인 경우에는 정점 번호가 작은 것을 먼저 방문. 2. 입력으로 주어지는 간선은 양방향. 3. 간선이 없는 노드. 더 이상 방문할 수 있는 점이 없는 경우 종료되기 때문에, 어떠한 노드와도 연결되어 있지 않은 노드는 따로 처리해줄 필요가 없다. #include #include #include #include #include using namespace std; int c[1001]; //정점에 방문했는지 체크하는 배열 vector a[1001]; //그래프 정보 저장하는 벡터 //처음 그래프를 구성할 때에 에지를 추가하는 함수 void insert_edge(int i, int j) { a[i]...