Last active
January 26, 2019 16:13
-
-
Save fpdjsns/2537bd9b509dc10e1820232bc84e4741 to your computer and use it in GitHub Desktop.
[BOJ] 1504. 특정한 최단 경로 : https://www.acmicpc.net/problem/1504. 플로이드
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
// | |
// Created by fpdjsns | |
// Copyright © 2019 fpdjsns. All rights reserved. | |
// | |
#include<iostream> | |
#include<vector> | |
#include<algorithm> | |
using namespace std; | |
/* | |
* 시간복잡도 : O(N^3) | |
* 공간복잡도 : O(N^2) | |
*/ | |
int main() { | |
int N, E; | |
cin >> N >> E; | |
vector<vector<int>> dist(N, vector<int>(N, 1e6)); | |
int a, b, c; | |
for (int i = 0; i < E; i++) { | |
cin >> a >> b >> c; | |
a--; | |
b--; | |
dist[a][b] = c; | |
dist[b][a] = c; | |
} | |
for (int i = 0; i < N; i++) dist[i][i] = 0; | |
for (int k = 0; k < N; k++) { | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) { | |
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); | |
} | |
} | |
} | |
cin >> a >> b; | |
a--; b--; | |
int ans = min(dist[0][a] + dist[a][b] + dist[b][N - 1], dist[0][b] + dist[b][a] + dist[a][N - 1]); | |
if (ans > 1e6) ans = -1; | |
printf("%d", ans); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment