alexjercan/bugnet huggingface dataset:
Problem p01908 (C++) — buggy submission, verdict: Time Limit Exceeded

// 26^8>(4*10^5)^2???????????????8????????\\???.
// 26^5>(4*10^5)*8???????????????5????????\\???. ??¨??¨????????????.
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

vector<bool> is_exist[6];

int encode(string s) {
  int ret = 0;
  int mul = 1;

  for (int i = (int)s.length() - 1; i >= 0; i--) {
    ret += (s[i] - 'a') * mul;
    mul *= 26;
  }
  return ret;
}

string decode(int id, int length) {
  string str;
  int mul = 1;

  for (int i = length - 1; i >= 0; i--) {
    int val = (id / mul) % 26;
    str += (char)(val + 'a');
    mul *= 26;
  }

  string ret;
  for (int i = length - 1; i >= 0; i--) {
    ret += str[i];
  }

  return ret;
}

void input() {
  int n;

  cin >> n;

  is_exist[1].resize(26);
  is_exist[2].resize(26 * 26);
  is_exist[3].resize(26 * 26 * 26);
  is_exist[4].resize(26 * 26 * 26 * 26);
  is_exist[5].resize(26 * 26 * 26 * 26 * 26);
  for (int i = 1; i <= 5; i++) {
    for (int j = 0; j < is_exist[i].size(); j++) {
      is_exist[i][j] = false;
    }
  }

  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < s.length(); j++) {
      string t;
      for (int k = j; k < min((int)s.length(), j + 5); k++) {
        t += s[k];
        is_exist[t.length()][encode(t)] = true;
      }
    }
  }
}

