Submission #798387

# Submission time Handle Problem Language Result Execution time Memory
798387 2023-07-30T16:32:36 Z hugo_pm Wild Boar (JOI18_wild_boar) C++17
62 / 100
18000 ms 603032 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});
		while (!pq.empty()) {
			auto [oldChemin, node] = pq.top();
			pq.pop();
			// oldChemin.len == dist[node]
			dbg(depart, node);
			if (!interesting(oldChemin, 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) {
			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 1 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 2 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 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 4 ms 376 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 376 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 2 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 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 4 ms 376 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 376 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 4 ms 640 KB Output is correct
27 Correct 10 ms 2352 KB Output is correct
28 Correct 15 ms 2400 KB Output is correct
29 Correct 439 ms 7332 KB Output is correct
30 Correct 426 ms 7012 KB Output is correct
31 Correct 365 ms 7252 KB Output is correct
32 Correct 407 ms 7308 KB Output is correct
33 Correct 438 ms 11260 KB Output is correct
34 Correct 449 ms 11456 KB Output is correct
35 Correct 381 ms 10712 KB Output is correct
36 Correct 385 ms 10060 KB Output is correct
37 Correct 487 ms 11856 KB Output is correct
38 Correct 399 ms 14552 KB Output is correct
39 Correct 424 ms 12328 KB Output is correct
40 Correct 354 ms 14628 KB Output is correct
41 Correct 369 ms 14584 KB Output is correct
42 Correct 391 ms 13720 KB Output is correct
43 Correct 291 ms 16184 KB Output is correct
44 Correct 305 ms 16052 KB Output is correct
45 Correct 76 ms 10976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 2 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 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 4 ms 376 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 376 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 4 ms 640 KB Output is correct
27 Correct 10 ms 2352 KB Output is correct
28 Correct 15 ms 2400 KB Output is correct
29 Correct 439 ms 7332 KB Output is correct
30 Correct 426 ms 7012 KB Output is correct
31 Correct 365 ms 7252 KB Output is correct
32 Correct 407 ms 7308 KB Output is correct
33 Correct 438 ms 11260 KB Output is correct
34 Correct 449 ms 11456 KB Output is correct
35 Correct 381 ms 10712 KB Output is correct
36 Correct 385 ms 10060 KB Output is correct
37 Correct 487 ms 11856 KB Output is correct
38 Correct 399 ms 14552 KB Output is correct
39 Correct 424 ms 12328 KB Output is correct
40 Correct 354 ms 14628 KB Output is correct
41 Correct 369 ms 14584 KB Output is correct
42 Correct 391 ms 13720 KB Output is correct
43 Correct 291 ms 16184 KB Output is correct
44 Correct 305 ms 16052 KB Output is correct
45 Correct 76 ms 10976 KB Output is correct
46 Correct 510 ms 19520 KB Output is correct
47 Correct 4738 ms 129132 KB Output is correct
48 Correct 6035 ms 231412 KB Output is correct
49 Correct 7162 ms 333992 KB Output is correct
50 Correct 7163 ms 341536 KB Output is correct
51 Correct 7185 ms 348620 KB Output is correct
52 Correct 8871 ms 402480 KB Output is correct
53 Correct 8889 ms 404052 KB Output is correct
54 Correct 8777 ms 400652 KB Output is correct
55 Correct 8771 ms 397788 KB Output is correct
56 Correct 9303 ms 449800 KB Output is correct
57 Correct 9861 ms 505236 KB Output is correct
58 Correct 9584 ms 533128 KB Output is correct
59 Correct 9815 ms 569868 KB Output is correct
60 Correct 9138 ms 588900 KB Output is correct
61 Correct 8780 ms 600644 KB Output is correct
62 Correct 8153 ms 603032 KB Output is correct
63 Correct 7779 ms 593288 KB Output is correct
64 Correct 2316 ms 377788 KB Output is correct
65 Correct 2357 ms 377792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 2 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 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 4 ms 376 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 376 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 4 ms 640 KB Output is correct
27 Correct 10 ms 2352 KB Output is correct
28 Correct 15 ms 2400 KB Output is correct
29 Correct 439 ms 7332 KB Output is correct
30 Correct 426 ms 7012 KB Output is correct
31 Correct 365 ms 7252 KB Output is correct
32 Correct 407 ms 7308 KB Output is correct
33 Correct 438 ms 11260 KB Output is correct
34 Correct 449 ms 11456 KB Output is correct
35 Correct 381 ms 10712 KB Output is correct
36 Correct 385 ms 10060 KB Output is correct
37 Correct 487 ms 11856 KB Output is correct
38 Correct 399 ms 14552 KB Output is correct
39 Correct 424 ms 12328 KB Output is correct
40 Correct 354 ms 14628 KB Output is correct
41 Correct 369 ms 14584 KB Output is correct
42 Correct 391 ms 13720 KB Output is correct
43 Correct 291 ms 16184 KB Output is correct
44 Correct 305 ms 16052 KB Output is correct
45 Correct 76 ms 10976 KB Output is correct
46 Correct 510 ms 19520 KB Output is correct
47 Correct 4738 ms 129132 KB Output is correct
48 Correct 6035 ms 231412 KB Output is correct
49 Correct 7162 ms 333992 KB Output is correct
50 Correct 7163 ms 341536 KB Output is correct
51 Correct 7185 ms 348620 KB Output is correct
52 Correct 8871 ms 402480 KB Output is correct
53 Correct 8889 ms 404052 KB Output is correct
54 Correct 8777 ms 400652 KB Output is correct
55 Correct 8771 ms 397788 KB Output is correct
56 Correct 9303 ms 449800 KB Output is correct
57 Correct 9861 ms 505236 KB Output is correct
58 Correct 9584 ms 533128 KB Output is correct
59 Correct 9815 ms 569868 KB Output is correct
60 Correct 9138 ms 588900 KB Output is correct
61 Correct 8780 ms 600644 KB Output is correct
62 Correct 8153 ms 603032 KB Output is correct
63 Correct 7779 ms 593288 KB Output is correct
64 Correct 2316 ms 377788 KB Output is correct
65 Correct 2357 ms 377792 KB Output is correct
66 Execution timed out 18086 ms 1748 KB Time limit exceeded
67 Halted 0 ms 0 KB -