Submission #806648

# Submission time Handle Problem Language Result Execution time Memory
806648 2023-08-04T08:31:28 Z thimote75 Wild Boar (JOI18_wild_boar) C++14
0 / 100
1 ms 340 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;
    }
    bool operator< (const State &other) const {
        return distance < other.distance;
    }
};

string to_string (State state) {
    if (state.distance == 1e18)
        return "State#";
    return "State[" + to_string(state.source + 1) + " => " + to_string(state.target + 1) + ", " + to_string(state.distance) + "]";
}
int modify (vector<State> &states, State new_state, bool based_on_target) {
    for (int i = 0; i < 2; i ++) {
        if (states[i].source   == new_state.source
         && states[i].distance == new_state.distance
         && states[i].target   == new_state.target) {
            return -1;
        }
    }

    vector<State> state_array;
    for (State a : states) state_array.push_back(a);
    state_array.push_back(new_state);
    sort(state_array.begin(), state_array.end());

    set<int> source_seen;
    states.clear();
    for (int i = 0; i < state_array.size(); i ++) {
        int src = based_on_target ? state_array[i].target : state_array[i].source;
        if (source_seen.find(src) == source_seen.end() && states.size() < 3)
            states.push_back(state_array[i]);

        if (src != -1) source_seen.insert(src);
    }

    while (states.size() < 3) states.push_back(State());

    for (int i = 0; i < 2; i ++) {
        if (states[i].source   == new_state.source
         && states[i].distance == new_state.distance
         && states[i].target   == new_state.target) {
            return i;
        }
    }

    return -1;
}

struct Matrix {
    vector<State> pcc     = vector<State>(3);
    vector<State> pcc_tar = vector<State>(3);

    vector<State> all () {
        return { pcc[0], pcc[1], pcc[2], pcc_tar[1], pcc_tar[2] };
    }

    Matrix merge (Matrix &other) {
        dbg(other);
        vector<State> simple(3);
        for (State s1 : all()) {
            for (State s2 : other.all()) {
                if (s1.target == s2.source) continue ;

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

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

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

        Matrix res;
        res.pcc = simple;
        res.pcc_tar = { simple[0], exclu[0], exclu[1] };

        dbg(res);

        return res;
    }
};
string to_string (Matrix matrix) {
    return "Mat" + to_string(matrix.all());
}
using vMatrix = vector<Matrix>;
using ti = pair<pair<int, int>, pair<int, bool>>;
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, false } });
    q.push({ { 0, 0 }, { start, true  } });

    vMatrix matrices(N);
    matrices[start].pcc    [0].distance = 0;
    matrices[start].pcc_tar[0].distance = 0;
    
    dbg(start);
    while (q.size() != 0) {
        const auto data = q.top(); q.pop();
        int srcId = data.first.second;
        int curr  = data.second.first;
        bool isTar = data.second.second;

        State state = isTar ? matrices[curr].pcc_tar[srcId] : matrices[curr].pcc[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(matrices[road.first].pcc, next_state, false);
            if (res != -1)
                q.push({ { next_state.distance, res }, { road.first, false } });

            res = modify(matrices[road.first].pcc_tar, next_state, true);
            if (res != -1)
                q.push({ { next_state.distance, res }, { road.first, true } });
        }
    }

    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.pcc[0].distance;
        if (answer == 1e18) answer = -1;
        cout << answer << "\n";
    }
}

Compilation message

wild_boar.cpp: In function 'long long int modify(std::vector<State>&, State, bool)':
wild_boar.cpp:75:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<State>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |     for (int i = 0; i < state_array.size(); i ++) {
      |                     ~~^~~~~~~~~~~~~~~~~~~~
# 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 212 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 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 220 KB Output is correct
9 Incorrect 1 ms 320 KB Output isn't correct
10 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 212 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 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 220 KB Output is correct
9 Incorrect 1 ms 320 KB Output isn't correct
10 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 212 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 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 220 KB Output is correct
9 Incorrect 1 ms 320 KB Output isn't correct
10 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 212 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 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 220 KB Output is correct
9 Incorrect 1 ms 320 KB Output isn't correct
10 Halted 0 ms 0 KB -