int main() {
  input();

  for (int i = 1; i <= 5; i++) {
    for (int j = 0; j < is_exist[i].size(); j++) {
      if (!is_exist[i][j]) {
        cout << decode(j, i) << endl;
        return 0;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

// 26^8>(4*10^5)^2???????????????8????????\\???.
// 26^5>(4*10^5)*8???????????????5????????\\???. ??¨??¨????????????.
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

unordered_set<string> dict;

void input() {
  int n;

  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < s.length(); j++) {
      string t;
      for (int k = j; k < min((int)s.length(), j + 5); k++) {
        t += s[k];
        dict.insert(t);
      }
    }
  }
}

string ans = "zzzzz";

void dfs(string s) {
  if (dict.find(s) == dict.end() && s.length() >= 1) {
    if (ans.length() > s.length())
      ans = s;
  }
  if (s.length() >= ans.length()) {
    return;
  }

  for (char c = 'a'; c <= 'z'; c++) {
    dfs(s + c);
  }
}

int main() {
  input();
  dfs("");
  cout << ans << endl;
  return 0;
}
Problem p01908 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int main() {
  int N;
  cin >> N;
  set<string> dangers;
  for (int i = 0; i < N; ++i) {
    string st;
    cin >> st;
    for (int asize = 1; asize <= 4; ++asize) {
      for (int l = 0; l <= int(st.size()) - asize; ++l) {
        dangers.emplace(st.substr(l, asize));
      }
    }
  }
  int num = 1;
  bool flag = false;
  string ans = "dd";
  for (int asize = 1; asize <= 4; ++asize) {
    num *= 26;
    for (int j = 0; j < num; ++j) {
      string pass(asize, ' ');
      int n(j);
      for (int i = 0; i < asize; ++i) {
        pass[asize - i - 1] = n % 26 + 'a';
        n /= 26;
      }
      if (dangers.find(pass) == dangers.end()) {
        ans = pass;
        flag = true;
        break;
      }
    }
    if (flag)
      break;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int main() {
  int N;
  cin >> N;
  set<string> dangers;
  for (int i = 0; i < N; ++i) {
    string st;
    cin >> st;
    for (int asize = 1; asize <= 4; ++asize) {
      for (int l = 0; l <= st.size() - asize; ++l) {
        dangers.emplace(st.substr(l, asize));
      }
    }
  }
  int num = 1;
  bool flag = false;
  string ans = "dd";
  for (int asize = 1; asize <= 4; ++asize) {
    num *= 26;
    for (int j = 0; j < num; ++j) {
      string pass(asize, ' ');
      int n(j);
      for (int i = 0; i < asize; ++i) {
        pass[asize - i - 1] = n % 26 + 'a';
        n /= 26;
      }
      if (dangers.find(pass) == dangers.end()) {
        ans = pass;
        flag = true;
        break;
      }
    }
    if (flag)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p01908 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

const int width = 7;

bool dfs(const int len, unordered_set<string> &S, string pas) {
  if ((int)pas.size() >= len) {
    if (S.count(pas) == 0) {
      cout << pas << endl;
      return true;
    }
    return false;
  }
  rep(i, 26) {
    string npas = pas + string(1, (char)('a' + i));
    if (dfs(len, S, npas))
      return true;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  unordered_set<string> S;
  rep(i, n) {
    string s;
    cin >> s;
    rep(j, (int)s.size()) {
      rep(k, width) {
        if (j + k >= (int)s.size())
          break;
        S.insert(s.substr(j, k + 1));
      }
    }
  }
  REP(len, 1, width) if (dfs(len, S, "")) break;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

const int width = 20;

bool dfs(const int len, unordered_set<string> &S, string pas) {
  if ((int)pas.size() >= len) {
    if (S.count(pas) == 0) {
      cout << pas << endl;
      return true;
    }
    return false;
  }
  rep(i, 26) {
    string npas = pas + string(1, (char)('a' + i));
    if (dfs(len, S, npas))
      return true;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  unordered_set<string> S;
  rep(i, n) {
    string s;
    cin >> s;
    rep(j, (int)s.size()) {
      rep(k, width) {
        if (j + k >= (int)s.size())
          break;
        S.insert(s.substr(j, k + 1));
      }
    }
  }
  REP(len, 1, width) if (dfs(len, S, "")) break;
  return 0;
}
Problem p01908 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define MAX_L 8
#define MAX_N 100005
typedef long long ll;
ll mod = 1e9 + 7;
ll B = 5575777;

int N;
string S[MAX_N];
unordered_set<ll> t[MAX_L + 1];
char tmp[MAX_L + 5];

bool dfs(ll hash, int depth, int anslen) {
  if (depth == anslen) {
    if (t[anslen].count(hash) == 1)
      return false;
    for (int i = 0; i < depth; i++)
      cout << tmp[i];
    cout << endl;
    return true;
  }

  for (char ch = 'a'; ch <= 'z'; ch++) {
    tmp[depth] = ch;
    ll nhash = (hash * B + ch) % mod;
    if (dfs(nhash, depth + 1, anslen))
      return true;
  }
  return false;
}

void solve() {
  for (int i = 0; i < N; i++) {
    int size = S[i].size();

    for (int j = 1; j <= MAX_L; j++) {

      ll key = 0, nb = 1;
      for (int k = 0; k < size; k++) {
        key = (key * B + S[i][k]) % mod;
        if (k - j >= 0) {
          key += (mod - (S[i][k - j] * nb) % mod);
          key %= mod;
        } else
          nb = (nb * B) % mod;
        if (k + 1 >= j)
          t[j].insert(key);
      }
    }
  }

  ll cnt = 1;
  int anslen = 1;
  for (int i = 1; i <= MAX_L; i++) {
    cnt *= 26;
    if ((ll)t[i].size() < cnt) {
      anslen = i;
      break;
    }
  }

  dfs(0, 0, anslen);
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> S[i];
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX_L 8
#define MAX_N 100005
typedef long long ll;
ll mod = 1e9 + 7;
ll B = 5575777;

int N;
string S[MAX_N];
set<ll> t[MAX_L + 1];
char tmp[MAX_L + 5];

bool dfs(ll hash, int depth, int anslen) {
  if (depth == anslen) {
    if (t[anslen].count(hash) == 1)
      return false;
    for (int i = 0; i < depth; i++)
      cout << tmp[i];
    cout << endl;
    return true;
  }

  for (char ch = 'a'; ch <= 'z'; ch++) {
    tmp[depth] = ch;
    ll nhash = (hash * B + ch) % mod;
    if (dfs(nhash, depth + 1, anslen))
      return true;
  }
  return false;
}

void solve() {
  for (int i = 0; i < N; i++) {
    int size = S[i].size();

    for (int j = 1; j <= MAX_L; j++) {

      ll key = 0, nb = 1;
      for (int k = 0; k < size; k++) {
        key = (key * B + S[i][k]) % mod;
        if (k - j >= 0) {
          key += (mod - (S[i][k - j] * nb) % mod);
          key %= mod;
        } else
          nb = (nb * B) % mod;
        if (k + 1 >= j)
          t[j].insert(key);
      }
    }
  }

  ll cnt = 1;
  int anslen = 1;
  for (int i = 1; i <= MAX_L; i++) {
    cnt *= 26;
    if ((ll)t[i].size() < cnt) {
      anslen = i;
      break;
    }
  }

  dfs(0, 0, anslen);
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> S[i];
  solve();
  return 0;
}
Problem p01908 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
const ll INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

set<string> pass_list;

string solve(string now, int length) {
  assert(now.size() <= 4);

  if (now.size() == length) {
    return pass_list.count(now) ? "" : now;
  }

  rep(i, 26) {
    string tmp = solve(now + (char)('a' + i), length);
    if (tmp.size() > 0)
      return tmp;
  }

  return "";
}

int main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<string> S(N);
  rep(i, N) cin >> S[i];

  for (auto s : S) {
    rep(i, s.size()) {
      string tmp;
      rep(j, 4) {
        if (i + j < (int)s.size()) {
          tmp += s[i + j];
          pass_list.insert(tmp);
        }
      }
    }
  }

  rep(i, 4) {
    string tmp = solve("", i + 1);
    if (tmp != "") {
      cout << tmp << endl;
      break;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y)                                                            \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl
#define BINARY(x) static_cast<bitset<16>>(x)

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)

#define in_range(x, y, w, h)                                                   \\
  (0 <= (int)(x) && (int)(x) < (int)(w) && 0 <= (int)(y) && (int)(y) < (int)(h))
#define ALL(a) (a).begin(), (a).end()

typedef long long ll;
const int INF = 1e9;
const ll INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};

int main() {
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<string> S(N);
  rep(i, N) cin >> S[i];

  set<string> list;

  for (auto s : S) {
    rep(i, s.size()) {
      string tmp;
      rep(j, 4) {
        if (i + j < (int)s.size()) {
          tmp += s[i + j];
          list.insert(tmp);
        }
      }
    }
  }

  string ans = "a";
  while (true) {
    if (list.count(ans)) {
      if (ans.back() != 'z') {
        ans.back() = (char)((int)ans.back() + 1);
      } else {
        ans.back() = 'a';
        ans += "a";
      }

      assert(ans.size() <= 4);
    } else {
      cout << ans << endl;
      break;
    }
  }
}
Problem p01908 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;

  cin >> N;

  int such[7];
  such[0] = 1;
  for (int i = 1; i < 7; i++)
    such[i] = such[i - 1] * 26;

  set<string> st[6];

  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    for (int k = 0; k < S.size(); k++) {
      string FF;
      for (int l = 0; l < 5; l++) {
        if (k + l >= S.size())
          break;
        FF += S[k + l];
        if (FF.size() == such[FF.size()])
          continue;
        st[FF.size()].insert(FF);
      }
    }
  }

  for (int i = 1; i < 7; i++) {
    if (st[i].size() < such[i]) {
      string minimum = string(i, 'a');
      for (auto &s : st[i]) {
        if (minimum == s) {
          for (int j = minimum.size() - 1; j >= 0; j--) {
            minimum[j] = (minimum[j] - 'a' + 1) % 26 + 'a';
            if (minimum[j] != 'a')
              break;
          }
        } else {
          cout << minimum << endl;
          return (0);
        }
      }
      cout << minimum << endl;
      return (0);
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;

  cin >> N;

  int such[7];
  such[0] = 1;
  for (int i = 1; i < 7; i++)
    such[i] = such[i - 1] * 26;

  set<string> st[7];

  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    for (int k = 0; k < S.size(); k++) {
      string FF;
      for (int l = 0; l < 6; l++) {
        if (k + l >= S.size())
          break;
        FF += S[k + l];
        if (FF.size() == such[FF.size()])
          continue;
        st[FF.size()].insert(FF);
      }
    }
  }

  for (int i = 1; i < 7; i++) {
    if (st[i].size() < such[i]) {
      string minimum = string(i, 'a');
      for (auto &s : st[i]) {
        if (minimum == s) {
          for (int j = minimum.size() - 1; j >= 0; j--) {
            minimum[j] = (minimum[j] - 'a' + 1) % 26 + 'a';
            if (minimum[j] != 'a')
              break;
          }
        } else {
          cout << minimum << endl;
          return (0);
        }
      }
      cout << minimum << endl;
      return (0);
    }
  }
}
Problem p01908 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;

  cin >> N;

  int such[7];
  such[0] = 1;
  for (int i = 1; i < 7; i++)
    such[i] = such[i - 1] * 26;

  set<string> st[6];

  for (int i = 0; i < N; i++) {
    string S;
    cin >> S;
    for (int k = 0; k < S.size(); k++) {
      string FF;
      for (int l = 0; l < 5; l++) {
        if (k + l >= S.size())
          break;
        FF += S[k + l];
        if (FF.size() == such[FF.size()])
          continue;
        st[FF.size()].insert(FF);
      }
    }
  }

  for (int i = 1; i < 7; i++) {
    if (st[i].size() < such[i]) {
      string minimum = string(i, 'a');
      for (auto &s : st[i]) {
        if (minimum == s) {
          for (int j = minimum.size() - 1; j >= 0; j--) {
            minimum[j] = (minimum[j] - 'a' + 1) % 26 + 'a';
            if (minimum[j] != 'a')
              break;
          }
        } else {
          cout << minimum << endl;
          return (0);
        }
      }
      cout << minimum << endl;
      return (0);
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<string> t;

void make(string s, int idx) {
  if (!s.empty())
    t.push_back(s);
  if (idx == 3)
    return;
  for (char i = 'a'; i <= 'z'; i++)
    make(s + i, idx + 1);
}

int main() {
  int N;
  string S[100000];

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }

  make("", 0);

  string ret = "~~~~~";
  for (string &s : t) {
    bool correct = true;
    for (int k = 0; k < N; k++) {
      for (int i = 0; i <= (int)S[k].size() - (int)s.size(); i++) {
        bool match = true;
        for (int j = 0; j < s.size(); j++) {
          match &= s[j] == S[k][j + i];
          if (!match)
            break;
        }
        correct &= match ^ 1;
        if (!correct)
          break;
      }
    }
    if (correct &&
        (s.size() < ret.size() || (s.size() == ret.size() && s < ret))) {
      ret = s;
    }
  }

  cout << ret << endl;
}
Problem p01910 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1LL << 60)

// verified AOJ GRL_1
#define MAX_V 100020
template <typename T> struct edge {
  int to;
  T cost;
};

template <typename T>
void dijkstra(int s, vector<T> &d, vector<edge<T>> G[MAX_V]) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  rep(i, d.size()) d[i] = INF;
  d[s] = 0;
  que.push(pii(0, s));

  while (que.size()) {
    pii p = que.top();
    que.pop();

    int v = p.second;
    if (d[v] < p.first)
      continue;

    rep(i, G[v].size()) {
      edge<T> e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

signed main() {
  int n, m, r, q;
  cin >> n >> m >> r >> q;
  vector<edge<int>> G[MAX_V];

  rep(i, m) {
    int s, t, c;
    cin >> s >> t >> c;
    G[t].pb(edge<int>{s, c});
  }
  for (int i = 1; i <= n; i++) {
    G[n + 1 + i % r].pb(edge<int>{i, 0});
  }

  static vector<vector<int>> d(r, vector<int>(n + r + 1));
  for (int i = 0; i < r; i++) {
    dijkstra<int>(n + i + 1, d[i], G);
  }

  int ans = 0;
  rep(i, q) {
    int x, y, z;
    cin >> x >> y >> z;
    int mini = INF;
    rep(j, r) { mini = min(mini, d[j][x] + d[j][y]); }
    if (z - mini > 0)
      ans += z - mini;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1LL << 60)

// verified AOJ GRL_1
#define MAX_V 100020
template <typename T> struct edge {
  int to;
  T cost;
};

template <typename T>
void dijkstra(int s, vector<T> &d, vector<edge<T>> G[MAX_V]) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  rep(i, d.size()) d[i] = INF;
  d[s] = 0;
  que.push(pii(0, s));

  while (que.size()) {
    pii p = que.top();
    que.pop();

    int v = p.second;
    if (d[v] < p.first)
      continue;

    rep(i, G[v].size()) {
      edge<T> e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

signed main() {
  int n, m, r, q;
  cin >> n >> m >> r >> q;
  vector<edge<int>> G[MAX_V];

  rep(i, m) {
    int s, t, c;
    cin >> s >> t >> c;
    G[t].pb(edge<int>{s, c});
  }
  for (int i = 1; i <= n; i++) {
    G[n + 1 + i % r].pb(edge<int>{i, 0});
  }

  static vector<vector<int>> d(r, vector<int>(n + r));
  for (int i = 0; i < r; i++) {
    dijkstra<int>(n + i + 1, d[i], G);
  }

  int ans = 0;
  rep(i, q) {
    int x, y, z;
    cin >> x >> y >> z;
    int mini = INF;
    rep(j, r) { mini = min(mini, d[j][x] + d[j][y]); }
    if (z - mini > 0)
      ans += z - mini;
  }
  cout << ans << endl;
}
Problem p01910 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define cost first
#define to second
typedef long long ll;
typedef pair<ll, int> edge;

#define MAX 100001
vector<edge> G[MAX];
ll dp[MAX][10];
priority_queue<edge, vector<edge>, greater<edge>> qu;

int main() {
  int N, M, R, Q, a, b, c;
  cin >> N >> M >> R >> Q;
  rep(i, M) {
    cin >> a >> b >> c;
    G[b].push_back({c, a});
  }
  rep1(i, N) rep(r, R) dp[i][r] = LLONG_MAX / 4;
  rep(r, R) {
    rep1(i, N) {
      if (i % R == r) {
        dp[i][r] = 0;
        qu.push({0, i});
      }
    }
    while (qu.size()) {
      edge v = qu.top();
      qu.pop();
      if (dp[v.to][r] < v.cost)
        continue;
      for (auto e : G[v.to]) {
        if (dp[e.to][r] > dp[v.to][r] + e.cost) {
          dp[e.to][r] = dp[v.to][r] + e.cost;
          qu.push({dp[e.to][r], e.to});
        }
      }
    }
  }

  ll res = 0;
  rep(_, Q) {
    int x, y, z;
    cin >> x >> y >> z;
    ll get = 0;
    rep(r, R) { get = max(get, z - (dp[x][r] + dp[y][r])); }
    res += get;
  }
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define cost first
#define to second
typedef long long ll;
typedef pair<ll, int> edge;

#define MAX 200001
vector<edge> G[MAX];
ll dp[MAX][10];
priority_queue<edge> qu;

int main() {
  int N, M, R, Q, a, b, c;
  cin >> N >> M >> R >> Q;
  rep(i, M) {
    cin >> a >> b >> c;
    G[b].push_back({c, a});
  }
  rep1(i, N) rep(r, R) dp[i][r] = LLONG_MAX;
  rep(r, R) {
    rep1(i, N) {
      if (i % R == r) {
        dp[i][r] = 0;
        qu.push({0, i});
      }
    }
    while (qu.size()) {
      edge v = qu.top();
      qu.pop();
      if (dp[v.to][r] < v.cost)
        continue;
      for (auto e : G[v.to]) {
        if (dp[e.to][r] > dp[v.to][r] + e.cost) {
          dp[e.to][r] = dp[v.to][r] + e.cost;
          qu.push({e.to, dp[e.to][r]});
        }
      }
    }
  }

  ll res = 0;
  rep(_, Q) {
    int x, y, z;
    cin >> x >> y >> z;
    ll get = 0;
    rep(r, R) { get = max(get, z - (dp[x][r] + dp[y][r])); }
    res += get;
  }
  cout << res << endl;
  return 0;
}