Submission #798394

# Submission time Handle Problem Language Result Execution time Memory
798394 2023-07-30T16:39:03 Z hugo_pm Wild Boar (JOI18_wild_boar) C++17
62 / 100
18000 ms 602532 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define sz(v) ((int)((v).size()))

template<typename T>
void chmax(T &x, const T &v) { if (x < v) x = v; }
template<typename T>
void chmin(T &x, const T &v) { if (x > v) x = v; }

using pii = pair<int, int>;
using vi = vector<int>;

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

const int MAX_N = 2005, MAX_ARRET = 1e5+5;
int nbNode, nbEdge, nbReq, nbArret;
vector<int> adj[MAX_N];
int xorNode[MAX_N];
int poids[MAX_N];
int arrets[MAX_ARRET];
int vus[MAX_N][MAX_N];
int VuTime = 0;

struct Chemin {
	int len, fst, lst;
	bool operator<(const Chemin &oth) const {
		return tie(len, fst, lst) < tie(oth.len, oth.fst, oth.lst);
	}
	bool operator==(const Chemin &oth) const {
		return tie(len, fst, lst) == tie(oth.len, oth.fst, oth.lst);
	}
};

template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;

string to_string(const Chemin c) {
	return "(len=" + to_string(c.len) + ", fst=" + to_string(c.fst) + ", lst=" + to_string(c.lst) + ")";
}

