fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6. const int MN = 1005;
  7. int n, k, bt[MN], dep[MN][MN], dp[MN];
  8.  
  9. int rec(int cur)
  10. {
  11. int& ret = dp[cur];
  12. if(ret) return ret;
  13. ret = bt[cur];
  14. for(int prev = 1; prev <= n; ++prev){
  15. if(dep[prev][cur]){
  16. ret = max(ret, rec(prev) + bt[cur]);
  17. }
  18. }
  19. return ret;
  20. }
  21.  
  22. int main()
  23. {
  24. int t;
  25. cin >> t;
  26. while(t--){
  27. cin >> n >> k;
  28. for(int i = 1; i <= n; ++i){
  29. dp[i] = 0;
  30. for(int j = 1; j <= n; ++j){
  31. dep[i][j] = 0;
  32. }
  33. }
  34.  
  35. for(int i = 1; i <= n; ++i){
  36. cin >> bt[i];
  37. }
  38.  
  39. for(int i = 0; i < k; ++i){
  40. int a, b;
  41. cin >> a >> b;
  42. dep[a][b] = 1;
  43. }
  44.  
  45. int target;
  46. cin >> target;
  47. cout << rec(target) << '\n';
  48. }
  49. }
Success #stdin #stdout 0s 7412KB
stdin
2
4 4
10 1 100 10
1 2
1 3
2 4
3 4
4
8 8
10 20 1 5 8 7 1 43
1 2
1 3
2 4
2 5
3 6
5 7
6 7
7 8
7
stdout
120
39