#include <bits/stdc++.h>
#define int long long
using namespace std;
string to_string(string s) { return s; }
template <typename T> string to_string(T v) {
bool first = true;
string res = "[";
for (const auto &x : v) {
if (!first)
res += ", ";
first = false;
res += to_string(x);
}
res += "]";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << to_string(H);
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
struct State {
int source = -1; int target = -1; int distance = 1e18;
State next (int node, int next, int cost) {
State st;
st.source = source == -1 ? next : source;
st.distance = cost + distance;
if (next == target) st.distance = 1e18;
st.target = node;
return st;
}
};
string to_string (State state) {
if (state.distance == 1e18)
return "State#";
return "State[" + to_string(state.source) + " => " + to_string(state.target) + ", " + to_string(state.distance) + "]";
}
int modify (vector<State> &states, State new_state) {
if (states[0].source == new_state.source) {
if (new_state.distance >= states[0].distance) return -1;
states[0].distance = new_state.distance;
states[0].target = new_state.target;
return 0;
} else if (states[0].distance >= new_state.distance) {
states[2] = states[1];
states[1] = states[0];
states[0] = new_state;
return 0;
} else if (states[1].source == new_state.source) {
if (new_state.distance >= states[1].distance) return -1;
states[1].distance = new_state.distance;
states[1].target = new_state.target;
return 1;
} else if (states[1].distance > new_state.distance) {
states[2] = states[1];
states[1] = new_state;
return 1;
} else if (states[2].source == new_state.source) {
if (new_state.distance >= states[2].distance) return -1;
states[2].distance = new_state.distance;
states[2].target = new_state.target;
return 2;
} else if (states[2].distance > new_state.distance) {
states[2] = new_state;
return 2;
}
return -1;
}
struct Matrix {
State ppc0, pcc1, pcc2, pcc_non0_0, pcc_non0_1;
Matrix merge (Matrix &other) {
dbg(other);
vector<State> simple(3);
for (State s1 : { ppc0, pcc1, pcc2, pcc_non0_0, pcc_non0_1 }) {
for (State s2 : { other.ppc0, other.pcc1, other.pcc2, other.pcc_non0_0, other.pcc_non0_1 }) {
if (s1.target == s2.source) continue ;
State s3 = s1;
s3.target = s2.target;
s3.distance += s2.distance;
dbg(s1, s2, s3);
modify(simple, s3);
}
}
int exc_targ = simple[0].target;
vector<State> exclu(3);
for (State s1 : { ppc0, pcc1, pcc2, pcc_non0_0, pcc_non0_1 }) {
for (State s2 : { other.ppc0, other.pcc1, other.pcc2, other.pcc_non0_0, other.pcc_non0_1 }) {
dbg(s1, s2, exc_targ);
if (s1.target == s2.source || s2.target == exc_targ) continue ;
State s3 = s1;
s3.target = s2.target;
s3.distance += s2.distance;
dbg(s1, s2, s3, exc_targ);
modify(exclu, s3);
}
}
Matrix res;
res.ppc0 = simple[0];
res.pcc1 = simple[1];
res.pcc2 = simple[2];
res.pcc_non0_0 = exclu[0];
res.pcc_non0_1 = exclu[1];
dbg(res);
return res;
}
};
string to_string (Matrix matrix) {
return "Mat[a00=" + to_string(matrix.ppc0) + ", a01=" + to_string(matrix.pcc_non0_0) + ", a10="+ to_string(matrix.pcc1) + ", a11="+ to_string(matrix.pcc_non0_1) + ", a20="+ to_string(matrix.pcc2) + "]";
}
using vMatrix = vector<Matrix>;
using ti = pair<pair<int, int>, int>;
using pq = priority_queue<ti, vector<ti>, less<ti>>;
template<typename T>
using grid = vector<vector<T>>;
using idata = vector<int>;
using graph = grid<pair<int, int>>;
int N, M, T, L;
graph roads;
vMatrix dijkstra (int start) {
pq q;
q.push({ { 0, 0 }, start });
grid<State> state_array(N, vector<State>(3));
grid<bool> visit_array(N, vector<bool>(3, false));
state_array[start][0].distance = 0;
state_array[start][0].source = -1;
while (q.size() != 0) {
const auto data = q.top(); q.pop();
int srcId = data.first.second;
int curr = data.second;
visit_array[curr][srcId] = true;
State state = state_array[curr][srcId];
if (state.distance != data.first.first) continue ;
for (const auto &road : roads[curr]) {
if (state.target == road.first) continue ;
State next_state = state.next( curr, road.first, road.second );
int res = modify(state_array[road.first], next_state);
if (res == -1) continue ;
q.push({ { next_state.distance, res }, road.first });
}
}
vMatrix matrices(N);
for (int i = 0; i < N; i ++) {
matrices[i].ppc0 = state_array[i][0];
matrices[i].pcc1 = state_array[i][1];
matrices[i].pcc2 = state_array[i][2];
int excluded_target = state_array[i][0].target;
vector<State> excluded(3);
for (const auto &road : roads[i]) {
int next = road.first; int cost = road.second;
if (next == excluded_target) continue ;
modify(excluded, state_array[next][0].next( next, i, cost ));
modify(excluded, state_array[next][1].next( next, i, cost ));
modify(excluded, state_array[next][2].next( next, i, cost ));
}
matrices[i].pcc_non0_0 = excluded[0];
matrices[i].pcc_non0_1 = excluded[1];
}
return matrices;
}
grid<Matrix> matrixGrid;
signed main () {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> N >> M >> T >> L;
roads.resize(N);
for (int i = 0; i < M; i ++) {
int a, b, c;
cin >> a >> b >> c;
a --; b --;
roads[a].push_back({ b, c });
roads[b].push_back({ a, c });
}
for (int i = 0; i < N; i ++)
matrixGrid.push_back( dijkstra(i) );
idata DV(L);
for (int i = 0; i < L; i ++) {
cin >> DV[i];
DV[i] --;
}
for (int i = 0; i < T; i ++) {
int p, v;
cin >> p >> v;
p --;
v --;
DV[p] = v;
Matrix result = matrixGrid[DV[0]][DV[1]];
dbg(result);
for (int j = 2; j < L; j ++) {
dbg(DV[j - 1] + 1, DV[j] + 1);
result = result.merge(matrixGrid[DV[j - 1]][DV[j]]);
}
int answer = result.ppc0.distance;
if (answer == 1e18) answer = -1;
cout << answer << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
320 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |