#include <iostream>
#define INF 1e9 //๋ฌดํ๋๋ฅผ ์๋ฏธํ๋ ๊ฐ์ผ๋ก 10์ต์ ์ง์
using namespace std;
int graph[251][251];
int N, M, K; //N=๊ฑด๋ฌผ์ ์, M=๊ธธ์ ์, ํ์๋ค์ ์ง๋ฌธ
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for (int i = 0; i < 251; i++) {
fill(graph[i], graph[i] + 251, INF);
}
for (int a = 1; a <= N; a++) {
for (int b = 1; b <= N; b++) {
if (a == b) graph[a][b] = 0; //์์ ๋ผ๋ฆฌ ์์ ๊ณผ ๋น๊ตํ ์ ์์ผ๋ ํ์๋ฅผ 0์ผ๋ก ์ฒ๋ฆฌ
}
}
for (int i = 0; i < M; i++) { //๊ธธ์ ๋ํ ์ ๋ณด ์
๋ ฅ
int a, b, c; cin >> a >> b >> c;
if (c == 0) {
graph[a][b] = 0; //c=0์ผ๋ ์ผ๋ฐฉํตํ์ด๊ธฐ๋๋ฌธ์
graph[b][a] = 1; //์๋ฐฉ์ผ๋ก ๋ง๋ค์ด์ผ ํ๊ธฐ๋๋ฌธ์ +1
}
else if (c == 1) { //c=1์ผ๋ ์๋ฐฉํตํ์ด๊ธฐ๋๋ฌธ์
graph[a][b] = 0;
graph[b][a] = 0; //์๋ฐฉ์ผ๋ก ๋ง๋ค์ด์ฃผ์ง ์์๋ ๋๋ค.
}
}
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 >> K; //ํ์๋ค์ ์ง๋ฌธ ์
๋ ฅ
for (int i = 0; i < K; i++) {
int s, e; cin >> s >> e;
cout << graph[s][e] << "\n";
}
}
'Algorithm ๐ง๐ปโ๐ป > ๋ฐฑ์ค(BOJ)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค,c++] 11650๋ฒ - ์ขํ ์ ๋ ฌํ๊ธฐ (0) | 2021.10.28 |
---|---|
[๋ฐฑ์ค,c++] 1159๋ฒ - ๋๊ตฌ ๊ฒฝ๊ธฐ (0) | 2021.10.28 |
[๋ฐฑ์ค,c++] 1157๋ฒ - ๋จ์ด ๊ณต๋ถ (0) | 2021.10.28 |
[๋ฐฑ์ค,c++] 11557๋ฒ - Yangjojang of The Year (0) | 2021.10.28 |
[๋ฐฑ์ค,c++] 1152๋ฒ - ๋จ์ด์ ๊ฐ์ (0) | 2021.10.28 |
[๋ฐฑ์ค,c++] 1149๋ฒ - RGB๊ฑฐ๋ฆฌ (0) | 2021.10.28 |
๋๊ธ