// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 6005;
const int MAXL = 100005;
int n, m, t, l, e;
vi adj[MAXN], radj[MAXN];
int w[MAXN];
int x[MAXL];
ll rd[MAXN][MAXN][4];
ll dp[MAXL][2];
ll dist[MAXN][MAXN];
int fi[MAXN][MAXN], se[MAXN][MAXN];
priority_queue<pll, vector<pll>, greater<pll>> pq;
void dijkstra(int s) {
REP (i, 0, e) {
dist[s][i] = LINF;
fi[s][i] = se[s][i] = -1;
}
dist[s][s] = w[s];
pq.push(pll{w[s], s});
while (!pq.empty()) {
auto [d, u] = pq.top(); pq.pop();
if (d != dist[s][u]) continue;
for (int v : adj[u]) {
ll nd = d + w[v];
if (mnto(dist[s][v], nd)) {
pq.push(pll{nd, v});
fi[s][v] = fi[s][u] == -1 ? v : fi[s][u];
se[s][v] = u;
}
}
}
}
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n >> m >> t >> l;
e = n + 1;
if (e & 1) {
e++;
}
REP (i, 0, m) {
int a, b, c; cin >> a >> b >> c;
w[e] = c;
adj[a].pb(e);
adj[e].pb(b);
radj[a].pb(e++);
w[e] = c;
adj[b].pb(e);
adj[e].pb(a);
radj[b].pb(e++);
}
REP (i, 1, e) {
dijkstra(i);
}
REP (i, 1, n + 1) {
REP (j, i + 1, n + 1) {
REP (z, 0, 4) {
rd[i][j][z] = LINF;
}
//rd[i][j][3] = dist[i][j];
for (int u : radj[i]) {
for (int v : radj[j]) {
v ^= 1;
int tp = 0;
auto onPath = [&] (int x) {
return dist[u][v] == dist[u][x] + dist[x][v] - w[x];
};
if (onPath(fi[i][j])) {
tp |= 1;
}
if (onPath(se[i][j])) {
tp |= 2;
}
mnto(rd[i][j][tp], dist[u][v]);
}
}
assert(rd[i][j][3] == dist[i][j]);
REP (z, 0, 4) {
rd[j][i][z] = rd[i][j][z];
cerr << i << ' ' << j << ' ' << z << ": " << rd[i][j][z] << '\n';
}
cerr << ' ' << fi[i][j] << ' ' << se[i][j] << '\n';
}
}
x[0] = -1;
REP (i, 1, l + 1) {
cin >> x[i];
}
while (t--) {
int p, q; cin >> p >> q;
x[p] = q;
dp[1][0] = dp[1][1] = 0;
REP (i, 2, l + 1) {
int p = x[i - 2], u = x[i - 1], v = x[i];
int f = fi[u][v], s = p == -1 ? -1 : se[p][u];
REP (j, 0, 2) {
dp[i][j] = LINF;
REP (k, 0, 2) {
REP (l, 0, 2) {
if (k == 1 && l == 1 && f ^ 1 == s) continue;
mnto(dp[i][j], dp[i - 1][k] + rd[u][v][(l << 1) + j]);
}
}
cerr << i << ' ' << j << ": " << dp[i][j] << '\n';
}
}
ll ans = min(dp[l][0], dp[l][1]);
if (ans >= LINF) {
ans = -1;
}
cout << ans << '\n';
}
return 0;
}
Compilation message
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:137:55: warning: suggest parentheses around comparison in operand of '^' [-Wparentheses]
137 | if (k == 1 && l == 1 && f ^ 1 == s) continue;
| ~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Incorrect |
1 ms |
980 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |