Submission #767667

# Submission time Handle Problem Language Result Execution time Memory
767667 2023-06-27T03:03:08 Z qwerasdfzxcl Wild Boar (JOI18_wild_boar) C++17
100 / 100
3709 ms 229564 KB
#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
constexpr ll INF = 4e18;
 
vector<pair<int, int>> adj0[6060];
pair<int, int> Edge[2020];
ll dist0[4040][4040], res0[4040];
int visited0[6060], F[2020];
ll (*dist)[4040];
 
vector<int> in[2020], out[2020];
int X[100100];
ll dp[100100][4];
 
struct Node{
	ll W[4][4];
	int S[4], E[4];
 
	Node(){}
 
	ll ans(){
		ll ret = INF;
		for (int i=0;i<4;i++){
			for (int j=0;j<4;j++){
				ret = min(ret, W[i][j]);
			}
		}
 
		return ret==INF?-1:ret;
	}
 
	Node operator + (const Node &N) const{
		Node ret;
        memset(ret.W[0], 0x3f, sizeof(ll) * 16);
        
        for (int k2=0;k2<4;k2++){
            for (int i=0;i<4;i++){
                ll mn = INF;
                for (int k1=0;k1<4;k1++) if (abs(E[k1])!=abs(N.S[k2])) mn = min(mn, W[i][k1]);
                for (int j=0;j<4;j++) ret.W[i][j] = min(ret.W[i][j], mn + N.W[k2][j]);
            }
            ret.S[k2] = S[k2];
            ret.E[k2] = N.E[k2];
        }
        
		return ret;
	}
};
 
Node initW(int p);
 
struct Seg{
	Node tree[400400];
	void init(int i, int l, int r){
		if (l==r){
			tree[i] = initW(l);
			return;
		} 
		
		int m = (l+r)>>1;
		init(i<<1, l, m); init(i<<1|1, m+1, r);
		tree[i] = tree[i<<1] + tree[i<<1|1];
	}
 
	void update(int i, int l, int r, int p){
		if (r<p || p<l) return;
		if (l==r){
			tree[i] = initW(l);
			return;
		}
 
		int m = (l+r)>>1;
		update(i<<1, l, m, p); update(i<<1|1, m+1, r, p);
		tree[i] = tree[i<<1] + tree[i<<1|1];
	}
 
}tree;
 
void dijkstra(int m, int s, ll (*dist)[4040], ll *res, vector<pair<int, int>> *adj){
	int *visited = visited0 + 2020;
	fill(res-2020, res+2020, INF);
	fill(visited-2020, visited+4040, -1);
 
	priority_queue<array<ll, 3>, vector<array<ll, 3>>, greater<array<ll, 3>>> pq;
	vector<array<ll, 3>> st;
 
	res[s] = F[abs(s)];
	pq.push({F[abs(s)], s, 0});
 
	while(!pq.empty() || !st.empty()){
		array<ll, 3> tmp;
		if (!st.empty()) {tmp = st.back(); st.pop_back();}
		else {tmp = pq.top(); pq.pop();}
 
		auto [dist, cur, p] = tmp;
		
		if (visited[cur]==-2) continue;
		if (visited[cur]==-1){
			if (cur <= m){
				// printf(" dist[%lld] = %lld\n", cur, res[cur]);
				visited[cur] = -2;
				for (auto &[v, w]:adj[cur]) if (visited[v]!=-2){
					st.push_back({dist, v, cur});
				}
			}
 
			else{
				visited[cur] = abs(p);
				for (auto &[v, w]:adj[cur]) if (visited[v]!=-2 && abs(v)!=abs(p)){
					res[v] = dist + w;
					pq.push({res[v], v, cur});
				}
			}
		}
 
		else{
			for (auto &[v, w]:adj[cur]) if (abs(v)==visited[cur] && visited[v]!=-2){
				res[v] = dist + w;
				pq.push({res[v], v, cur});
			}
 
			visited[cur] = -2;
		}
	}
 
 
}
 
