GitHubSeob

C++ / 백준 / 6156 / Cow Contest 본문

Baekjoon/Gold

C++ / 백준 / 6156 / Cow Contest

GitHubSeob 2024. 4. 4.
문제

 

https://www.acmicpc.net/problem/6156

 

6156번: Cow Contest

N (1 <= N <= 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors. The contest is conducted i

www.acmicpc.net

 

문제풀이

 

플로이드 워셜 알고리즘을 이용해 풀었다.

소의 등수를 알려면 소가 다른 소한테 이기든 지든 모든 소에 대해 결과를 알아야 한다.

따라서 최단 거리를 구해 다른 소와 모두 연결된 소의 개수를 세면 된다.

 

코드
#include <iostream>
#include <vector>
#define INF 4501
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int N(0), M(0);
	cin >> N >> M;

	vector<vector<int>>dist(N + 1, vector<int>(N + 1, INF));

	for (int cow = 0; cow <= N; ++cow) {
		dist[cow][cow] = 0;
	}

	int cow1(0), cow2(0);
	for (int idx = 0; idx < M; ++idx) {
		cin >> cow1 >> cow2;
		dist[cow1][cow2] = 1;
	}

	for (int mid = 1; mid <= N; ++mid) {
		for (cow1 = 1; cow1 <= N; ++cow1) {
			for (cow2 = 1; cow2 <= N; ++cow2) {
				dist[cow1][cow2] = min(dist[cow1][cow2], dist[cow1][mid] + dist[mid][cow2]);
			}
		}
	}

	int answer(0);
	for (cow1 = 1; cow1 <= N; ++cow1) {
		int count(0);
		for (cow2 = 1; cow2 <= N; ++cow2) {
			if (dist[cow1][cow2] != INF || dist[cow2][cow1] != INF) {
				++count;
			}
		}
		if (count == N) ++answer;
	}
	cout << answer;
}

 

'Baekjoon > Gold' 카테고리의 다른 글

C++ / 백준 / 10942 / 팰린드롬?  (0) 2024.06.05
C++ / 백준 / 5052 / 전화번호 목록  (0) 2024.04.04
C++ / 백준 / 3020 / 개똥벌레  (0) 2024.03.28
C++ / 백준 / 1245 / 농장 관리  (0) 2023.10.15
C++ / 백준 / 13422 / 도둑  (0) 2023.10.06