Submission #882869

#TimeUsernameProblemLanguageResultExecution timeMemory
882869MilosMilutinovicWild Boar (JOI18_wild_boar)C++14
47 / 100
4282 ms1048576 KiB
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, t, l; cin >> n >> m >> t >> l; vector<vector<pair<int, int>>> g(n); for (int i = 0; i < m; i++) { int x, y, z; cin >> x >> y >> z; --x; --y; g[x].emplace_back(y, z); g[y].emplace_back(x, z); } vector<int> seq(l); for (int i = 0; i < l; i++) { cin >> seq[i]; --seq[i]; } { int p, x; cin >> p >> x; --p; --x; seq[p] = x; } vector<int> deg(n); for (int i = 0; i < n; i++) { deg[i] = (int) g[i].size(); } vector<vector<int>> idx(n, vector<int>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < deg[i]; j++) { idx[i][g[i][j].first] = j; } } const long long inf = (long long) 1e18; vector<vector<vector<vector<long long>>>> dist(n, vector<vector<vector<long long>>>(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { continue; } dist[i][j].resize(deg[i]); for (int k = 0; k < deg[i]; k++) { dist[i][j][k] = vector<long long>(deg[j], inf); } } } vector<vector<long long>> d(n); for (int i = 0; i < n; i++) { d[i].resize(deg[i]); } vector<int> cnt(n); set<tuple<long long, int, int>> st; for (int ver = 0; ver < n; ver++) { for (int e = 0; e < deg[ver]; e++) { for (int i = 0; i < n; i++) { for (int j = 0; j < deg[i]; j++) { d[i][j] = inf; } cnt[i] = 0; } int to = g[ver][e].first; int w = g[ver][e].second; d[to][idx[to][ver]] = w; st.clear(); st.emplace(d[to][idx[to][ver]], to, idx[to][ver]); while (!st.empty()) { auto it = st.begin(); int v = get<1>(*it); int lst = get<2>(*it); st.erase(it); cnt[v] += 1; if (cnt[v] >= 3) { continue; } for (int i = 0; i < deg[v]; i++) { if (i == lst) { continue; } int to = g[v][i].first; int w = g[v][i].second; if (d[to][idx[to][v]] > d[v][lst] + w) { if (d[to][idx[to][v]] != inf) { st.erase(st.find({d[to][idx[to][v]], to, idx[to][v]})); } d[to][idx[to][v]] = d[v][lst] + w; st.emplace(d[to][idx[to][v]], to, idx[to][v]); } } } for (int i = 0; i < n; i++) { if (i == ver) { continue; } for (int j = 0; j < deg[i]; j++) { dist[ver][i][e][j] = d[i][j]; } } } } vector<vector<vector<vector<int>>>> good(n, vector<vector<vector<int>>>(n)); for (int from = 0; from < n; from++) { for (int to = 0; to < n; to++) { if (from == to) { continue; } good[from][to].resize(deg[from]); for (int i = 0; i < deg[from]; i++) { int x = -1, y = -1; for (int j = 0; j < deg[to]; j++) { if (x == -1 || dist[from][to][i][j] < dist[from][to][i][x]) { y = x; x = j; } else if (y == -1 || dist[from][to][i][j] < dist[from][to][i][y]) { y = j; } } if (x != -1) { good[from][to][i].push_back(x); } if (y != -1) { good[from][to][i].push_back(y); } } } } vector<long long> dp(n, inf); for (int i = 0; i < deg[seq[0]]; i++) { for (int j = 0; j < deg[seq[1]]; j++) { dp[j] = min(dp[j], dist[seq[0]][seq[1]][i][j]); } } vector<long long> new_dp(n); vector<long long> val(n); for (int p = 2; p < l; p++) { for (int i = 0; i < deg[seq[p]]; i++) { new_dp[i] = inf; } for (int i = 0; i < deg[seq[p - 1]]; i++) { val[i] = inf; } { long long mn = inf; for (int i = 0; i < deg[seq[p - 1]]; i++) { val[i] = min(val[i], mn); mn = min(mn, dp[i]); } } { long long mn = inf; for (int i = deg[seq[p - 1]] - 1; i >= 0; i--) { val[i] = min(val[i], mn); mn = min(mn, dp[i]); } } for (int i = 0; i < deg[seq[p - 1]]; i++) { if (val[i] != inf) { for (int j : good[seq[p - 1]][seq[p]][i]) { new_dp[j] = min(new_dp[j], val[i] + dist[seq[p - 1]][seq[p]][i][j]); } } } for (int i = 0; i < deg[seq[p]]; i++) { dp[i] = new_dp[i]; } } long long res = inf; for (int i = 0; i < deg[seq.back()]; i++) { res = min(res, dp[i]); } cout << (res == inf ? -1 : res) << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...