Problem:
F: カードゲーム
問題
カードを使ったゲームを $Q$ 回行います。
カードには $1 \cdots N$ の数が書かれており、各数が書かれたカードはゲームを行うのに十分な枚数があります。
$i$ 回目のゲームでは、はじめに手札として 2 枚のカードが配られます。
それぞれのカードに書かれている数字は $x_i$ と $y_i$ です。
カードはルールに従って交換することができます。
$j$ 番目 $(1 \le j \le M)$ のルールでは、カード $a_j$ を手数料 $c_j$ 円で別のカード $b_j$ に交換することができます。
各ルールは何回でも用いることができます。また、手数料が足りなくて交換できない場合はありません。
最後に、手札のカードの数字を $R$ で割ったあまりが等しい時、報酬として $z_i$ 円受け取ります。
異なる場合報酬は $0$ 円です。
$Q$ 回ゲームを終えた時に増やすことのできるお金の最大値を求めてください。
制約
$1 \le N \le 10^5$
$0 \le M \le \min(2 \times 10^5, N\times(N-1))$
$2 \le R \le 10$
$1 \le Q \le 10^5$
$1 \le a_j, b_j \le N$
$a_j \neq b_j$
$0 \le c_j \le 10^5$
$1 \le x_i, y_i \le N$
$0 \le z_i \le 10^5$
入力
入力は以下の形式で標準入力から与えられます。
$N \ M \ R \ Q$
$a_1 \ b_1 \ c_1$
$\vdots$
$a_M \ b_M \ c_M$
$x_1 \ y_1 \ z_1$
$\vdots$
$x_Q \ y_Q \ z_Q$
出力
ゲームを $Q$ 回行ったときに増やすことのできるお金の最大値を 1 行で出力してください。また、末尾に改行も出力してください。
サンプル
入力例 1
4 4 2 2
1 2 1
2 3 1
3 4 5
4 1 7
1 4 5
2 4 3
出力例 1
7
1 回目のゲームではカード 1 を手数料 1 でカード 2 に交換すると手札のカードを 2 で割った時のあまりがどちらも 0 となり、得られる報酬は 5 となります。
2 回目のゲームではすでに 2 で割ったときのあまりがどちらも 0 であり、得られる報酬は 3 となります。
2 回のゲームの結果、増やせるお金は $5-1+3=7$ です。
入力例 2
4 4 2 1
1 2 1
2 3 1
3 4 5
4 1 7
3 4 2
出力例 2
0
カードの交換での手数料が報酬よりも多いので、交換を行わないのが最適です。

Submitted solutions:

code_snippet: // AOJ #2810 Card Game
// 2025.4.23

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18;

#define gc() getchar_unlocked()

