Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- vector
- 그래프
- 분할정복
- Sort
- 티스토리챌린지
- priority_queue
- 정렬
- 깊이우선탐색
- C++
- BFS
- 백트래킹
- 이분탐색
- 최소공배수
- 문자열
- 프로그래머스
- stoi
- int
- map
- 다이나믹프로그래밍
- 유클리드호제법
- 배열
- DFS
- Set
- 백준
- 에라토스테네스의 체
- 오블완
- N과M
- 알고리즘
- DP
- 우선순위큐
Archives
- Today
- Total
안녕 세상아,
[c++/백준] 14889 스타트와 링크 본문
https://www.acmicpc.net/problem/14889
14889번: 스타트와 링크
예제 2의 경우에 (1, 3, 6), (2, 4, 5)로 팀을 나누면 되고, 예제 3의 경우에는 (1, 2, 4, 5), (3, 6, 7, 8)로 팀을 나누면 된다.
www.acmicpc.net
팀 나누고 최솟값 구하는게 중요한 것 같다.
#include <iostream>
#include <cmath>
using namespace std;
int n;
int arr[21][21];
bool isVisited[22];
int minNum = 10000000; //최솟값 찾기 위함
void dfs(int x, int idx) {
if (x == n / 2) {
int start, link;
start = 0;
link = 0;
//start와 link 팀 나누기
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (isVisited[i] == true && isVisited[j] == true) {
start += arr[i][j];
}
if (isVisited[i] == false && isVisited[j] == false) {
link += arr[i][j];
}
}
}
int temp = 0;
temp = abs(start - link);
if (temp < minNum)
minNum = temp;
}
//백트래킹
for (int i = idx; i < n; i++) {
isVisited[i] = true;
dfs(x + 1, i + 1);
isVisited[i] = false;
}
}
int main() {
cin >> n;
//인접리스트
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> arr[i][j];
}
}
dfs(0, 1);
cout << minNum;
}
'백준' 카테고리의 다른 글
[c++/백준] 1012 유기농 배추 (0) | 2023.06.12 |
---|---|
[c++/백준] 11279 최대 힙 (0) | 2023.06.12 |
[c++/백준]1927 최소 힙 (1) | 2023.06.10 |
[c++/백준] 9020 골드바흐의 추측 (0) | 2023.06.06 |
[c++/백준] 1654 랜선 자르기 (0) | 2023.06.05 |