Submission #726420

# Submission time Handle Problem Language Result Execution time Memory
726420 2023-04-18T21:47:49 Z GusterGoose27 Wild Boar (JOI18_wild_boar) C++17
100 / 100
5726 ms 631404 KB
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>

#define int ll

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, bool> plb;
typedef pair<ll, int> pli;

const int MAXN = 2000;
const int MAXM = 12000;
const int MAX_PATH = 1e5;
const ll inf = 1e18;

class path {
public:
	ll len;
	int s, e;
	path(ll l, int s, int e) : len(l), s(s), e(e) {}
	path() {}
};

vector<int> edges[MAXN];
pii init_edge[2*MAXN]; // dest, weight
vector<int> dual[MAXM];
int ecount = 0;

int make_edge() {
	return (ecount++);
}

path dist[MAXN][MAXN][5]; // 0 : normal, 1 : first diff, 2 : f/e, 3 : e, 4 : e/f
ll edge_dist[2*MAXN][2*MAXN];
bool vis[MAXM]; // bitset?
int order[MAX_PATH];
int n, m, t, l;

path min(path a, path b) {
	if (a.len <= b.len) return a;
	return b;
}

path comb(path a, path b) {
	assert(a.e != b.s);
	if (a.len == inf || b.len == inf) return path(inf, -1, -10);
	return path(a.len+b.len, a.s, b.e);
}

class stree {
public:
	int lp, rp;
	stree *l = nullptr;
	stree *r = nullptr;
	path seg[5];
	stree(int lv, int rv) {
		lp = lv;
		rp = rv;
		if (lp < rp) {
			int mid = (lp+rp)/2;
			l = new stree(lp, mid);
			r = new stree(mid+1, rp);
		}
		make();
	}
	int rdiff(int a) {
		if (a == 0) return 3;
		return 2;
	}
	void avoid(path seg[], int s, int e, int &a) { // best path avoiding these things
		if (seg[a].s == s) a = 1;
		if (seg[a].e == e) a = rdiff(a);
		if (seg[a].s == s) a++;
	}
	void make() { // really slow. Could probably be faster
		if (lp == rp) {
			for (int i = 0; i < 5; i++) seg[i] = dist[order[lp]][order[lp+1]][i];
			return;
		}
		for (int i = 0; i < 5; i++) {
			int a = 0, b = 0;
			int lv = -2, rv = -2;
			if (i == 1 || i == 2) lv = seg[0].s;
			if (i == 4) lv = seg[3].s;
			if (i == 2) rv = seg[1].e;
			if (i == 3 || i == 4) rv = seg[0].e;
			avoid(l->seg, lv, -2, a);
			avoid(r->seg, l->seg[a].e, rv, b);
			seg[i] = comb(l->seg[a], r->seg[b]);
			a = 0, b = 0;
			avoid(r->seg, -2, rv, b);
			avoid(l->seg, lv, r->seg[b].s, a);
			seg[i] = min(seg[i], comb(l->seg[a], r->seg[b]));
		}
	}
	void upd(int p) {
		if (lp > p || rp < p) return;
		if (lp == rp) return make();
		l->upd(p);
		r->upd(p);
		make();
	}
};

stree *tree;

void dijkstra(int s) {
	fill(edge_dist[s], edge_dist[s]+2*m, inf);
	fill(vis, vis+MAXM, 0); // could optimize by expanding dists to make pq smaller
	priority_queue<pli, vector<pli>, greater<pli>> pq;
	pq.push(pli(init_edge[s].second, s));
	while (!pq.empty()) {
		pli tp = pq.top();
		pq.pop();
		if (vis[tp.second]) continue;
		if (tp.second < 2*m) edge_dist[s][tp.second] = tp.first;
		vis[tp.second] = 1;
		for (int nxt: dual[tp.second]) {
			if (vis[nxt]) continue;
			ll ndist = tp.first+((nxt<2*m) ? init_edge[nxt].second : 0);
			pq.push(pli(ndist, nxt));
		}
	}
}

path make_opt(int i, int j, int s, int e) {
	path best(inf, -1, -10);
	for (int a: edges[i]) {
		if (a == s) continue;
		for (int b: edges[j]) {
			if (b == e) continue;
			best = min(best, path(edge_dist[a][b^1], a, b));
		}
	}
	return best;
}