int repondre() {
	vector<vector<vector<Chemin>>> matrice(nbNode);
	vector<int> cntFirst(nbEdge+1), cntLast(nbEdge+1);
	auto compressTab = [&](vector<Chemin> &chemins) {
		sort(all(chemins));
		VuTime++;
		vector<Chemin> ret;
		for (Chemin chem : chemins) {
			if (ret.size() > 8)
				break;
			dbg(chem);
			if (cntFirst[1+chem.fst] == 3 or cntLast[1+chem.lst] == 3 or vus[1+chem.fst][1+chem.lst] == VuTime)
				continue;
			ret.push_back(chem);
			cntFirst[1+chem.fst]++;
			cntLast[1+chem.lst]++;
			vus[1+chem.fst][1+chem.lst] = VuTime;
		}
		for (Chemin chem : ret) {
			cntFirst[1+chem.fst]--;
			cntLast[1+chem.lst]--;
		}
		return ret;
	};
	auto interesting = [&] (const Chemin &add, vector<Chemin> &V) {
		V.push_back(add);
		V = compressTab(V);
		return (find(all(V), add) != end(V));
	};
	rep(depart, 0, nbNode) {
		auto& cand = matrice[depart];
		cand.resize(nbNode);
		cand[depart].push_back({0, -1, -1});
		minpq<pair<Chemin, int>> pq;
		pq.push({cand[depart].back(), depart});
		vector<int> nbVus(nbNode);
		while (!pq.empty()) {
			auto [oldChemin, node] = pq.top();
			pq.pop();
			nbVus[node]++;
			// oldChemin.len == dist[node]
			dbg(depart, node);
			if (find(all(cand[node]), oldChemin) == end(cand[node])) {
				continue;
			}
			for (int iEdge : adj[node]) {
				if (iEdge == oldChemin.lst) continue;
				int voisin = xorNode[iEdge]^node;
				Chemin nouvChemin {
					oldChemin.len + poids[iEdge],
					(oldChemin.fst == -1 ? iEdge : oldChemin.fst),
					iEdge
				};
				if (interesting(nouvChemin, cand[voisin])) {
					dbg(voisin, cand[voisin], nouvChemin);
					pq.push({nouvChemin, voisin});
				}
			}
		}
		rep(node, 0, nbNode) {
			cand[node] = compressTab(cand[node]);
			assert(sz(cand[node]) <= 9);
		}
	}
	vector<Chemin> candidats;
	candidats.push_back({0, -1, -1});
	rep(iRawArret, 1, nbArret) {
		int prev = arrets[iRawArret-1];
		int cur = arrets[iRawArret];
		dbg(prev+1, cur+1);
		dbg(matrice[prev][cur]);
		vector<Chemin> nv;
		for (Chemin c1 : candidats) for (Chemin c2 : matrice[prev][cur]) {
			if (c1.lst != c2.fst) {
				int newFst = (c1.fst == -1 ? c2.fst : c1.fst);
				nv.push_back({c1.len + c2.len, newFst, c2.lst});
			}
		}
		candidats = compressTab(nv);
		assert(sz(candidats) <= 9);
		dbg(candidats);
	}
	if (candidats.empty()) return -1;
	return min_element(all(candidats))->len;
}
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> nbNode >> nbEdge >> nbReq >> nbArret;
	rep(iEdge, 0, nbEdge) {
		int u, v; cin >> u >> v;
		--u, --v;
		cin >> poids[iEdge];
		xorNode[iEdge] = u^v;
		adj[u].push_back(iEdge);
		adj[v].push_back(iEdge);
	}
	rep(iArret, 0, nbArret) {
		cin >> arrets[iArret];
		--arrets[iArret];
	}
	rep(iReq, 0, nbReq) {
		int indice, val;
		cin >> indice >> val;
		--indice, --val;
		arrets[indice] = val;
		cout << repondre() << '\n';
	}
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 424 KB Output is correct
16 Correct 1 ms 372 KB Output is correct
17 Correct 1 ms 376 KB Output is correct
18 Correct 1 ms 372 KB Output is correct
19 Correct 1 ms 380 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 372 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 380 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 424 KB Output is correct
16 Correct 1 ms 372 KB Output is correct
17 Correct 1 ms 376 KB Output is correct
18 Correct 1 ms 372 KB Output is correct
19 Correct 1 ms 380 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 372 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 380 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 3 ms 724 KB Output is correct
27 Correct 9 ms 2516 KB Output is correct
28 Correct 8 ms 2576 KB Output is correct
29 Correct 352 ms 7508 KB Output is correct
30 Correct 363 ms 6984 KB Output is correct
31 Correct 330 ms 7392 KB Output is correct
32 Correct 306 ms 7340 KB Output is correct
33 Correct 349 ms 11568 KB Output is correct
34 Correct 355 ms 11696 KB Output is correct
35 Correct 304 ms 10680 KB Output is correct
36 Correct 302 ms 10236 KB Output is correct
37 Correct 366 ms 12064 KB Output is correct
38 Correct 344 ms 14892 KB Output is correct
39 Correct 316 ms 12408 KB Output is correct
40 Correct 254 ms 14840 KB Output is correct
41 Correct 251 ms 14712 KB Output is correct
42 Correct 295 ms 13900 KB Output is correct
43 Correct 200 ms 16288 KB Output is correct
44 Correct 217 ms 16204 KB Output is correct
45 Correct 57 ms 11128 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 424 KB Output is correct
16 Correct 1 ms 372 KB Output is correct
17 Correct 1 ms 376 KB Output is correct
18 Correct 1 ms 372 KB Output is correct
19 Correct 1 ms 380 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 372 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 380 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 3 ms 724 KB Output is correct
27 Correct 9 ms 2516 KB Output is correct
28 Correct 8 ms 2576 KB Output is correct
29 Correct 352 ms 7508 KB Output is correct
30 Correct 363 ms 6984 KB Output is correct
31 Correct 330 ms 7392 KB Output is correct
32 Correct 306 ms 7340 KB Output is correct
33 Correct 349 ms 11568 KB Output is correct
34 Correct 355 ms 11696 KB Output is correct
35 Correct 304 ms 10680 KB Output is correct
36 Correct 302 ms 10236 KB Output is correct
37 Correct 366 ms 12064 KB Output is correct
38 Correct 344 ms 14892 KB Output is correct
39 Correct 316 ms 12408 KB Output is correct
40 Correct 254 ms 14840 KB Output is correct
41 Correct 251 ms 14712 KB Output is correct
42 Correct 295 ms 13900 KB Output is correct
43 Correct 200 ms 16288 KB Output is correct
44 Correct 217 ms 16204 KB Output is correct
45 Correct 57 ms 11128 KB Output is correct
46 Correct 340 ms 19480 KB Output is correct
47 Correct 3766 ms 129240 KB Output is correct
48 Correct 4829 ms 231424 KB Output is correct
49 Correct 5938 ms 333876 KB Output is correct
50 Correct 5877 ms 341224 KB Output is correct
51 Correct 5826 ms 348536 KB Output is correct
52 Correct 7154 ms 402672 KB Output is correct
53 Correct 7190 ms 404208 KB Output is correct
54 Correct 6920 ms 400624 KB Output is correct
55 Correct 7081 ms 397884 KB Output is correct
56 Correct 7219 ms 449924 KB Output is correct
57 Correct 7566 ms 505468 KB Output is correct
58 Correct 7338 ms 532720 KB Output is correct
59 Correct 7465 ms 569160 KB Output is correct
60 Correct 6986 ms 588252 KB Output is correct
61 Correct 6861 ms 599952 KB Output is correct
62 Correct 6350 ms 602532 KB Output is correct
63 Correct 5772 ms 592700 KB Output is correct
64 Correct 1825 ms 377464 KB Output is correct
65 Correct 1771 ms 377460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 424 KB Output is correct
16 Correct 1 ms 372 KB Output is correct
17 Correct 1 ms 376 KB Output is correct
18 Correct 1 ms 372 KB Output is correct
19 Correct 1 ms 380 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 372 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 380 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 3 ms 724 KB Output is correct
27 Correct 9 ms 2516 KB Output is correct
28 Correct 8 ms 2576 KB Output is correct
29 Correct 352 ms 7508 KB Output is correct
30 Correct 363 ms 6984 KB Output is correct
31 Correct 330 ms 7392 KB Output is correct
32 Correct 306 ms 7340 KB Output is correct
33 Correct 349 ms 11568 KB Output is correct
34 Correct 355 ms 11696 KB Output is correct
35 Correct 304 ms 10680 KB Output is correct
36 Correct 302 ms 10236 KB Output is correct
37 Correct 366 ms 12064 KB Output is correct
38 Correct 344 ms 14892 KB Output is correct
39 Correct 316 ms 12408 KB Output is correct
40 Correct 254 ms 14840 KB Output is correct
41 Correct 251 ms 14712 KB Output is correct
42 Correct 295 ms 13900 KB Output is correct
43 Correct 200 ms 16288 KB Output is correct
44 Correct 217 ms 16204 KB Output is correct
45 Correct 57 ms 11128 KB Output is correct
46 Correct 340 ms 19480 KB Output is correct
47 Correct 3766 ms 129240 KB Output is correct
48 Correct 4829 ms 231424 KB Output is correct
49 Correct 5938 ms 333876 KB Output is correct
50 Correct 5877 ms 341224 KB Output is correct
51 Correct 5826 ms 348536 KB Output is correct
52 Correct 7154 ms 402672 KB Output is correct
53 Correct 7190 ms 404208 KB Output is correct
54 Correct 6920 ms 400624 KB Output is correct
55 Correct 7081 ms 397884 KB Output is correct
56 Correct 7219 ms 449924 KB Output is correct
57 Correct 7566 ms 505468 KB Output is correct
58 Correct 7338 ms 532720 KB Output is correct
59 Correct 7465 ms 569160 KB Output is correct
60 Correct 6986 ms 588252 KB Output is correct
61 Correct 6861 ms 599952 KB Output is correct
62 Correct 6350 ms 602532 KB Output is correct
63 Correct 5772 ms 592700 KB Output is correct
64 Correct 1825 ms 377464 KB Output is correct
65 Correct 1771 ms 377460 KB Output is correct
66 Execution timed out 18068 ms 1620 KB Time limit exceeded
67 Halted 0 ms 0 KB -