No Rules Rules

운동 (feat. 백준, 1956번) 본문

생활/코테

운동 (feat. 백준, 1956번)

개발하는 완두콩 2022. 8. 23. 12:56
728x90
반응형

운동
https://www.acmicpc.net/problem/1956

 

1956번: 운동

첫째 줄에 V와 E가 빈칸을 사이에 두고 주어진다. (2 ≤ V ≤ 400, 0 ≤ E ≤ V(V-1)) 다음 E개의 줄에는 각각 세 개의 정수 a, b, c가 주어진다. a번 마을에서 b번 마을로 가는 거리가 c인 도로가 있다는 의

www.acmicpc.net

 

반응형

 

// woohyeon.kim
// kim519620.tistory.com
#include <iostream>
#include <algorithm>
using namespace std;
int V, E;
int arr[401][401];
int main() {
	ios::sync_with_stdio(false), cin.tie(NULL);
	cin >> V >> E;
	for (register int i = 1, j; i <= V; ++i)
		for(j = 1; j <= V; ++j)
			arr[i][j] = static_cast<int>(1e9);
	for (register int e = 0, a, b, c; e < E; ++e)
		cin >> a >> b >> c, arr[a][b] = c;
	for (register int m = 1, f, t; m <= V; ++m)
		for (f = 1; f <= V; ++f)
			for (t = 1; t <= V; ++t)
				arr[f][t] = min(arr[f][t], arr[f][m] + arr[m][t]);
	register int ans = static_cast<int>(1e9);
	for(register int i = 1, j; i <= V; ++i)
		for(j = 1; j <= V; ++j)
			ans = min(ans, arr[i][j] + arr[j][i]);
	if (ans == static_cast<int>(1e9))
		cout << -1;
	else
		cout << ans;
	return 0;
}
// *&)*@*

이전 '플로이드' 문제와 같은 워셜 알고리즘 풀이법입니다.

방식은 3중 for문으로 정형화된 형태이므로 아래를 참고하세요.

 

플로이드 (feat. 백준, 11404번)

플로이드 https://www.acmicpc.net/problem/11404 11404번: 플로이드 첫째 줄에 도시의 개수 n이 주어지고 둘째 줄에는 버스의 개수 m이 주어진다. 그리고 셋째 줄부터 m+2줄까지 다음과 같은 버스의 정보가 주

kim519620.tistory.com

 

728x90
반응형
Comments