๐ฆ1005: ACM_Craft
[์๊ณ ๋ฆฌ์ฆ ํ์ด] ๊ฐ๋๊ท
Reviewed by Kade Kang (devkade12@gmail.com)
Reviewed:: 2024-02-20
๋ฌธ์ ํ์ธํ๊ธฐ
ํ์ด
- ์์ ์ ๋ ฌ ๋ฌธ์ ์ด๋ค.
- ๊ฑด์ค ์์๊ฐ ์ฃผ์ด์ง๊ธฐ ๋๋ฌธ์ Indegree๋ฅผ ํตํด์ ์์๋ฅผ ๋ค๋ก ๋ฏธ๋ค์ค๋ค.
- Res[i][j] ์ i ๋ฒ์งธ ๊ฑด๋ฌผ์ ๊ฑด์ค ์๋ฃํ๋๋ฐ ๋ฐ์ํ๋ ์ต์ ์๊ฐ์ ๋งํ๋ค.
- Res์ ์ ๋ฐ์ดํธ ํ ๋๋ ๋ฌธ์ ์์ ์ ์ํ ๋๋ก ๋ฆ๋ ๊ฑด๋ฌผ์ ์๊ฐ์ ๋ง์ถฐ์ผ ํ๊ธฐ ๋๋ฌธ์ ์์ ์๊ฐ์ด ๋ ํฐ ๊ฑด๋ฌผ๋ก ๊ณ์ฐํ๋ค.
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#define endl '\n'
using namespace std;
int T, N, K, a, b, goal;
int Time[1001];
vector<int> Sequence[1001];
int Indegree[1001];
int Res[1001];
void Input(){
cin >> N >> K;
for(int i = 1; i <= N; i++){
cin >> Time[i];
}
for(int i = 0; i < K; i++){
cin >> a >> b;
Sequence[a].push_back(b);
Indegree[b]++;
}
cin >> goal;
}
void Initialize(){
for (int i = 0; i < N; i++) Sequence[i].clear();
memset(Time, 0, sizeof(Time));
memset(Res, 0, sizeof(Res));
memset(Indegree, 0, sizeof(Indegree));
}
void Solution(){
queue<int> q;
for(int i = 1; i <= N; i++){
if(Indegree[i]==0){
q.push(i);
Res[i] = Time[i];
}
}
while(!q.empty()){
int cur = q.front(); q.pop();
for(auto nxt : Sequence[cur]){
Res[nxt] = (Res[nxt] < Res[cur]+Time[nxt]) ? Res[cur]+Time[nxt] : Res[nxt];
Indegree[nxt]--;
if(Indegree[nxt]==0){
q.push(nxt);
}
}
}
cout << Res[goal] << endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> T;
while(T--){
Input();
Solution();
Initialize();
}
return 0;
}