int Cin() {
	int n = 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

int main(){
    int N = Cin(), M = Cin(), R = Cin(), Q = Cin();

    vector<vector<pair<int,int>>> G(N+1);
    for(int i = 0; i < M; i++){
        int a = Cin(), b = Cin(), c = Cin();
        G[b].emplace_back(a, c);
    }
    vector<vector<ll>> dist(R, vector<ll>(N+1, INF));
    for(int r = 0; r < R; r++){
        priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<>> pq;
        for(int v = r == 0 ? R : r; v <= N; v += R){
            dist[r][v] = 0;
            pq.emplace(0, v);
        }
        while(!pq.empty()){
            auto [d, v] = pq.top(); pq.pop();
            if(d > dist[r][v]) continue;
            for(auto &e : G[v]){
                int u = e.first, w = e.second;
                if(dist[r][u] > d + w){
                    dist[r][u] = d + w;
                    pq.emplace(dist[r][u], u);
                }
            }
        }
    }

    ll ans = 0;
    for(int i = 0; i < Q; i++){
        int x = Cin(), y = Cin();
        ll z = Cin();
        ll best = 0;
        for(int r = 0; r < R; r++){
            ll cx = dist[r][x], cy = dist[r][y];
            if(cx == INF || cy == INF) continue;
            ll cost = cx + cy;
            if(cost <= z) best = max(best, z - cost);
        }
        ans += best;
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 17116
final_rank: 1

code_snippet: //#include<atcoder/all>
//using namespace atcoder;

#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define eps (double)(1e-9)
#define pi (double) acos(-1)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

struct edge{
    ll to, cost;
};

using Graph = vector<vector<edge>>;

#define P pair<ll, ll>
vector<ll> dijkstra(const vector<vector<edge>> &G, int s) {
    vector<ll> d((int)G.size(), inf);
	priority_queue<P, vector<P>, greater<P>>que;
	d[s] = 0;
	que.push(P(0, s));
	while (!que.empty()) {
		P p = que.top(); que.pop();
		int v = p.second;
		if (d[v] < p.first)continue;
		rep(i, G[v].size()) {
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost) {
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
	return d;
}

int main(){
    int n, m, r, q;
    cin >> n >> m >> r >> q;
    vector<ll> a(m), b(m), c(m);
    vector<ll> x(q), y(q), z(q);
    rep(i, m)cin >> a[i] >> b[i] >> c[i];
    rep(i, q)cin >> x[i] >> y[i] >> z[i];
    rep(i, m){a[i]--; b[i]--;}

    Graph G(n+r);
    rep(i, m)G[b[i]].push_back({a[i], c[i]});
    rep(i, n)G[n+i%r].push_back({i, 0});

    vector<vector<ll>> d(r);
    rep(i, r)d[i] = dijkstra(G, n+i);

    ll ans = 0;
    rep(i, q){
        x[i]--;
        y[i]--;
        ll now = inf;
        rep(j, r){
            chmin(now, d[j][x[i]] + d[j][y[i]]);
        }

        ans += max(0LL, z[i] - now);
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 200
memory_kb: 23304
final_rank: 10

code_snippet: // #ifndef ONLINE_JUDGE
#if __has_include("all.h")

#include "all.h"

#else

#include <bits/extc++.h>

// #include <atcoder/all>

#endif

using ll = long long int;

template <class T>
bool chmin(T &x, const T val) {
  if (x > val) {
    x = val;
    return true;
  } else {
    return false;
  }
}

template <class T>
bool chmax(T &x, const T val) {
  if (x < val) {
    x = val;
    return true;
  } else {
    return false;
  }
}

template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class... T>
std::istream &operator>>(std::istream &is, std::tuple<T...> &tpl) {
  std::apply([&](auto &&...args) { (is >> ... >> args); }, tpl);
  return is;
}

template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
  for (T &x : v) is >> x;
  return is;
}

// template <class mint, atcoder::internal::is_static_modint_t<mint> * =
// nullptr> std::ostream &operator<<(std::ostream &os, const mint &v) {
//   return os << v.val();
// }

template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  for (int i = 0; i < v.size(); i++)
    os << v[i] << (i == v.size() - 1 ? "" : " ");
  return os;
}

struct Initialization {
  Initialization() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
  }
} initialization;

constexpr std::pair<int, int> dir[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

template <typename T>
using infs = std::numeric_limits<T>;

template <typename T>
class factorials {
 public:
  static size_t n;
  static std::vector<T> fact, inv_fact;

  static void extend(size_t m) {
    if (m <= n) return;
    fact.resize(m + 1);
    inv_fact.resize(m + 1);
    for (size_t i = n + 1; i <= m; i++) fact[i] = fact[i - 1] * i;
    inv_fact[m] = fact[m].inv();
    for (size_t i = m; i > n + 1; i--) inv_fact[i - 1] = inv_fact[i] * i;
    n = m;
  }

  static T inv(int k) {
    extend(k);
    return inv_fact[k];
  }

  static T get(int k) {
    extend(k);
    return fact[k];
  }

  static T perm(int n, int k) {
    if (n < k) return 0;
    if (k < 0) return 0;
    extend(n);
    return fact[n] * inv_fact[n - k];
  }

  static T choose(int n, int k) {
    if (n < k) return 0;
    if (k < 0) return 0;
    extend(n);
    return fact[n] * inv_fact[n - k] * inv_fact[k];
  }
};

template <typename T>
size_t factorials<T>::n = 0;

template <typename T>
std::vector<T> factorials<T>::fact = {1};

template <typename T>
std::vector<T> factorials<T>::inv_fact = {1};

// template <typename T>
// class fps {
//   std::vector<T> v;
//
//  public:
//   using value_type = T;
//   using reference = T &;
//   using const_reference = const T &;
//   using iterator = typename std::vector<T>::iterator;
//   using const_iterator = typename std::vector<T>::const_iterator;
//
//   size_t size() const { return v.size(); }
//
//   const std::vector<T> &data() const { return v; }
//
//   explicit fps(int n) : v(n) {}
//
//   fps(const std::vector<T> &v) : v(v) {}
//   fps(std::vector<T> &&v) : v(v) {}
//
//   template <class InputIterator>
//   fps(InputIterator first, InputIterator last) : v(first, last) {}
//
//   void resize(int n) { v.resize(n); }
//
//   T &operator[](int i) { return v[i]; }
//
//   iterator begin() { return v.begin(); }
//
//   iterator end() { return v.end(); }
//
//   fps diff() {
//     std::vector<T> res(v.size() - 1);
//     for (int i = 0; i < res.size(); i++) res[i] = v[i + 1] * (i + 1);
//     return fps(res);
//   }
//
//   fps integral() {
//     std::vector<T> res(v.size() + 1);
//     for (int i = 0; i < v.size(); i++) res[i + 1] = v[i] / (i + 1);
//     return fps(res);
//   }
//
//   fps inv(int deg = -1) {
//     assert(v[0] != 0);
//
//     if (deg == -1) deg = size();
//     std::vector<T> res(deg);
//
//     res[0] = v[0].inv();
//
//     for (int d = 1; d < deg; d <<= 1) {
//       std::vector<T> f(2 * d), g(2 * d);
//
//       std::copy(v.begin(), v.begin() + std::min(2 * d, (int)v.size()),
//                 std::back_inserter(f));
//       std::copy(res.begin(), res.begin() + d, std::back_inserter(g));
//
//       atcoder::internal::butterfly(f);
//       atcoder::internal::butterfly(g);
//
//       for (int i = 0; i < 2 * d; i++) f[i] *= g[i];
//
//       atcoder::internal::butterfly_inv(f);
//
//       for (int i = 0; i < d; i++) f[i] = 0;
//
//       atcoder::internal::butterfly(f);
//
//       for (int i = 0; i < 2 * d; i++) f[i] *= g[i];
//
//       atcoder::internal::butterfly_inv(f);
//
//       for (int i = d; i < std::min(2 * d, deg); i++) res[i] = -f[i];
//     }
//
//     res.resize(deg);
//
//     return res;
//   }
//
//   fps shift(T c) {
//     std::vector<T> res(size()), ifacts(size());
//
//     T x = 1;
//
//     for (int i = 0; i < size(); i++) {
//       ifacts[i] = x * factorials<T>::inv(i);
//       x *= c;
//     }
//
//     for (int i = 0; i < size(); i++) {
//       res[size() - 1 - i] = v[i] * factorials<T>::get(i);
//     }
//
//     res = atcoder::convolution(res, ifacts);
//
//     res.resize(size());
//
//     std::ranges::reverse(res);
//
//     for (int i = 0; i < size(); i++) {
//       res[i] *= factorials<T>::inv(i);
//     }
//
//     return res;
//   }
//
//   fps operator-() {
//     fps res(v.size());
//     for (int i = 0; i < v.size(); i++) res[i] = -v[i];
//     return res;
//   }
//
//   fps &operator+=(const fps &rhs) {
//     if (v.size() < rhs.v.size()) v.resize(rhs.v.size());
//     for (int i = 0; i < rhs.v.size(); i++) v[i] += rhs.v[i];
//     return *this;
//   }
//
//   fps &operator-=(const fps &rhs) {
//     if (v.size() < rhs.v.size()) v.resize(rhs.v.size());
//     for (int i = 0; i < rhs.v.size(); i++) v[i] -= rhs.v[i];
//     return *this;
//   }
//
//   fps &operator*=(const fps &rhs) {
//     return *this = atcoder::convolution(v, rhs.v);
//   }
//
//   fps &operator+=(const T &rhs) {
//     if (v.size() == 0) v.resize(1);
//     v[0] += rhs;
//     return *this;
//   }
//
//   fps &operator-=(const T &rhs) {
//     if (v.size() == 0) v.resize(1);
//     v[0] -= rhs;
//     return *this;
//   }
//
//   fps &operator*=(const T &rhs) {
//     for (int i = 0; i < v.size(); i++) v[i] *= rhs;
//     return *this;
//   }
//
//   fps &operator/=(const T &rhs) {
//     T rhs_inv = rhs.inv();
//     for (int i = 0; i < v.size(); i++) v[i] *= rhs_inv;
//     return *this;
//   }
//
//   friend fps operator+(const fps &lhs, const fps &rhs) {
//     return fps(lhs) += rhs;
//   }
//
//   friend fps operator-(const fps &lhs, const fps &rhs) {
//     return fps(lhs) -= rhs;
//   }
//
//   friend fps operator*(const fps &lhs, const fps &rhs) {
//     return fps(lhs) *= rhs;
//   }
//
//   friend fps operator+(const fps &lhs, const T &rhs) { return fps(lhs) +=
//   rhs; }
//
//   friend fps operator-(const fps &lhs, const T &rhs) { return fps(lhs) -=
//   rhs; }
//
//   friend fps operator*(const fps &lhs, const T &rhs) { return fps(lhs) *=
//   rhs; }
//
//   friend fps operator/(const fps &lhs, const T &rhs) { return fps(lhs) /=
//   rhs; }
//
//   friend fps operator+(const T &lhs, const fps &rhs) { return fps(rhs) +=
//   lhs; }
//
//   friend fps operator-(const T &lhs, const fps &rhs) {
//     return -(fps(rhs) -= lhs);
//   }
//
//   friend fps operator*(const T &lhs, const fps &rhs) { return fps(rhs) *=
//   lhs; }
// };

// using mint = atcoder::modint998244353;
// using mint = atcoder::modint1000000007;

// using fs = factorials<mint>;

int main() {
  int N, M, R, Q;
  std::cin >> N >> M >> R >> Q;

  std::vector graph(N, std::vector<std::pair<int, ll>>());

  for (int i = 0; i < M; i++) {
    int a, b;
    ll c;
    std::cin >> a >> b >> c;
    a--;
    b--;

    graph[b].emplace_back(a, c);
  }

  std::vector dist(R, std::vector(N, infs<ll>::max() / 4));

  for (int r = 0; r < R; r++) {
    std::priority_queue<std::pair<ll, int>> priq;

    std::vector<bool> locked(N);

    for (int i = r; i < N; i += R) {
      dist[r][i] = 0;
      priq.emplace(0, i);
    }

    while (!priq.empty()) {
      auto [c, v] = priq.top();
      priq.pop();

      if (locked[v]) continue;
      c = -c;
      locked[v] = true;

      for (auto [w, d] : graph[v]) {
        if (chmin(dist[r][w], c + d)) {
          priq.emplace(-(c + d), w);
        }
      }
    }
  }

  ll ans = 0;

  for (int i = 0; i < Q; i++) {
    int x, y;
    ll z;
    std::cin >> x >> y >> z;

    x--;
    y--;

    ll tmp = infs<ll>::max() / 4;

    for (int r = 0; r < R; r++) {
      chmin(tmp, dist[r][x] + dist[r][y]);
    }

    ans += std::max(0LL, z - tmp);
  }

  std::cout << ans << std::endl;
}
accuracy: 1
time_ms: 130
memory_kb: 17072
final_rank: 3

code_snippet: //#include<atcoder/all>
//using namespace atcoder;

#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define eps (double)(1e-9)
#define pi (double) acos(-1)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

int main(){
    int n, m, r, q;
    cin >> n >> m >> r >> q;
    vector<ll> a(m), b(m), c(m);
    vector<ll> x(q), y(q), z(q);
    rep(i, m)cin >> a[i] >> b[i] >> c[i];
    rep(i, q)cin >> x[i] >> y[i] >> z[i];

    vector<vector<ll>> mn(n+1, vector<ll>(r, inf));
    rep(i, m)chmin(mn[a[i]][b[i]%r], c[i]);

    ll ans = 0;
    rep(i, q){
        ll now = inf;
        rep(j, r){
            ll cost1 = mn[x[i]][j];
            ll cost2 = mn[y[i]][j];
            if(x[i] % r == j)cost1 = 0;
            if(y[i] % r == j)cost2 = 0;
            chmin(now, cost1 + cost2);
        }
        ans += max(0LL, - now + z[i]);
    }

    cout << ans << endl;

}
accuracy: 0.03333333333333333
time_ms: 50
memory_kb: 12844
final_rank: 15

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

//高速化 
struct ponjuice{ponjuice(){cin.tie(0);ios::sync_with_stdio(0);cout<<fixed<<setprecision(20);}}PonJuice;
//#define endl '\n' //インタラクティブ問題の時は消す

//型
using ll = long long;
using ld = long double;
template<class T>using vc = vector<T>; template<class T>using vvc = vc<vc<T>>; template<class T>using vvvc = vvc<vc<T>>;
using vi = vc<int>;  using vvi = vvc<int>;  using vvvi = vvvc<int>;
using vl = vc<ll>;   using vvl = vvc<ll>;   using vvvl = vvvc<ll>;
using pi = pair<int, int>;  using pl = pair<ll, ll>;
using ull = unsigned ll;
template<class T>using priq = priority_queue<T>;
template<class T>using priqg = priority_queue<T, vc<T>, greater<T>>;

// for文
#define overload4(a, b, c, d, e, ...) e
#define rep1(n)             for(ll i = 0; i < n; i++)
#define rep2(i, n)          for(ll i = 0; i < n; i++)
#define rep3(i, a, b)       for(ll i = a; i < b; i++)
#define rep4(i, a, b, step) for(ll i = a; i < b; i+= step)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define per1(n)             for(ll i = n-1; i >= 0; i--)
#define per2(i, n)          for(ll i = n-1; i >= 0; i--)
#define per3(i, a, b)       for(ll i = b-1; i >= a; i--)
#define per4(i, a, b, step) for(ll i = b-1; i >= a; i-= step)
#define per(...) overload4(__VA_ARGS__, per4, per3, per2, per1)(__VA_ARGS__)
#define fore1(a)       for(auto&& i : a)	
#define fore2(i,a)     for(auto&& i : a)
#define fore3(x,y,a)   for(auto&& [x, y] : a)
#define fore4(x,y,z,a) for(auto&& [x, y, z] : a)
#define fore(...) overload4(__VA_ARGS__, fore4, fore3, fore2, fore1)(__VA_ARGS__)

//関数
#define mp make_pair
#define mt make_tuple
#define a first
#define b second
#define pb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define si(x) (ll)(x).size()
template<class S, class T>inline bool chmax(S& a, T b){return a < b && ( a = b , true);}
template<class S, class T>inline bool chmin(S& a, T b){return a > b && ( a = b , true);}
template<class T>void uniq(vc<T>&a){sort(all(a));a.erase(unique(all(a)),a.end());}
template<class T>vc<T> operator++(vc<T>&v,signed){auto res = v;fore(e,v)e++;return res;}
template<class T>vc<T> operator--(vc<T>&v,signed){auto res = v;fore(e,v)e--;return res;}
template<class T>vc<T> operator++(vc<T>&v){fore(e,v)e++;return v;}
template<class T>vc<T> operator--(vc<T>&v){fore(e,v)e--;return v;}

//入出力(operator)
template<class S,class T>istream&operator>>(istream&is,pair<S,T>&a){is>>a.a>>a.b;return is;}
template<class T>istream&operator>>(istream&is,vc<T>&a){fore(e,a)is>>e;return is;}

template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>&a){return os<<a.a<<" "<<a.b;}
template<class T>ostream&operator<<(ostream&os,set<T>&a){fore(it,a){os<<it<<" ";}return os;}
template<class T>ostream&operator<<(ostream&os,multiset<T>&a){fore(it,a){os<<it<<" ";}return os;}
template<class S,class T>ostream&operator<<(ostream&os,map<S,T>&a){fore(x,y,a){os<<x<<" "<<y<<"\n";}return os;}
template<class T>ostream&operator<<(ostream&os,unordered_set<T>&a){fore(it,a){os<<it<<" ";}return os;}
template<class S,class T>ostream&operator<<(ostream&os,unordered_map<S,T>&a){fore(x,y,a){os<<x<<" "<<y<<"\n";}return os;}
template<class T>ostream&operator<<(ostream&os,vc<T>&a){fore(e,a)os<<e<<" ";return os;}
template<class T>ostream&operator<<(ostream&os,vvc<T>&a){fore(e,a)os<<e<<"\n";return os;}

//入出力(関数)
vi readvi(ll n){vi a(n);cin>>a;return a;}
vl readvl(ll n){vl a(n);cin>>a;return a;}
vvi readg(ll n,ll m,bool bidirected=true){vvi g(n);rep(i,m){ll a,b;cin>>a>>b;a--;b--;g[a].pb(b);if(bidirected)g[b].pb(a);}return g;}
vvc<pi>readgc(ll n,ll m,bool bidirected=true){vvc<pi> g(n);rep(i,m){ll a,b,c;cin>>a>>b>>c;a--;b--;g[a].pb(b,c);if(bidirected)g[b].pb(a,c);}return g;}
vvi readt(ll n,bool bidirected=true){return readg(n,n-1,bidirected);}
vvc<pi> readtc(ll n,bool bidirected=true){return readgc(n,n-1,bidirected);}

inline void yes(){cout << "Yes\n";}
inline void no(){cout << "No\n";}
inline void yesno(bool y = true){if(y)yes();else no();}

//定数
constexpr ll mod = 998244353;
constexpr ll minf=-(1<<29);
constexpr ll inf=(1<<29);
constexpr ll MINF=-(1LL<<60);
constexpr ll INF=(1LL<<60);
constexpr ld EPS = 1e-8;
const ld PI = acosl(-1);
#define equals(a, b) (abs((a) - (b)) < EPS)
const int dx[4] ={-1, 0, 1, 0};
const int dy[4] ={ 0, 1, 0,-1};
const int dx8[8] ={-1,-1,-1, 0, 1, 1, 1, 0};
const int dy8[8] ={-1, 0, 1, 1, 1, 0,-1,-1};

void solve();
int main() {
	int t = 1;
    // cin >>t;
    while(t--)solve();
}



void solve(){
    ll n,m,q,r;
    cin >> n >> m >> r >> q;

    vvc<pi> g(n);

    vvi cst(n, vi(r, inf));
    rep(i,n) cst[i][i % r] = 0;
    rep(i,m){
        int a,b,c;
        cin >> a >> b >> c;
        a--,b--;
        g[b].emplace_back(a, c);
    }

    rep(i,r){
        priqg<pi> q;
        rep(j,n){
            if(j * r + i >= n) break;
            q.push({0, j * r + i});
        }

        while(q.size()){
            int p = q.top().second;
            int c = q.top().first;
            q.pop();
            if(cst[p][i] != c) continue;

            rep(j, si(g[p])){
                if(chmin(cst[g[p][j].first][i], c + g[p][j].second)){
                    q.push({c + g[p][j].second, g[p][j].first});
                }
            }
        }
    }

    ll ans = 0;
    rep(_, q){
        int x,y,z;
        cin >> x >> y >> z;
        x--,y--;

        int mx = 0;
        rep(i, r){
            chmax(mx, z - cst[x][i] - cst[y][i]);
        }
        ans += mx;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 150
memory_kb: 15880
final_rank: 4

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 2810.cc: Card Game
 */

#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_R = 10;
const long long LINF = 1LL << 60;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<pii> vpii;

/* global variables */

vpii nbrs[MAX_N];
ll kds[MAX_R][MAX_N];

/* subroutines */

void calcdists(int n, int k, int r, ll ds[]) {
  fill(ds, ds + n, LINF);
  priority_queue<pli> q;

  for (int u = k; u < n; u += r)
    ds[u] = 0, q.push(pii(0, u));

  while (! q.empty()) {
    auto ue = q.top(); q.pop();
    ll ud = -ue.first;
    int u = ue.second;
    if (ud != ds[u]) continue;

    for (auto &vw: nbrs[u]) {
      int v = vw.first;
      ll vd = ud + vw.second;
      if (ds[v] > vd) {
	ds[v] = vd;
	q.push(pli(-vd, v));
      }
    }
  }
}

/* main */

int main() {
  int n, m, r, qn;
  scanf("%d%d%d%d", &n, &m, &r, &qn);
  
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--, b--;
    nbrs[b].push_back(pii(a, c));
  }

  for (int k = 0; k < r; k++) calcdists(n, k, r, kds[k]);

  ll sum = 0;
  
  while (qn--) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    x--, y--;

    ll maxd = 0;
    for (int k = 0; k < r; k++)
      maxd = max(maxd, z - (kds[k][x] + kds[k][y]));

    sum += maxd;
  }

  printf("%lld\n", sum);
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 16540
final_rank: 2

code_snippet: #pragma GCC optimize("O3")
#include<bits/stdc++.h> 
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>; 
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
const vector<int> dy={-1,0,1,0},dx={0,-1,0,1};
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
struct edge{
      int to;
      ll cost;
      edge(int to,ll cost):to(to),cost(cost){}
};
int main(){
      int n,m,r,q;
      cin>>n>>m>>r>>q;
      V<V<int>> d(r);
      for(int i=1;i<=n;i++){
             d[i%r].emplace_back(i);
      }
      V<V<edge>> g(n+1);
      for(int i=0;i<m;i++){
            ll a,b,c;
            cin>>a>>b>>c;
            g[b].emplace_back(a,c);
      }
      V<V<ll>> dp(n+1,V<ll>(r,inf));
      for(int i=0;i<r;i++){
            priority_queue<P,V<P>,greater<P>> pq;
            for(int v:d[i]){
                  pq.emplace(0,v);
                  dp[v][i]=0;
            }
            while(pq.size()){
                  auto[v,cur]=pq.top();
                  pq.pop();
                  if(dp[cur][i]<v)continue;
                  for(edge &e:g[cur]){
                          if(chmin(dp[e.to][i],dp[cur][i]+e.cost)){
                                pq.emplace(dp[e.to][i],e.to);
                          }
                  }
            }
      }
      ll ans=0;
      for(int i=0;i<q;i++){
            ll x,y,z;
            cin>>x>>y>>z;
            ll mi=inf;
            for(int j=0;j<r;j++){
                  chmin(mi,dp[x][j]+dp[y][j]);
            }
            ans+=max(0ll,z-mi);
      }
      cout<<ans<<"\n";
}
accuracy: 1
time_ms: 210
memory_kb: 21344
final_rank: 8

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
using PII = pair<ll, ll>;
#define FOR(i,a,n) for(ll i=(ll)a; i<(ll)n; ++i)
#define REP(i,n) FOR(i,0,n)



int main() {
	int N, M, MD, K;
	cin >> N >> M >> MD >> K;
	vector<vector<pair<int, long long int>>>rev_edge(N + 1);
	for (int i = 0; i < M; i++) {
		int l, r, k;
		cin >> l >> r >> k;
		rev_edge[r].push_back({ l,k });
	}
	vector<vector<long long int>>dis(MD, vector<long long int>(N + 1,MOD*MOD));
	for (int i = 0; i < MD; i++) {
		for (int j = i; j <= N; j += MD) {
			dis[i][j] = 0;
		}
		priority_queue<pair<long long int, int>, vector<pair<long long int, int>>, greater<pair<long long int, int>>>PQ;
		for (int j = 0; j <= N; j++) {
			if (dis[i][j] == MOD*MOD) continue;
			PQ.push({ dis[i][j],j });
		}
		while (!PQ.empty()) {
			int cn = PQ.top().second;
			long long int c = PQ.top().first;
			PQ.pop();
			for (auto j : rev_edge[cn]) {
				if (dis[i][j.first]>c + j.second) {
					dis[i][j.first] = c + j.second;
					PQ.push({ dis[i][j.first],j.first });
				}
			}
		}
	}
	long long int ans = 0;
	for (int i = 0; i < K; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		long long int add = 0;
		for (int j = 0; j < MD; j++) {
			add = max(add, c - dis[j][a] - dis[j][b]);
		}
		ans += add;
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 260
memory_kb: 16780
final_rank: 6

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
using PII = pair<ll, ll>;
#define FOR(i,a,n) for(ll i=(ll)a; i<(ll)n; ++i)
#define REP(i,n) FOR(i,0,n)



int main() {
	int N, M, MD, K;
	cin >> N >> M >> MD >> K;
	vector<vector<pair<int, long long int>>>rev_edge(N + 1);
	for (int i = 0; i < M; i++) {
		int l, r, k;
		cin >> l >> r >> k;
		rev_edge[r].push_back({ l,k });
	}
	vector<vector<long long int>>dis(MD, vector<long long int>(N + 1,MOD*MOD));
	for (int i = 0; i < MD; i++) {
		for (int j = i; j <= N; j += MD) {
			dis[i][j] = 0;
		}
		priority_queue<pair<long long int, int>, vector<pair<long long int, int>>, greater<pair<long long int, int>>>PQ;
		for (int j = 0; j <= N; j++) {
			if (dis[i][j] == MOD*MOD) continue;
			PQ.push({ dis[i][j],j });
		}
		while (!PQ.empty()) {
			int cn = PQ.top().second;
			long long int c = PQ.top().first;
			PQ.pop();
			for (auto j : rev_edge[cn]) {
				if (dis[i][j.first]>c + j.second) {
					dis[i][j.first] = c + j.second;
					PQ.push({ dis[i][j.first],j.first });
				}
			}
		}
	}
	long long int ans = 0;
	for (int i = 0; i < K; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		long long int add = 0;
		for (int j = 0; j < MD; j++) {
			add = max(add, c - dis[j][a] - dis[j][b]);
		}
		ans += add;
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 260
memory_kb: 16844
final_rank: 7

code_snippet: #define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>

using namespace std;

typedef pair<long long int, long long int> P;

long long int INF = 1e18;
long long int MOD = 1e9 + 7;

#define MAX_V 200000

struct edge{
	int to;
	long long int cost;
};

vector<edge> G[MAX_V];
long long int cost[10][MAX_V];

void shortest_path(int s, int V, long long int d[]){
	
	priority_queue<P, vector<P>, greater<P> > que;
	
	for(int i = 0; i < V; i++){
		d[i] = INF;
	}
	d[s] = 0;
	
	que.push(P(0, s));
	
	while(!que.empty()){
		
		P p = que.top();
		que.pop();
		
		int v = p.second;
		
		if(d[v] < p.first){
			continue;
		}
		
		for(int i = 0; i < G[v].size(); i++){
			edge e = G[v][i];
			if(d[e.to] > e.cost + d[v]){
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

int main(){
    int N, M, R, Q;
    cin >> N >> M >> R >> Q;
    for(int i = 0; i < M; i++){
		int u, v, cost;
		cin >> u >> v >> cost;
        edge e;
        e.cost = cost;
        e.to = u;
        G[v].push_back(e);
    }
    for(int i = 0; i < R; i++){
        G[0].clear();
        edge e;
        e.cost = 0;
        for(int j = 1; j <= N; j++){
            if(j % R == i){
                e.to = j;
                G[0].push_back(e);
            }
        }
        shortest_path(0, N + 1, cost[i]);
    }
    long long int ans = 0;
    for(int loop = 0; loop < Q; loop++){
        long long int x, y, z;
        cin >> x >> y >> z;
        long long int S = 0;
        for(int i = 0; i < R; i++){
            S = max(S, z - cost[i][x] - cost[i][y]);
        }
        ans += S;
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 260
memory_kb: 19252
final_rank: 11

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 2810.cc: Card Game
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_R = 10;
const int MAX_Z = 100000;

/* typedef */

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
typedef map<pii,int> mpii;

struct Stat {
  int d, i, j;
  Stat() {}
  Stat(int _d, int _i, int _j): d(_d), i(_i), j(_j) {}

  bool operator<(const Stat &s) const { return d > s.d; }
  void print() { printf("Stat: (%d,%d):%d\n", i, j, d); }
};

/* global variables */

vpii nbrs[MAX_N];
mpii dists;

/* subroutines */

/* main */

int main() {
  int n, m, r, qn;
  scanf("%d%d%d%d", &n, &m, &r, &qn);
  
  for (int i = 0; i < n; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    a--, b--;
    nbrs[a].push_back(pii(b, c));
  }

  ll sum = 0;
  
  while (qn--) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    x--, y--;
    if (x > y) swap(x, y);

    priority_queue<Stat> q;
    q.push(Stat(0, x, y));

    dists.clear();
    dists[pii(x, y)] = 0;

    int mind = z;
    while (! q.empty()) {
      Stat u = q.top(); q.pop();
      //u.print();
      if (u.d != dists[pii(u.i, u.j)]) continue;

      if (u.i % r == u.j % r) {
	mind = u.d;
	break;
      }

      vpii &nbri = nbrs[u.i];
      for (vpii::iterator vit = nbri.begin(); vit != nbri.end(); vit++) {
	int vi = vit->first, vd = u.d + vit->second;
	if (vd < z) {
	  int vj = u.j;
	  if (vi > vj) swap(vi, vj);
	  pii vp(vi, vj);
	  mpii::iterator mit = dists.find(vp);
	  if (mit == dists.end() || mit->second > vd) {
	    dists[vp] = vd;
	    q.push(Stat(vd, vi, vj));
	  }
	}
      }

      vpii &nbrj = nbrs[u.j];
      for (vpii::iterator vit = nbrj.begin(); vit != nbrj.end(); vit++) {
	int vj = vit->first, vd = u.d + vit->second;
	if (vd < z) {
	  int vi = u.i;
	  if (vi > vj) swap(vi, vj);
	  pii vp(vi, vj);
	  mpii::iterator mit = dists.find(vp);
	  if (mit == dists.end() || mit->second > vd) {
	    dists[vp] = vd;
	    q.push(Stat(vd, vi, vj));
	  }
	}
      }
    }

    sum += z - mind;
  }

  printf("%lld\n", sum);
  return 0;
}
accuracy: 0.03333333333333333
time_ms: 20
memory_kb: 7052
final_rank: 14

code_snippet: #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;
}
accuracy: 1
time_ms: 260
memory_kb: 19192
final_rank: 9

code_snippet: #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;
}
accuracy: 0.16666666666666666
time_ms: 260
memory_kb: 18096
final_rank: 12

code_snippet: #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 100011
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});
    }
    
    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;
}
accuracy: 0.16666666666666666
time_ms: 260
memory_kb: 18204
final_rank: 13

code_snippet: #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 100011
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+i%r].pb(edge<int>{i,0});
    }
    
    vector<vector<int>> d(r,vector<int>(n+r));
    for(int i=0;i<r;i++){
        dijkstra<int>(n+i,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;
}
accuracy: 0.03333333333333333
time_ms: 110
memory_kb: 13980
final_rank: 19

code_snippet: #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 100011
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+i%r].pb(edge<int>{i,0});
    }
    
    vector<vector<int>> d(r,vector<int>(n+r));
    for(int i=0;i<r;i++){
        dijkstra<int>(n+i,d[i],G);
    }
    
    int ans = 0;
    rep(i,q){
        int x,y,z;
        cin>>x>>y>>z;
        int mini = INF;
        rep(i,r){
            mini = min(mini,d[i][x]+d[i][y]);
        }
        if(z-mini>0)ans += z-mini;
    }
    cout<<ans<<endl;
}
accuracy: 0.03333333333333333
time_ms: 110
memory_kb: 14124
final_rank: 20

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long 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 (1e9+1)

//verified AOJ GRL_1
#define MAX_V 100011
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));
            }
        }
    }
}



