프로그래머스 순위
2019. 10. 19. 17:19ㆍ알고리즘/프로그래머스
모든 정점에 대해서 연결 유무를 파악해야 하는 플로이드 와샬 알고리즘 문제다. 최단 경로보다는 연결 유무에 초점이 맞춰져있는 문제다. 하나의 노드에 관해서 나가는 간선과 들어오는 간선의 합이 반드시 N-1이어야만 확실한 순위를 알 수 있다
문제: https://programmers.co.kr/learn/courses/30/lessons/49191
깃허브주소: https://github.com/surinoel/boj/blob/master/Programmers_순위.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
int d[101][101]; | |
int solution(int n, vector<vector<int>> results) { | |
int answer = 0; | |
fill(&d[0][0], &d[n][n + 1], 1e6); | |
for (int i = 0; i < results.size(); i++) { | |
int x = results[i][0]; | |
int y = results[i][1]; | |
d[x][y] = 1; | |
} | |
for (int k = 1; k <= n; k++) { | |
for (int i = 1; i <= n; i++) { | |
for (int j = 1; j <= n; j++) { | |
if (d[i][j] > d[i][k] + d[k][j]) { | |
d[i][j] = d[i][k] + d[k][j]; | |
} | |
} | |
} | |
} | |
for (int i = 1; i <= n; i++) { | |
bool ok = true; | |
for (int j = 1; j <= n; j++) { | |
if (i == j) continue; | |
if (d[i][j] == 1e6 && d[j][i] == 1e6) { | |
ok = false; | |
break; | |
} | |
} | |
if (ok) answer += 1; | |
} | |
return answer; | |
} |
'알고리즘 > 프로그래머스' 카테고리의 다른 글
프로그래머스 큰 수 만들기 (0) | 2019.10.21 |
---|---|
프로그래머스 조이스틱 (0) | 2019.10.20 |
프로그래머스 가장 먼 노드 (0) | 2019.10.18 |
프로그래머스 체육복 (0) | 2019.10.08 |
프로그래머스 카펫 (0) | 2019.10.03 |