-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj1005.cpp
More file actions
52 lines (45 loc) · 936 Bytes
/
boj1005.cpp
File metadata and controls
52 lines (45 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
int t, n, k, w;
int main()
{
scanf("%d", &t);
while(t--){
scanf("%d %d", &n, &k);
vector<int>len[n+1], val(n+1,0), len_cnt(n+1,0), MAX(n+1,-1);
queue<pair<int,int> >q;
for(int i = 1; i <= n; i++){
scanf("%d", &val[i]);
}
for(int i = 0, a, b; i < k; i++){
scanf("%d %d", &a, &b);
len[a].push_back(b);
len_cnt[b]++;
}
scanf("%d", &w);
for(int i = 1; i <= n; i++){
if(len_cnt[i] == 0){
q.push(make_pair(i,val[i]));
}
}
while(!q.empty()){
int now = q.front().first;
int time = q.front().second;
q.pop();
if(now == w){
printf("%d\n", time);
break;
}
for(int i = 0; i < len[now].size(); i++){
int next = len[now][i];
MAX[next] = max(MAX[next], time+val[next]);
if(--len_cnt[next] == 0){
q.push(make_pair(next,MAX[next]));
}
}
}
}
}