2026-02-26 20:19:48 +09:00

60 lines
1.7 KiB
Java

import java.util.*;
public class _1005_1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
int[] ans = new int[T];
for(int t=0; t<T; t++) {
int N = sc.nextInt(), K = sc.nextInt();
int[] build_time = new int[N+1];
for(int i=1; i<=N; i++) build_time[i] = sc.nextInt();
int[] indegree = new int[N+1];
int[][] graph = new int[N+1][N+1];
for(int i=0; i<K; i++) {
int X = sc.nextInt(), Y = sc.nextInt();
indegree[Y]++;
graph[X][++graph[X][0]] = Y;
}
int W = sc.nextInt();
int[] complete_time = new int[N+1];
int[] qu = new int[N];
int front = 0, rear = 0;
for(int i=1; i<=N; i++) {
if(indegree[i] == 0) {
qu[rear++] = i;
complete_time[i] = build_time[i];
}
}
while(front <= rear) {
int now = qu[front++];
if(now == W) break;
for(int i=1; i<=graph[now][0]; i++) {
int next = graph[now][i];
indegree[next]--;
complete_time[next] = Math.max(complete_time[now] + build_time[next], complete_time[next]);
if(indegree[next] == 0) {
qu[rear++] = next;
}
}
}
ans[t] = complete_time[W];
}
sc.close();
for (int n : ans) {
System.out.println(n);
}
}
}