๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 11562๋ฒˆ - ๋ฐฑ์–‘๋กœ ๋ธŒ๋ ˆ์ดํฌ

by dkswnkk 2021. 10. 28.
 

11562๋ฒˆ: ๋ฐฑ์–‘๋กœ ๋ธŒ๋ ˆ์ดํฌ

์„œ์šธ ์†Œ์žฌ Y๋ชจ ๋Œ€ํ•™๊ต์—์„œ ๋Œ€๊ทœ๋ชจ ๊ณต์‚ฌ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉด์„œ, ํ•™๊ต๊ฐ€ ๋งˆ์น˜ ๋ฏธ๋กœ์ฒ˜๋Ÿผ ๋ณ€ํ•ด๋ฒ„๋ฆฌ๊ณ  ๋ง์•˜๋‹ค. ๊ณต์‚ฌ ์ด์ „๊นŒ์ง€๋Š” ์–ด๋–ค ๊ฑด๋ฌผ์—์„œ ์ถœ๋ฐœํ•˜๋”๋ผ๋„ ๋‹ค๋ฅธ ๋ชจ๋“  ๊ฑด๋ฌผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ธธ์ด ์žˆ์—ˆ์œผ๋‚˜, ๊ณต

www.acmicpc.net


#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";        
    }

}

๋Œ“๊ธ€