#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<bool> visited(101, false);
bool compare(const vector<int> &a, const vector<int> &b)
{
return a[2] < b[2];
}
bool isCycle(vector<int> &cost)
{
if (visited[cost[0]] && visited[cost[1]])
{
return true;
}
return false;
}
int solution(int n, vector<vector<int> > costs)
{
int answer = 0;
sort(costs.begin(), costs.end(), compare);
for (int i = 0, count = 0; i < costs.size() && count != n - 1; i++)
{
if (!isCycle(costs[i]))
{
visited[costs[i][0]] = true;
visited[costs[i][1]] = true;
answer += costs[i][2];
count++;
}
}
return answer;
}
'알고리즘 문제 > 프로그래머스' 카테고리의 다른 글
프로그래머스 탐욕법 단속카메라 (0) | 2021.08.08 |
---|---|
프로그래머스 동적계획법 등굣길 c++ 풀이 (0) | 2021.07.23 |
프로그래머스 동적 계획법 정수 삼각형 c++ 풀이 (0) | 2021.06.24 |
[C++] 프로그래머스 그래프 가장 먼 노드 풀이 (0) | 2021.05.23 |
[C++] 프로그래머스 힙 디스크 컨트롤러 풀이 (0) | 2021.05.16 |