signed main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> n >> m >> t >> l;
	for (int i = 0; i < m; i++) {
		int a, b, c;
		cin >> a >> b >> c; a--; b--;
		init_edge[2*i] = pii(b, c);
		init_edge[2*i+1] = pii(a, c);
		edges[a].push_back(make_edge());
		edges[b].push_back(make_edge());
	}
	for (int i = 0; i < n; i++) {
		vector<int> pref;
		vector<int> suf;
		for (int j = 0; j < edges[i].size(); j++) {
			pref.push_back(make_edge());
			dual[pref.back()].push_back(edges[i][j]);
		}
		for (int j = 0; j < edges[i].size(); j++) {
			suf.push_back(make_edge());
			dual[suf.back()].push_back(edges[i][j]);
		}
		for (int j = 1; j < edges[i].size(); j++) {
			dual[pref[j]].push_back(pref[j-1]);
			dual[suf[j-1]].push_back(suf[j]);
		}
		for (int j = 0; j < edges[i].size(); j++) {
			if (j)dual[edges[i][j]^1].push_back(pref[j-1]);
			if (j<edges[i].size()-1)dual[edges[i][j]^1].push_back(suf[j+1]);
		}
	}
	for (int i = 0; i < 2*m; i++) dijkstra(i); // much cleaner?
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			dist[i][j][0] = make_opt(i, j, -5, -5);
			dist[i][j][1] = make_opt(i, j, dist[i][j][0].s, -5);
			dist[i][j][2] = make_opt(i, j, dist[i][j][0].s, dist[i][j][1].e);
			dist[i][j][3] = make_opt(i, j, -5, dist[i][j][0].e);
			dist[i][j][4] = make_opt(i, j, dist[i][j][3].s, dist[i][j][0].e);
		}
	}
	for (int i = 0; i < l; i++) {
		cin >> order[i];
		order[i]--;
	}
	tree = new stree(0, l-2);
	for (int i = 0; i < t; i++) {
		int x, v; cin >> x >> v;
		x--; v--;
		order[x] = v;
		if (x) tree->upd(x-1);
		if (x < l-1) tree->upd(x);
		if (tree->seg[0].len == inf) cout << "-1\n";
		else cout << tree->seg[0].len << "\n";
	}
	return 0;
}

Compilation message

