fork download
  1. // ~~ icebear love attttt ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define BIT(S, i) (((S) >> (i)) & 1)
  27. #define mp make_pair
  28. #define pb push_back
  29. #define fi first
  30. #define se second
  31. #define all(x) x.begin(), x.end()
  32. #define task "robot"
  33.  
  34. const int MOD = 1e9 + 7;
  35. const int inf = 1e9 + 27092008;
  36. const ll INF = 1e18 + 27092008;
  37. const int N = 2e5 + 5;
  38. int n, m, s, k;
  39. vector<ii> G[N];
  40. int score[N];
  41. bool vis[N], vis2[N];
  42. ll cur_sum;
  43.  
  44. int test(int u) {
  45. priority_queue<ii, vector<ii>, greater<ii>> Q;
  46. vector<int> nodes;
  47. Q.push(mp(0, u));
  48. int ans = 0;
  49. while(!Q.empty()) {
  50. int w, u; tie(w, u) = Q.top(); Q.pop();
  51. if (w > cur_sum) break;
  52. if (vis2[u]) continue;
  53. vis2[u] = true;
  54. nodes.pb(u);
  55. for(ii x : G[u]) if (!vis[x.fi] && !vis2[x.fi]) {
  56. Q.push(mp(x.se, x.fi));
  57. }
  58. }
  59.  
  60. for(int u : nodes) vis2[u] = false;
  61. return (int)nodes.size();
  62. }
  63.  
  64. void init(void) {
  65. cin >> n >> m >> s >> k;
  66. FOR(i, 1, n) cin >> score[i];
  67. FOR(i, 1, m) {
  68. int u, v, w;
  69. cin >> u >> v >> w;
  70. G[u].pb(mp(v, w));
  71. G[v].pb(mp(u, w));
  72. }
  73. }
  74.  
  75. void process(void) {
  76. priority_queue<ii, vector<ii>, greater<ii>> Q;
  77. Q.push(mp(0, s));
  78. while(!Q.empty()) {
  79. int u, w; tie(w, u) = Q.top();
  80. Q.pop();
  81. if (vis[u]) continue;
  82. if (w > cur_sum) break;
  83. cur_sum += score[u];
  84. vis[u] = true;
  85. for(ii x : G[u]) if (!vis[x.fi]) {
  86. Q.push(mp(x.se, x.fi));
  87. }
  88. }
  89. int ans = count(vis, vis + n + 1, true), add = 0;
  90. if (k == 1) {
  91. while(!Q.empty()) {
  92. int u = Q.top().se; Q.pop();
  93. maximize(add, test(u));
  94. }
  95. }
  96. cout << ans + add;
  97. }
  98.  
  99. int main() {
  100. ios_base::sync_with_stdio(0);
  101. cin.tie(0); cout.tie(0);
  102. if (fopen(task".inp", "r")) {
  103. freopen(task".inp", "r", stdin);
  104. freopen(task".out", "w", stdout);
  105. }
  106. int tc = 1;
  107. // cin >> tc;
  108. while(tc--) {
  109. init();
  110. process();
  111. }
  112. return 0;
  113. }
  114.  
Success #stdin #stdout 0.01s 9256KB
stdin
Standard input is empty
stdout
1