map<int, Node> mp[2020];
Node initW(int p){
	vector<ll> W;
	vector<int> S, E;
	if (mp[X[p]].find(X[p+1])!=mp[X[p]].end()){
		return mp[X[p]][X[p+1]];
	}
 
	W.push_back(INF);
	S.push_back(-1);
	E.push_back(-1);
 
	for (auto &s1:out[X[p]]){
		for (auto &e1:in[X[p+1]]){
			if (W.back() > dist[s1][e1]){
				W.back() = dist[s1][e1];
				S.back() = s1;
				E.back() = e1;
			}
		}
	}
 
	W.push_back(INF);
	S.push_back(-1);
	E.push_back(-1);
 
	for (auto &s2:out[X[p]]) if (s2!=S[0]){
		for (auto &e2:in[X[p+1]]) if (e2!=E[0]){
			if (W.back() > dist[s2][e2]){
				W.back() = dist[s2][e2];
				S.back() = s2;
				E.back() = e2;
			}
		}
	}
 
	W.push_back(INF);
	S.push_back(-1);
	E.push_back(-1);
 
	for (auto &s3:out[X[p]]) if (s3!=S[0]){
		for (auto &e3:in[X[p+1]]) if (e3!=E[1]){
			if (W.back() > dist[s3][e3]){
				W.back() = dist[s3][e3];
				S.back() = s3;
				E.back() = e3;
			}
		}
	}
 
	W.push_back(INF);
	S.push_back(-1);
	E.push_back(-1);
 
	for (auto &s4:out[X[p]]) if (s4!=S[1]){
		for (auto &e4:in[X[p+1]]) if (e4!=E[0]){
			if (W.back() > dist[s4][e4]){
				W.back() = dist[s4][e4];
				S.back() = s4;
				E.back() = e4;
			}
		}
	}
 
	Node ret;
	for (int i=0;i<4;i++){
		for (int j=0;j<4;j++){
			if (i!=j) ret.W[i][j] = INF;
			else ret.W[i][j] = W[i];
		}
 
		ret.S[i] = S[i];
		ret.E[i] = E[i];
	}
	mp[X[p]][X[p+1]] = ret;
	return ret;
}
 
int main(){
	int n, m, q, l;
	scanf("%d %d %d %d", &n, &m, &q, &l);
 
	vector<pair<int, int>> *adj = adj0 + m;
	for (int i=1;i<=m;i++){
		int x, y, z;
		scanf("%d %d %d", &x, &y, &z);
		Edge[i] = {x, y}; F[i] = z;
 
		adj[i].emplace_back(y+m, 0);
		adj[-i].emplace_back(x+m, 0);
 
		adj[x+m].emplace_back(i, z);
		adj[y+m].emplace_back(-i, z);
 
		out[x].push_back(i);
		out[y].push_back(-i);
 
		in[x].push_back(-i);
		in[y].push_back(i);
	}
 
	dist = (ll (*)[4040])(&(dist0[2020][2020]));
	ll *res = res0 + 2020;
 
	for (int i=1;i<=m;i++){
		dijkstra(m, i, dist, res, adj);
		for (int j=-m;j<=m;j++) dist[i][j] = res[j];
		
		dijkstra(m, -i, dist, res, adj);
		for (int j=-m;j<=m;j++) dist[-i][j] = res[j];
	}
 
	for (int i=1;i<=l;i++) scanf("%d", X+i);
 
	tree.init(1, 1, l-1);
	while(q--){
		int pp, qq;
		scanf("%d %d", &pp, &qq);
		X[pp] = qq;
		if (pp > 1) tree.update(1, 1, l-1, pp-1);
		if (pp < l) tree.update(1, 1, l-1, pp);
 
		printf("%lld\n", tree.tree[1].ans());
	}
}

Compilation message

wild_boar.cpp: In function 'int main()':
wild_boar.cpp:211:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  211 |  scanf("%d %d %d %d", &n, &m, &q, &l);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:216:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  216 |   scanf("%d %d %d", &x, &y, &z);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:243:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  243 |  for (int i=1;i<=l;i++) scanf("%d", X+i);
      |                         ~~~~~^~~~~~~~~~~
