๋ฌธ์
์ฝ๋
#include <iostream>
#include <vector>
#include <algorithm>
#define INF 1e9
using namespace std;
int N,Q;
int graph[301][301];
vector<pair<int, int>>v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N;
for (int i = 0; i < 301; i++) { //์ต๋จ ๊ฑฐ๋ฆฌ ํ
์ด๋ธ์ ๋ชจ๋ ๋ฌดํ์ผ๋ก ์ด๊ธฐํ
fill(graph[i], graph[i] + 301, INF);
}
for (int a = 1; a <= N; a++) { //์๊ธฐ ์์ ์์ ์๊ธฐ ์์ ์ผ๋ก ๊ฐ๋ ๋น์ฉ์ 0์ผ๋ก ์ด๊ธฐํ
for (int b = 1; b <= N; b++) {
if (a == b) graph[a][b] = 0;
}
}
for (int i = 0; i < N; i++) { //๊ฐ์ ์ ๋ณด ์
๋ ฅ
int a, b; cin >> a >> b;
v.push_back({ a,b });
}
for (int i = 1; i <= N; i++) {
for (int k = 1; k <= N; k++) {
if (v[i-1].second >= v[k-1].first && v[k-1].second >= v[i-1].first) { //๊ทธ๋ํ๊ฐ ๊ฒน์น๋ ๋ถ๋ถ์ด ์์๋ ์น๊ตฌ๊ด๊ณ์ด๋ค.
graph[i][k] = 1;
graph[k][i] = 1;
}
}
}
for (int k = 1; k <= N; k++) { //ํ๋ก์ด๋-์์ฌ ์๊ณ ๋ฆฌ์ฆ ์ํ
for (int a = 1; a <= N; a++) {
for (int b = 1; b <= N; b++) {
graph[a][b] = min(graph[a][b], graph[a][k] + graph[k][b]);
}
}
}
cin >> Q;
for (int i = 0; i < Q; i++) { //์ ๋ถ์ด ๊ฐ๊น์ด ์ ๋๋ฅผ ์ถ๋ ฅ.
int a, b; cin >> a >> b;
if (graph[a][b] == INF) cout << -1 << "\n";
else cout << graph[a][b] << "\n";
}
}
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 14681๋ฒ - ์ฌ๋ถ๋ฉด ๊ณ ๋ฅด๊ธฐ (0) | 2021.11.11 |
---|---|
[๋ฐฑ์ค,c++] 1463๋ฒ - 1๋ก ๋ง๋ค๊ธฐ (0) | 2021.11.11 |
[๋ฐฑ์ค,c++] 14621๋ฒ - ๋๋ง ์๋๋ ์ฐ์ (0) | 2021.11.11 |
[๋ฐฑ์ค,c++] 14567๋ฒ - ์ ์๊ณผ๋ชฉ (0) | 2021.11.11 |
[๋ฐฑ์ค,c++] 14563๋ฒ - ์์ ์ (0) | 2021.11.11 |
[๋ฐฑ์ค,c++] 14502๋ฒ - ์ฐ๊ตฌ์ (0) | 2021.11.11 |
๋๊ธ