cgy12306

[백준 BoJ] 1956 - 운동 본문

Algorithm/C++

[백준 BoJ] 1956 - 운동

cgy12306 2022. 1. 4. 19:53
// https://www.acmicpc.net/problem/1956
// 운동
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int map[402][402];
int V, E;
int INF = 999999999;
void floyd() {
	for(int k = 1; k <= V; k++) {
		for (int i = 1; i <= V; i++) {
			for (int j = 1; j <= V; j++) {
				if (map[i][k] != INF && map[k][j] != INF) {
					map[i][j] = min(map[i][k] + map[k][j], map[i][j]);
				}
			}
		}
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	fill(map[0], map[402], INF);
	cin >> V >> E;
	for (int i = 0; i < E; i++) {
		int from, to, cost;
		cin >> from >> to >> cost;
		map[from][to] = cost;
	}

	floyd();
	int Min = INF;
	for (int i = 1; i <= V; i++) {
		Min = min(map[i][i], Min);
	}
	if (Min == INF) cout << "-1";
	else cout << Min;
}

'Algorithm > C++' 카테고리의 다른 글

[백준 BoJ] 1219 - 오민식의 고민  (0) 2022.01.07
[백준 BoJ] 1865 - 웜홀  (0) 2022.01.07
[백준 BoJ] 10159 - 저울  (0) 2022.01.04
[백준 BoJ] 1261 - 알고스팟  (0) 2022.01.03
[백준 BoJ] 4485 - 녹색 옷 입은 애가 젤다지?  (0) 2022.01.03
Comments