wild_boar.cpp:248:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  248 |   scanf("%d %d", &pp, &qq);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 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 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 724 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 724 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 724 KB Output is correct
24 Correct 1 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 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 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 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 724 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 724 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 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 2 ms 1108 KB Output is correct
27 Correct 59 ms 45276 KB Output is correct
28 Correct 55 ms 45164 KB Output is correct
29 Correct 109 ms 49404 KB Output is correct
30 Correct 85 ms 47324 KB Output is correct
31 Correct 96 ms 47340 KB Output is correct
32 Correct 94 ms 47408 KB Output is correct
33 Correct 120 ms 51920 KB Output is correct
34 Correct 118 ms 51812 KB Output is correct
35 Correct 77 ms 47412 KB Output is correct
36 Correct 85 ms 47352 KB Output is correct
37 Correct 126 ms 51916 KB Output is correct
38 Correct 131 ms 54756 KB Output is correct
39 Correct 89 ms 47308 KB Output is correct
40 Correct 128 ms 54776 KB Output is correct
41 Correct 135 ms 54864 KB Output is correct
42 Correct 77 ms 47388 KB Output is correct
43 Correct 132 ms 57552 KB Output is correct
44 Correct 167 ms 57548 KB Output is correct
45 Correct 114 ms 59724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 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 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 724 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 724 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 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 2 ms 1108 KB Output is correct
27 Correct 59 ms 45276 KB Output is correct
28 Correct 55 ms 45164 KB Output is correct
29 Correct 109 ms 49404 KB Output is correct
30 Correct 85 ms 47324 KB Output is correct
31 Correct 96 ms 47340 KB Output is correct
32 Correct 94 ms 47408 KB Output is correct
33 Correct 120 ms 51920 KB Output is correct
34 Correct 118 ms 51812 KB Output is correct
35 Correct 77 ms 47412 KB Output is correct
36 Correct 85 ms 47352 KB Output is correct
37 Correct 126 ms 51916 KB Output is correct
38 Correct 131 ms 54756 KB Output is correct
39 Correct 89 ms 47308 KB Output is correct
40 Correct 128 ms 54776 KB Output is correct
41 Correct 135 ms 54864 KB Output is correct
42 Correct 77 ms 47388 KB Output is correct
43 Correct 132 ms 57552 KB Output is correct
44 Correct 167 ms 57548 KB Output is correct
45 Correct 114 ms 59724 KB Output is correct
46 Correct 134 ms 12612 KB Output is correct
47 Correct 2396 ms 168988 KB Output is correct
48 Correct 2288 ms 168940 KB Output is correct
49 Correct 2361 ms 168932 KB Output is correct
50 Correct 2320 ms 168912 KB Output is correct
51 Correct 2304 ms 168900 KB Output is correct
52 Correct 2453 ms 188192 KB Output is correct
53 Correct 2455 ms 188240 KB Output is correct
54 Correct 2505 ms 188184 KB Output is correct
55 Correct 2635 ms 188232 KB Output is correct
56 Correct 2471 ms 188380 KB Output is correct
57 Correct 2487 ms 188616 KB Output is correct
58 Correct 2442 ms 188544 KB Output is correct
59 Correct 2391 ms 188708 KB Output is correct
60 Correct 2390 ms 188788 KB Output is correct
61 Correct 2335 ms 188780 KB Output is correct
62 Correct 2265 ms 189120 KB Output is correct
63 Correct 2152 ms 188964 KB Output is correct
64 Correct 743 ms 189068 KB Output is correct
65 Correct 713 ms 189004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 724 KB Output is correct
2 Correct 1 ms 724 KB Output is correct
3 Correct 1 ms 724 KB Output is correct
4 Correct 1 ms 724 KB Output is correct
5 Correct 1 ms 724 KB Output is correct
6 Correct 1 ms 724 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 724 KB Output is correct
11 Correct 1 ms 724 KB Output is correct
12 Correct 1 ms 724 KB Output is correct
13 Correct 1 ms 724 KB Output is correct
14 Correct 1 ms 724 KB Output is correct
15 Correct 1 ms 724 KB Output is correct
16 Correct 1 ms 724 KB Output is correct
17 Correct 1 ms 724 KB Output is correct
18 Correct 1 ms 724 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 1 ms 724 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 724 KB Output is correct
24 Correct 1 ms 724 KB Output is correct
25 Correct 1 ms 724 KB Output is correct
26 Correct 2 ms 1108 KB Output is correct
27 Correct 59 ms 45276 KB Output is correct
28 Correct 55 ms 45164 KB Output is correct
29 Correct 109 ms 49404 KB Output is correct
30 Correct 85 ms 47324 KB Output is correct
31 Correct 96 ms 47340 KB Output is correct
32 Correct 94 ms 47408 KB Output is correct
33 Correct 120 ms 51920 KB Output is correct
34 Correct 118 ms 51812 KB Output is correct
35 Correct 77 ms 47412 KB Output is correct
36 Correct 85 ms 47352 KB Output is correct
37 Correct 126 ms 51916 KB Output is correct
38 Correct 131 ms 54756 KB Output is correct
39 Correct 89 ms 47308 KB Output is correct
40 Correct 128 ms 54776 KB Output is correct
41 Correct 135 ms 54864 KB Output is correct
42 Correct 77 ms 47388 KB Output is correct
43 Correct 132 ms 57552 KB Output is correct
44 Correct 167 ms 57548 KB Output is correct
45 Correct 114 ms 59724 KB Output is correct
46 Correct 134 ms 12612 KB Output is correct
47 Correct 2396 ms 168988 KB Output is correct
48 Correct 2288 ms 168940 KB Output is correct
49 Correct 2361 ms 168932 KB Output is correct
50 Correct 2320 ms 168912 KB Output is correct
51 Correct 2304 ms 168900 KB Output is correct
52 Correct 2453 ms 188192 KB Output is correct
53 Correct 2455 ms 188240 KB Output is correct
54 Correct 2505 ms 188184 KB Output is correct
55 Correct 2635 ms 188232 KB Output is correct
56 Correct 2471 ms 188380 KB Output is correct
57 Correct 2487 ms 188616 KB Output is correct
58 Correct 2442 ms 188544 KB Output is correct
59 Correct 2391 ms 188708 KB Output is correct
60 Correct 2390 ms 188788 KB Output is correct
61 Correct 2335 ms 188780 KB Output is correct
62 Correct 2265 ms 189120 KB Output is correct
63 Correct 2152 ms 188964 KB Output is correct
64 Correct 743 ms 189068 KB Output is correct
65 Correct 713 ms 189004 KB Output is correct
66 Correct 48 ms 42572 KB Output is correct
67 Correct 598 ms 77036 KB Output is correct
68 Correct 538 ms 127604 KB Output is correct
69 Correct 1025 ms 167088 KB Output is correct
70 Correct 1637 ms 228092 KB Output is correct
71 Correct 3104 ms 170352 KB Output is correct
72 Correct 2988 ms 170368 KB Output is correct
73 Correct 3699 ms 223252 KB Output is correct
74 Correct 3627 ms 223252 KB Output is correct
75 Correct 3709 ms 223184 KB Output is correct
76 Correct 2980 ms 170428 KB Output is correct
77 Correct 2885 ms 170392 KB Output is correct
78 Correct 2963 ms 170296 KB Output is correct
79 Correct 3561 ms 225496 KB Output is correct
80 Correct 3604 ms 226388 KB Output is correct
81 Correct 2841 ms 170444 KB Output is correct
82 Correct 3556 ms 227568 KB Output is correct
83 Correct 2841 ms 170360 KB Output is correct
84 Correct 3273 ms 228756 KB Output is correct
85 Correct 1680 ms 229564 KB Output is correct