https://www.acmicpc.net/problem/14889
μμ΄λμ΄
λλ λ¬Έμ λ₯Ό 3κ°μ§ ννΈλ‘ λλ μ νμλ€.
1. μ λ ₯μ λ°λ λΆλΆ
2. νμ μ§λ λΆλΆ
3. μ§μ¬μ§ νμ μ΄μ©ν΄μ μ μλ₯Ό κ³μ°νκ³ νμ κΈ°λ‘
μ λ ₯μ λ°κ³ , μ§μ¬μ§ νμ μ΄μ©ν΄ μ μλ₯Ό κ³μ°νλ κ²μ μ΄λ ΅μ§ μλ€. μ΄ λ¬Έμ μ ν΅μ¬μ μΈ λΆλΆμ νμ μ§λ λΆλΆμ΄λ€. κ·Έ λΆλΆμ μλμ κ°μ΄ ꡬννμλ€.
μ΄ λ¬Έμ μ κ²½μ°μλ μ¬λμ΄ μ€ννΈν νΉμ λ§ν¬νμ΄κΈ° λλ¬Έμ μ€ννΈνμ μ λ°μ λ°°μ νλ©΄ λλ¨Έμ§λ μλμΌλ‘ λ§ν¬νμ΄ λλ€.
//νμ μ§λ ν¨μ
//curteamsiz : νμ¬ νμ λͺ λͺ
μ΄ λ€μ΄μλκ°
//before_end : λͺ λ² μ¬λκΉμ§ νμ λ°°μΉλ₯Ό νλκ°
void maketeam(int curteamsiz, int before_end) {
//ν μΈμμ΄ λͺ¨λ μ°° κ²½μ°(μ 체 μΈμμ μ λ°μ΄ μ€ννΈνμΌλ‘ λ°°μΉ μλ£)
if (curteamsiz == n/2) {
//ν λ°°μΉκ° μλ£λμΌλ μ€ννΈνμ μ μ-λ§ν¬ νμ μ μ
int save = calculate_start()-calculate_link();
if (save < 0) {
//κ°μ΄ μμμ΄λ©΄ μμλ‘ λ°κΏμ€λ€.
save *= -1;
}
//μ°μ μμ νμ μ½μ
νλ€.
q.push(save);
}
else {
for (int i = before_end+1; i <= n; i++) {
//iλ² μ¬λμ μ€ννΈνμΌλ‘ λ°°μΉ
people[i] = 1;
//μ΄ν i+1λ² μ¬λλΆν° ν λ°°μΉ μμ
maketeam(curteamsiz + 1, i);
//μν μ΄ν iλ² μ¬λμ λ€μ λ§ν¬νμΌλ‘ λ°°μΉ
people[i] = 0;
}
}
}
μ 체 μ½λ
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int arr[21][21];
int n, s;
//μ€ννΈνμ 1, λ§ν¬νμ 0
int people[21];
priority_queue <int, vector<int>, greater<int>> q;
//μ€ννΈ νμ μ μ κ³μ° ν¨μ
int calculate_start() {
int startscore = 0;
vector<int> teamstart;
for (int i = 1; i <= n; i++) {
if (people[i] == 1) {
teamstart.push_back(i);
}
}
for (int i = 0; i < teamstart.size() - 1; i++) {
for (int j = i + 1; j < teamstart.size(); j++) {
startscore += arr[teamstart[i]][teamstart[j]] + arr[teamstart[j]][teamstart[i]];
}
}
return startscore;
}
//λ§ν¬ νμ μ μ κ³μ°ν¨μ
int calculate_link() {
int linkscore = 0;
vector<int> teamlink;
for (int i = 1; i <= n; i++) {
if (people[i] == 0) {
teamlink.push_back(i);
}
}
for (int i = 0; i < teamlink.size() - 1; i++) {
for (int j = i + 1; j < teamlink.size(); j++) {
linkscore += arr[teamlink[i]][teamlink[j]] + arr[teamlink[j]][teamlink[i]];
}
}
return linkscore;
}
//νμ μ§λ ν¨μ
//curteamsiz : νμ¬ νμ λͺ λͺ
μ΄ λ€μ΄μλκ°
//before_end : λͺ λ² μ¬λκΉμ§ νμ λ°°μΉλ₯Ό νλκ°
void maketeam(int curteamsiz, int before_end) {
//ν μΈμμ΄ λͺ¨λ μ°° κ²½μ°(μ 체 μΈμμ μ λ°μ΄ μ€ννΈνμΌλ‘ λ°°μΉ μλ£)
if (curteamsiz == n / 2) {
//ν λ°°μΉκ° μλ£λμΌλ μ€ννΈνμ μ μ-λ§ν¬ νμ μ μ
int save = calculate_start() - calculate_link();
if (save < 0) {
//κ°μ΄ μμμ΄λ©΄ μμλ‘ λ°κΏμ€λ€.
save *= -1;
}
//μ°μ μμ νμ μ½μ
νλ€.
q.push(save);
}
else {
for (int i = before_end + 1; i <= n; i++) {
//iλ² μ¬λμ μ€ννΈνμΌλ‘ λ°°μΉ
people[i] = 1;
//μ΄ν i+1λ² μ¬λλΆν° ν λ°°μΉ μμ
maketeam(curteamsiz + 1, i);
//μν μ΄ν iλ² μ¬λμ λ€μ λ§ν¬νμΌλ‘ λ°°μΉ
people[i] = 0;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> arr[i][j];
}
}
maketeam(0, 1);
cout << q.top();
}
'μ½λ©ν μ€νΈ > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[λ°±μ€ 9461] νλλ° μμ΄ (C++) (0) | 2021.12.03 |
---|---|
[λ°±μ€ 1011] Fly me to the Alpha Centauri (C++) (0) | 2021.12.02 |
[λ°±μ€ 1065] νμ (C++) (0) | 2021.12.01 |
[λ°±μ€ 1541] μμ΄λ²λ¦° κ΄νΈ(C++) (0) | 2021.08.13 |
[λ°±μ€ 1676]- ν©ν λ¦¬μΌ 0μ κ°μ(C++) (0) | 2021.07.30 |
λκΈ