int 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+i%r].pb(edge<int>{i,0});
    }
    
    vector<vector<int>> d(r,vector<int>(n+r));
    for(int i=0;i<r;i++){
        dijkstra<int>(n+i,d[i],G);
    }
    
    int ans = 0;
    rep(i,q){
        int x,y,z;
        cin>>x>>y>>z;
        int mini = INF;
        rep(i,r){
            mini = min(mini,d[i][x]+d[i][y]);
        }
        if(z-mini>0)ans += z-mini;
    }
    cout<<ans<<endl;
}
accuracy: 0.03333333333333333
time_ms: 100
memory_kb: 10416
final_rank: 17

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
//#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define BIG_NUM 200000000000
#define NUM 100000


struct Info{
	Info(int arg_to,ll arg_cost){
		to = arg_to;
		cost = arg_cost;
	}
	int to;
	ll cost;
};

struct Data{
	Data(int arg_num,ll arg_cost){
		num = arg_num;
		cost = arg_cost;
	}
	bool operator<(const struct Data &arg) const{
		return cost > arg.cost;
	}
	int num;
	ll cost;
};

vector<Info> rev_G[NUM+1];
ll min_cost[10][NUM+1];


int main(){

	int N,M,R,Q;
	scanf("%d %d %d %d",&N,&M,&R,&Q);

	int from,to;
	ll cost;

	for(int loop = 0; loop < M; loop++){
		scanf("%d %d %lld",&from,&to,&cost);
		rev_G[to].push_back(Info(from,cost));
	}

	for(int i = 0; i < R; i++){
		for(int k = 1; k <= N; k++)min_cost[i][k] = BIG_NUM;
	}

	int next_num;
	ll next_cost;
	priority_queue<Data> PQ;

	for(int start = 0; start < R; start++){

		for(int to = 1; to <= N; to++){
			if(to%R == start){
				min_cost[start][to] = 0;
				PQ.push(Data(to,0));
			}else{
				min_cost[start][to] = BIG_NUM;
			}
		}

		while(!PQ.empty()){

			if(PQ.top().cost > min_cost[start][PQ.top().num]){
				PQ.pop();
				continue;
			}

			for(int i = 0; i < rev_G[PQ.top().num].size(); i++){
				next_num = rev_G[PQ.top().num][i].to;
				next_cost = PQ.top().cost+rev_G[PQ.top().num][i].cost;

				if(min_cost[start][next_num] > next_cost){
					min_cost[start][next_num] = next_cost;
					PQ.push(Data(next_num,next_cost));
				}
			}

			PQ.pop();
		}
	}

	ll ans = 0;
	int x,y;
	ll z,minimum;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %lld",&x,&y,&z);

		if(x%R == y%R){
			ans += z;
			continue;
		}

		minimum = BIG_NUM;
		for(int i = 0; i < R; i++){
			if(min_cost[i][x] == BIG_NUM || min_cost[i][y] == BIG_NUM)continue;
			minimum = min(minimum,min_cost[i][x]+min_cost[i][y]);
		}

		if(z > minimum){
			ans += z-minimum;
		}
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 16620
final_rank: 5

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
//#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define BIG_NUM 200000000000
#define NUM 100000


struct Info{
	Info(int arg_to,ll arg_cost){
		to = arg_to;
		cost = arg_cost;
	}
	int to;
	ll cost;
};

struct Data{
	Data(int arg_num,ll arg_cost){
		num = arg_num;
		cost = arg_cost;
	}
	bool operator<(const struct Data &arg) const{
		return cost > arg.cost;
	}
	int num;
	ll cost;
};

vector<Info> G[NUM+1];
ll min_cost[NUM+1][10];


int main(){

	int N,M,R,Q;
	scanf("%d %d %d %d",&N,&M,&R,&Q);

	int from,to;
	ll cost;

	for(int loop = 0; loop < M; loop++){
		scanf("%d %d %lld",&from,&to,&cost);
		G[from].push_back(Info(to,cost)); //★有向と解釈
	}

	//各数字から、Rで割ったあまりへの最小移行コストを計算する
	for(int i = 1; i <= N; i++){
		for(int k = 0; k < R; k++)min_cost[i][k] = BIG_NUM;
	}

	int next_num;
	ll next_cost;
	priority_queue<Data> PQ;

	for(int i = 1; i <= N; i++){
		min_cost[i][i%R] = 0; //数字そのものの、Rで割った余りは当然コスト0

		PQ.push(Data(i,0));

		while(!PQ.empty()){

			if(PQ.top().cost > min_cost[i][PQ.top().num%R]){
				PQ.pop();
				continue;
			}

			for(int k = 0; k < G[PQ.top().num].size(); k++){
				next_num = G[PQ.top().num][k].to;
				next_cost = PQ.top().cost+G[PQ.top().num][k].cost;

				if(min_cost[i][next_num%R] > next_cost){
					min_cost[i][next_num%R] = next_cost;
					PQ.push(Data(next_num,next_cost));
				}
			}
			PQ.pop();
		}
	}

	ll ans = 0;
	int x,y;
	ll z,minimum;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %lld",&x,&y,&z);

		if(x%R == y%R){
			ans += z;
			continue;
		}

		minimum = BIG_NUM;
		for(int i = 0; i < R; i++){
			if(min_cost[x][i] == BIG_NUM || min_cost[y][i] == BIG_NUM)continue;
			minimum = min(minimum,min_cost[x][i]+min_cost[y][i]);
		}

		if(z > minimum){ //報酬が最小変換コストより高い場合
			ans += z-minimum;
		}
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 0.03333333333333333
time_ms: 60
memory_kb: 13324
final_rank: 18

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
//#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define BIG_NUM 200000000000
#define NUM 100000


struct Info{
	Info(int arg_to,ll arg_cost){
		to = arg_to;
		cost = arg_cost;
	}
	int to;
	ll cost;
};

struct Data{
	Data(int arg_num,ll arg_cost){
		num = arg_num;
		cost = arg_cost;
	}
	bool operator<(const struct Data &arg) const{
		return cost > arg.cost;
	}
	int num;
	ll cost;
};

vector<Info> G[NUM+1];
ll min_cost[NUM+1][10];


int main(){

	int N,M,R,Q;
	scanf("%d %d %d %d",&N,&M,&R,&Q);

	int from,to,cost;

	for(int loop = 0; loop < M; loop++){
		scanf("%d %d %lld",&from,&to,&cost);
		G[from].push_back(Info(to,cost));
	}

	for(int i = 1; i <= N; i++){
		for(int k = 0; k < R; k++)min_cost[i][k] = BIG_NUM;
	}

	int next_num;
	ll next_cost;
	priority_queue<Data> PQ;

	for(int i = 1; i <= N; i++){
		min_cost[i][i%R] = 0;

		PQ.push(Data(i,0));

		while(!PQ.empty()){

			for(int k = 0; k < G[PQ.top().num].size(); k++){
				next_num = G[PQ.top().num][k].to;
				next_cost = PQ.top().cost+G[PQ.top().num][k].cost;

				if(min_cost[i][next_num%R] > next_cost){
					min_cost[i][next_num%R] = next_cost;
					PQ.push(Data(next_num,next_cost));
				}
			}
			PQ.pop();
		}
	}

	ll ans = 0;
	int x,y;
	ll z,minimum;

	for(int loop = 0; loop < Q; loop++){
		scanf("%d %d %lld",&x,&y,&z);

		if(x%R == y%R){
			ans += z;
			continue;
		}

		minimum = BIG_NUM;
		for(int i = 0; i < R; i++){
			if(min_cost[x][i] == BIG_NUM || min_cost[y][i] == BIG_NUM)continue;
			minimum = min(minimum,min_cost[x][i]+min_cost[y][i]);
		}

		if(z > minimum){
			ans += z-minimum;
		}
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 0.03333333333333333
time_ms: 50
memory_kb: 13384
final_rank: 16