Submission #805558

# Submission time Handle Problem Language Result Execution time Memory
805558 2023-08-03T17:51:33 Z thimote75 Wild Boar (JOI18_wild_boar) C++14
0 / 100
1 ms 316 KB
#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 a00, a01, a20, a10, a11;

    Matrix merge (Matrix &other) {
        dbg(other);
        vector<State> simple(3);
        for (State s1 : { a00, a10, a20, a01, a11 }) {
            for (State s2 : { other.a00, other.a10, other.a20, other.a01, other.a11 }) {
                if (s1.target == s2.source) continue ;

                State s3 = s1;
                s3.target    = s2.target;
                s3.distance += s2.distance;
                modify(simple, s3);
            }
        }

        int exc_targ = simple[0].target;
        vector<State> exclu(3);
        for (State s1 : { a00, a10, a20, a10, a11 }) {
            for (State s2 : { other.a00, other.a10, other.a20, other.a10, other.a11 }) {
                if (s1.target == s2.source || s2.target == exc_targ) continue ;

                State s3 = s1;
                s3.target    = s2.target;
                s3.distance += s2.distance;
                modify(exclu, s3);
            }
        }

        Matrix res;
        res.a00 = simple[0];
        res.a10 = simple[1];
        res.a20 = simple[2];
        res.a01 = exclu[0];
        res.a11 = exclu[1];
        dbg(res);
        return res;
    }
};
string to_string (Matrix matrix) {
    return "Mat[a00=" + to_string(matrix.a00) + ", a01=" + to_string(matrix.a01) + ", a10="+ to_string(matrix.a10) + ", a11="+ to_string(matrix.a11) + ", a20="+ to_string(matrix.a20) + "]";
}
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];

        for (const auto &road : roads[curr]) {
            dbg(curr, road.first, road.second);
            if (state.target == road.first) continue ;
            dbg(curr, road.first, road.second, string("OK"));

            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 });
        }
    }

    dbg(start);
    dbg(state_array);

    vMatrix matrices(N);

    for (int i = 0; i < N; i ++) {
        matrices[i].a00 = state_array[i][0];
        matrices[i].a10 = state_array[i][1];
        matrices[i].a20 = 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].a01 = excluded[0];
        matrices[i].a11 = 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) );
    for (int i = 0; i < N; i ++) {
        for (int j = 0; j < N; j ++) {
            if (i == j) continue ;
            dbg(i, j);
            dbg(matrixGrid[i][j]);
        }
    }
    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 ++)
            result = result.merge(matrixGrid[DV[j - 1]][DV[j]]);
        
        dbg(result);

        int answer = result.a00.distance;
        if (answer == 1e18) answer = -1;
        cout << answer << "\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Incorrect 1 ms 212 KB Output isn't correct
3 Halted 0 ms 0 KB -