ll res = 0; for (int i = 0; i < s.length(); i++) { char c = s[i] - 'A'; res += cnt[c] * (i - 1) - sum[c]; cnt[c]++; sum[c] += i; } cout << res << "\n";
vector<array<int, 3>> E(m); for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; u--, v--; E[i] = { u, v, w }; }
vector<array<int, 3>> que(q); vector<int> del(m); for (int i = 0; i < q; i++) { int op; cin >> op; if (op == 1) { int x; cin >> x; x--; del[x] = 1; que[i] = { op, x, 0 }; } else { int u, v; cin >> u >> v; u--, v--; que[i] = { op, u, v }; } }
vector dis(n, vector(n, Inf)); for (int u = 0; u < n; ++u) { dis[u][u] = 0; } for (int i = 0; i < m; i++) { if (!del[i]) { auto [u, v, w] = E[i]; dis[u][v] = dis[v][u] = w; } } for (int k = 0; k < n; ++k) { for (int u = 0; u < n; ++u) { for (int v = 0; v < n; ++v) { dis[u][v] = min(dis[u][v], dis[u][k] + dis[k][v]); } } }
vector<ll> ans(q); for (int i = q - 1; i >= 0; i--) { auto [op, a, b] = que[i]; if (op == 1) { auto [s, t, w] = E[a]; for (int u = 0; u < n; ++u) { for (int v = 0; v < n; ++v) dis[u][v] = min(dis[u][v], min(dis[u][s] + dis[t][v] + w, dis[u][t] + dis[s][v] + w)); } } else { ans[i] = dis[a][b]; } }
for (int i = 0; i < q; i++) { if (ans[i]) cout << (ans[i] == Inf ? -1 : ans[i]) << "\n"; }
vector<vector<pair<int, int>>> g(n); vector<array<int, 3>> E(m); for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; u--, v--; g[u].push_back({ w, v }); g[v].push_back({ w, u }); E[i] = { u, v, w }; }
auto Dijkstra = [&](int s) { vector<ll> dis(n, Inf); vector<int> vis(n); vector<int> from(n, -1); vector<ll> pcnt(n); dis[s] = 0, pcnt[s] = 1; priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> Q; Q.push({ 0, s }); while (!Q.empty()) { auto [d, u] = Q.top(); Q.pop(); if (vis[u]) continue; vis[u] = 1; for (auto& [w, v] : g[u]) { if (dis[v] > w + d) { dis[v] = w + d; from[v] = u; pcnt[v] = pcnt[u]; Q.push({ dis[v], v }); } elseif (dis[v] == w + d) { (pcnt[v] += pcnt[u]) %= mod; } } } returnmake_pair(dis, pcnt); }; auto [d1, p1] = Dijkstra(0); auto [dn, pn] = Dijkstra(n - 1); for (auto& [u, v, w] : E) { if (d1[u] > d1[v]) swap(u, v); bool only = (d1[u] + w + dn[v] == dn[0]) && (p1[u] * pn[v] % mod == pn[0]); cout << (only ? "Yes" : "No") << "\n"; }