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

[๋ฐฑ์ค€,c++] 1005๋ฒˆ - ACM Craft

by dkswnkk 2021. 10. 16.
 

1005๋ฒˆ: ACM Craft

์ฒซ์งธ ์ค„์—๋Š” ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฃผ์–ด์ง„๋‹ค. ์ฒซ์งธ ์ค„์— ๊ฑด๋ฌผ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฑด๋ฌผ๊ฐ„์˜ ๊ฑด์„ค์ˆœ์„œ ๊ทœ์น™์˜ ์ด ๊ฐœ์ˆ˜ K์ด ์ฃผ์–ด์ง„๋‹ค. (๊ฑด๋ฌผ์˜ ๋ฒˆํ˜ธ๋Š” 1๋ฒˆ๋ถ€

www.acmicpc.net

 

//  Copyright © 2021 ์•ˆ์ฃผํ˜•. All rights reserved.
//  https://www.acmicpc.net/problem/1005
//  BOJ1005 ACM Craft
#include <iostream>
#include <queue>
#include <memory.h>

using namespace std;

int N, K,W; //N=๊ฑด๋ฌผ์˜ ๊ฐœ์ˆ˜ , K=๊ฑด์„ค์ˆœ์„œ ๊ทœ์น™ ๊ฐœ์ˆ˜, W=์Šน๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ๊ฑด์„คํ•ด์•ผ ํ•  ๊ฑด๋ฌผ
int indegree[1001];
int time[1001];
int totalTime[1001];
vector<int>graph[1001];

void itit() {
    memset(time, 0, sizeof(time));
    memset(totalTime, 0, sizeof(totalTime));
    memset(indegree, 0, sizeof(indegree));
    for (int i = 1; i <= N; i++) {
        graph[i].clear();
    }
}
void topologySort() {    //์œ„์ƒ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ˆ˜ํ–‰
    queue<int>q;
    for (int i = 1; i <= N; i++) {    //์ฒ˜์Œ ์‹œ์ž‘ํ•  ๋•Œ๋Š” ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ ๋…ธ๋“œ๋ฅผ ํ์— ์‚ฝ์ž…
        totalTime[i] = time[i];
        if (indegree[i] == 0) q.push(i);        
    }
    while (!q.empty()) {
        if (indegree[W] == 0) break;
        int now = q.front();
        q.pop();
        for (int i = 0; i < graph[now].size(); i++) {    
            indegree[graph[now][i]]--;    // ํ•ด๋‹น ์›์†Œ์™€ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์˜ ์ง„์ž…์ฐจ์ˆ˜์—์„œ 1๋นผ๊ธฐ
            totalTime[graph[now][i]] = max(totalTime[graph[now][i]], time[graph[now][i]] +totalTime[now]);
            if (indegree[graph[now][i]] == 0) q.push(graph[now][i]); //์ƒˆ๋กญ๊ฒŒ ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ด ๋˜๋Š” ๋…ธ๋“œ๋ฅผ ํ์— ์‚ฝ์ž…

        }
    }
    cout << totalTime[W]<<"\n";
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int tk; cin >> tk;
    while (tk--) {
        cin >> N >> K;
        for (int i = 1; i <= N; i++) {    //๊ฑด์„ค์— ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ ์ž…๋ ฅ
            int t; cin >> t;
            time[i] = t;
        }
        for (int i = 0; i < K; i++) {
            int a, b; cin >> a >> b;
            graph[a].push_back(b);    //    ์ •์  A์—์„œ B๋กœ ์ด๋™๊ฐ€๋Šฅ
            indegree[b] += 1;    //์ง„์ž…์ฐจ์ˆ˜ 1 ์ฆ๊ฐ€
        }
        cin >> W;

        topologySort();
        itit();    //ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์—ฌ๋Ÿฌ๊ฐœ์ผ ์ˆ˜ ์žˆ์œผ๋‹ˆ ์ดˆ๊ธฐํ™”
    }
}

๋Œ“๊ธ€