wild_boar.cpp: In function 'int main()':
wild_boar.cpp:154:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  154 |   for (int j = 0; j < edges[i].size(); j++) {
      |                   ~~^~~~~~~~~~~~~~~~~
wild_boar.cpp:158:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |   for (int j = 0; j < edges[i].size(); j++) {
      |                   ~~^~~~~~~~~~~~~~~~~
wild_boar.cpp:162:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |   for (int j = 1; j < edges[i].size(); j++) {
      |                   ~~^~~~~~~~~~~~~~~~~
wild_boar.cpp:166:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |   for (int j = 0; j < edges[i].size(); j++) {
      |                   ~~^~~~~~~~~~~~~~~~~
wild_boar.cpp:168:9: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  168 |    if (j<edges[i].size()-1)dual[edges[i][j]^1].push_back(suf[j+1]);
      |        ~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 788 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 1 ms 788 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 784 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 744 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 788 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 788 KB Output is correct
19 Correct 1 ms 788 KB Output is correct
20 Correct 1 ms 792 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 788 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 788 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 1 ms 788 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 784 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 744 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 788 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 788 KB Output is correct
19 Correct 1 ms 788 KB Output is correct
20 Correct 1 ms 792 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 788 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 3 ms 1236 KB Output is correct
27 Correct 39 ms 35752 KB Output is correct
28 Correct 38 ms 35844 KB Output is correct
29 Correct 138 ms 40012 KB Output is correct
30 Correct 133 ms 39972 KB Output is correct
31 Correct 130 ms 39908 KB Output is correct
32 Correct 131 ms 39864 KB Output is correct
33 Correct 139 ms 41692 KB Output is correct
34 Correct 131 ms 41640 KB Output is correct
35 Correct 113 ms 41732 KB Output is correct
36 Correct 118 ms 41568 KB Output is correct
37 Correct 142 ms 41676 KB Output is correct
38 Correct 117 ms 44024 KB Output is correct
39 Correct 112 ms 43968 KB Output is correct
40 Correct 115 ms 44016 KB Output is correct
41 Correct 124 ms 44040 KB Output is correct
42 Correct 98 ms 45528 KB Output is correct
43 Correct 95 ms 46836 KB Output is correct
44 Correct 108 ms 46896 KB Output is correct
45 Correct 62 ms 50224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 788 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 1 ms 788 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 784 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 744 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 788 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 788 KB Output is correct
19 Correct 1 ms 788 KB Output is correct
20 Correct 1 ms 792 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 788 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 3 ms 1236 KB Output is correct
27 Correct 39 ms 35752 KB Output is correct
28 Correct 38 ms 35844 KB Output is correct
29 Correct 138 ms 40012 KB Output is correct
30 Correct 133 ms 39972 KB Output is correct
31 Correct 130 ms 39908 KB Output is correct
32 Correct 131 ms 39864 KB Output is correct
33 Correct 139 ms 41692 KB Output is correct
34 Correct 131 ms 41640 KB Output is correct
35 Correct 113 ms 41732 KB Output is correct
36 Correct 118 ms 41568 KB Output is correct
37 Correct 142 ms 41676 KB Output is correct
38 Correct 117 ms 44024 KB Output is correct
39 Correct 112 ms 43968 KB Output is correct
40 Correct 115 ms 44016 KB Output is correct
41 Correct 124 ms 44040 KB Output is correct
42 Correct 98 ms 45528 KB Output is correct
43 Correct 95 ms 46836 KB Output is correct
44 Correct 108 ms 46896 KB Output is correct
45 Correct 62 ms 50224 KB Output is correct
46 Correct 401 ms 18484 KB Output is correct
47 Correct 5589 ms 190188 KB Output is correct
48 Correct 4891 ms 237248 KB Output is correct
49 Correct 4559 ms 280332 KB Output is correct
50 Correct 4544 ms 280344 KB Output is correct
51 Correct 4645 ms 280324 KB Output is correct
52 Correct 4745 ms 280508 KB Output is correct
53 Correct 4752 ms 280652 KB Output is correct
54 Correct 4829 ms 280584 KB Output is correct
55 Correct 4942 ms 280520 KB Output is correct
56 Correct 4965 ms 305568 KB Output is correct
57 Correct 4788 ms 332956 KB Output is correct
58 Correct 4322 ms 362684 KB Output is correct
59 Correct 3955 ms 394864 KB Output is correct
60 Correct 3588 ms 429152 KB Output is correct
61 Correct 3354 ms 466072 KB Output is correct
62 Correct 3159 ms 505224 KB Output is correct
63 Correct 2804 ms 546700 KB Output is correct
64 Correct 933 ms 628548 KB Output is correct
65 Correct 972 ms 628648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 788 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 2 ms 724 KB Output is correct
6 Correct 1 ms 788 KB Output is correct
7 Correct 1 ms 724 KB Output is correct
8 Correct 1 ms 724 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 784 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 2 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 744 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 788 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 788 KB Output is correct
19 Correct 1 ms 788 KB Output is correct
20 Correct 1 ms 792 KB Output is correct
21 Correct 1 ms 724 KB Output is correct
22 Correct 1 ms 724 KB Output is correct
23 Correct 1 ms 788 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 3 ms 1236 KB Output is correct
27 Correct 39 ms 35752 KB Output is correct
28 Correct 38 ms 35844 KB Output is correct
29 Correct 138 ms 40012 KB Output is correct
30 Correct 133 ms 39972 KB Output is correct
31 Correct 130 ms 39908 KB Output is correct
32 Correct 131 ms 39864 KB Output is correct
33 Correct 139 ms 41692 KB Output is correct
34 Correct 131 ms 41640 KB Output is correct
35 Correct 113 ms 41732 KB Output is correct
36 Correct 118 ms 41568 KB Output is correct
37 Correct 142 ms 41676 KB Output is correct
38 Correct 117 ms 44024 KB Output is correct
39 Correct 112 ms 43968 KB Output is correct
40 Correct 115 ms 44016 KB Output is correct
41 Correct 124 ms 44040 KB Output is correct
42 Correct 98 ms 45528 KB Output is correct
43 Correct 95 ms 46836 KB Output is correct
44 Correct 108 ms 46896 KB Output is correct
45 Correct 62 ms 50224 KB Output is correct
46 Correct 401 ms 18484 KB Output is correct
47 Correct 5589 ms 190188 KB Output is correct
48 Correct 4891 ms 237248 KB Output is correct
49 Correct 4559 ms 280332 KB Output is correct
50 Correct 4544 ms 280344 KB Output is correct
51 Correct 4645 ms 280324 KB Output is correct
52 Correct 4745 ms 280508 KB Output is correct
53 Correct 4752 ms 280652 KB Output is correct
54 Correct 4829 ms 280584 KB Output is correct
55 Correct 4942 ms 280520 KB Output is correct
56 Correct 4965 ms 305568 KB Output is correct
57 Correct 4788 ms 332956 KB Output is correct
58 Correct 4322 ms 362684 KB Output is correct
59 Correct 3955 ms 394864 KB Output is correct
60 Correct 3588 ms 429152 KB Output is correct
61 Correct 3354 ms 466072 KB Output is correct
62 Correct 3159 ms 505224 KB Output is correct
63 Correct 2804 ms 546700 KB Output is correct
64 Correct 933 ms 628548 KB Output is correct
65 Correct 972 ms 628648 KB Output is correct
66 Correct 36 ms 33440 KB Output is correct
67 Correct 384 ms 163212 KB Output is correct
68 Correct 965 ms 596140 KB Output is correct
69 Correct 1135 ms 597264 KB Output is correct
70 Correct 1362 ms 629996 KB Output is correct
71 Correct 5726 ms 192512 KB Output is correct
72 Correct 5282 ms 239676 KB Output is correct
73 Correct 5055 ms 283200 KB Output is correct
74 Correct 5006 ms 283068 KB Output is correct
75 Correct 4977 ms 283260 KB Output is correct
76 Correct 4770 ms 282816 KB Output is correct
77 Correct 4908 ms 282760 KB Output is correct
78 Correct 4817 ms 282720 KB Output is correct
79 Correct 4626 ms 335560 KB Output is correct
80 Correct 4485 ms 365308 KB Output is correct
81 Correct 3819 ms 396956 KB Output is correct
82 Correct 3910 ms 431880 KB Output is correct
83 Correct 3400 ms 468248 KB Output is correct
84 Correct 3237 ms 549424 KB Output is correct
85 Correct 1476 ms 631404 KB Output is correct