https://programmers.co.kr/learn/courses/30/lessons/72413
#include <string>
#include <vector>
using namespace std;
#define INF 987654321
const int MAX = 201;
int fare[MAX][MAX];
int N;
int Min(int a, int b) { if (a < b) return a; return b; }
void FindCheappest() {
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
for (int k = 1; k <= N; k++) {
if (i == j || i == k || j == k) continue;
if(fare[j][i] != INF && fare[i][k] != INF) fare[j][k] = Min(fare[j][k], fare[j][i] + fare[i][k]);
}
}
}
}
int solution(int n, int s, int a, int b, vector<vector<int>> fares) {
int answer = 0;
N = n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
fare[i][j] = 987654321;
}
}
for (int i = 0; i < fares.size(); i++) {
fare[fares[i][0]][fares[i][1]] = fares[i][2];
fare[fares[i][1]][fares[i][0]] = fares[i][2];
}
for (int i = 1; i <= n; i++) fare[i][i] = 0;
FindCheappest();
answer = fare[s][a] + fare[s][b];
for (int i = 1; i <= n; i++) {
if(fare[s][i] != INF && fare[i][a] != INF && fare[i][b] != INF)
answer = Min(fare[s][i] + fare[i][a] + fare[i][b], answer);
}
return answer;
}
'백준 알고리즘 > 플로이드 와샬 알고리즘' 카테고리의 다른 글
백준 2606번 C++ (0) | 2019.01.05 |
---|