답안 #209307

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
209307 2020-03-13T16:19:19 Z songc Wild Boar (JOI18_wild_boar) C++14
100 / 100
12214 ms 411728 KB
#include <bits/stdc++.h>
#define pb push_back
#define all(v) v.begin(),v.end()
#define lb lower_bound
#define fi first
#define se second
#define INF (1ll<<60)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;

int N, M, Q, L;
int P[101010];
int num[2020][2020], nm;
LL D[4040][4040];
bool chk[4040];
vector<pii> adj[2020];

struct Path{
	int u, v;
	LL w;
	bool operator<(const Path &r)const{
		return w < r.w;
	}
};
priority_queue<Path> PQ;

struct Node{
	Path p[4];
} C[2020][2020], T[404040];

void dijk(int a, int b){
	PQ.push((Path){a, b, 0});
	D[num[a][b]][num[a][b]]=0;
	while (!PQ.empty()){
		Path T = PQ.top();
		PQ.pop();
		if (chk[num[T.u][T.v]]) continue;
		chk[num[T.u][T.v]] = true;
		for (pii x : adj[T.u]) if (x.fi != T.v && D[num[a][b]][num[x.fi][T.u]] > -T.w+x.se){
			D[num[a][b]][num[x.fi][T.u]] = -T.w+x.se;
			PQ.push((Path){x.fi, T.u, T.w-x.se});
		}
	}
}

Path add(Path a, Path b){
	return (Path){a.u, b.v, (a.v==b.u)?INF:min(INF,a.w+b.w)};
}

Node add(Node a, Node b){
	Node ret;
	for (int i=0; i<4; i++) ret.p[i].w=INF;
	vector<Path> v;
	for (int i=0; i<4; i++) for (int j=0; j<4; j++) v.pb(add(a.p[i], b.p[j]));
	for (Path x : v) ret.p[0] = min(ret.p[0], x);
	for (Path x : v) if (ret.p[0].u != x.u && ret.p[0].v != x.v) ret.p[1] = min(ret.p[1], x);
	for (Path x : v) if (ret.p[1].u != x.u && ret.p[0].v != x.v) ret.p[2] = min(ret.p[2], x);
	for (Path x : v) if (ret.p[0].u != x.u && ret.p[1].v != x.v) ret.p[3] = min(ret.p[3], x);
	return ret;
}

void init(int id, int s, int e){
	if (s+1 == e){
		T[id] = C[P[s]][P[e]];
		return;
	}
	int mid=(s+e)/2;
	init(id*2, s, mid), init(id*2+1, mid, e);
	T[id] = add(T[id*2], T[id*2+1]);
}

void upd(int id, int s, int e, int t){
	if (e < t || t < s) return;
	if (s+1 == e){
		T[id] = C[P[s]][P[e]];
		return;
	}
	int mid=(s+e)/2;
	upd(id*2, s, mid, t), upd(id*2+1, mid, e, t);
	T[id] = add(T[id*2], T[id*2+1]);
}

int main(){
	scanf("%d %d %d %d", &N, &M, &Q, &L);
	for (int i=1; i<=M; i++){
		int u, v, w;
		scanf("%d %d %d", &u, &v, &w);
		adj[u].pb(pii(v, w)), adj[v].pb(pii(u, w));
	}
	for (int u=1; u<=N; u++) for (pii v : adj[u]) num[u][v.fi]=++nm;
	for (int i=1; i<=nm; i++) for (int j=1; j<=nm; j++) D[i][j] = INF;
	for (int u=1; u<=N; u++) for (pii v : adj[u]){
		memset(chk, false, sizeof chk);
		dijk(u, v.fi);
	}
	for (int i=1; i<=N; i++) for (int j=1; j<=N; j++){
		if (i == j) continue;
		for (int k=0; k<4; k++) C[i][j].p[k].w = INF;
		vector<Path> V;
		for (pii u : adj[i]) for (pii v : adj[j]) V.pb((Path){u.fi, v.fi, D[num[u.fi][i]][num[j][v.fi]]+u.se});
		for (Path x : V) C[i][j].p[0] = min(C[i][j].p[0], x);
		for (Path x : V) if (C[i][j].p[0].u != x.u && C[i][j].p[0].v != x.v) C[i][j].p[1] = min(C[i][j].p[1], x);
		for (Path x : V) if (C[i][j].p[1].u != x.u && C[i][j].p[0].v != x.v) C[i][j].p[2] = min(C[i][j].p[2], x);
		for (Path x : V) if (C[i][j].p[0].u != x.u && C[i][j].p[1].v != x.v) C[i][j].p[3] = min(C[i][j].p[3], x);
	}
	for (int i=1; i<=L; i++) scanf("%d", &P[i]);
	init(1, 1, L);
	while(Q--){
		int a, b;
		scanf("%d %d", &a, &b);
		P[a]=b;
		upd(1, 1, L, a);
		if (T[1].p[0].w >= INF) puts("-1");
		else printf("%lld\n", T[1].p[0].w);
	}
	return 0;
}

Compilation message

wild_boar.cpp: In function 'int main()':
wild_boar.cpp:85:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d %d", &N, &M, &Q, &L);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:88:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d", &u, &v, &w);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:107:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i=1; i<=L; i++) scanf("%d", &P[i]);
                           ~~~~~^~~~~~~~~~~~~
wild_boar.cpp:111:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 632 KB Output is correct
2 Correct 5 ms 504 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 504 KB Output is correct
5 Correct 5 ms 504 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 5 ms 504 KB Output is correct
11 Correct 5 ms 504 KB Output is correct
12 Correct 5 ms 508 KB Output is correct
13 Correct 5 ms 508 KB Output is correct
14 Correct 5 ms 504 KB Output is correct
15 Correct 5 ms 504 KB Output is correct
16 Correct 5 ms 504 KB Output is correct
17 Correct 5 ms 504 KB Output is correct
18 Correct 5 ms 504 KB Output is correct
19 Correct 5 ms 504 KB Output is correct
20 Correct 5 ms 504 KB Output is correct
21 Correct 5 ms 504 KB Output is correct
22 Correct 5 ms 504 KB Output is correct
23 Correct 5 ms 504 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 5 ms 504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 632 KB Output is correct
2 Correct 5 ms 504 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 504 KB Output is correct
5 Correct 5 ms 504 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 5 ms 504 KB Output is correct
11 Correct 5 ms 504 KB Output is correct
12 Correct 5 ms 508 KB Output is correct
13 Correct 5 ms 508 KB Output is correct
14 Correct 5 ms 504 KB Output is correct
15 Correct 5 ms 504 KB Output is correct
16 Correct 5 ms 504 KB Output is correct
17 Correct 5 ms 504 KB Output is correct
18 Correct 5 ms 504 KB Output is correct
19 Correct 5 ms 504 KB Output is correct
20 Correct 5 ms 504 KB Output is correct
21 Correct 5 ms 504 KB Output is correct
22 Correct 5 ms 504 KB Output is correct
23 Correct 5 ms 504 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 5 ms 504 KB Output is correct
26 Correct 6 ms 888 KB Output is correct
27 Correct 88 ms 20088 KB Output is correct
28 Correct 86 ms 20088 KB Output is correct
29 Correct 156 ms 24256 KB Output is correct
30 Correct 153 ms 24184 KB Output is correct
31 Correct 158 ms 24260 KB Output is correct
32 Correct 166 ms 24504 KB Output is correct
33 Correct 155 ms 25464 KB Output is correct
34 Correct 158 ms 25464 KB Output is correct
35 Correct 153 ms 25392 KB Output is correct
36 Correct 154 ms 25512 KB Output is correct
37 Correct 160 ms 25464 KB Output is correct
38 Correct 156 ms 27064 KB Output is correct
39 Correct 168 ms 26872 KB Output is correct
40 Correct 157 ms 27000 KB Output is correct
41 Correct 155 ms 27004 KB Output is correct
42 Correct 146 ms 28024 KB Output is correct
43 Correct 151 ms 28816 KB Output is correct
44 Correct 153 ms 28792 KB Output is correct
45 Correct 116 ms 31096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 632 KB Output is correct
2 Correct 5 ms 504 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 504 KB Output is correct
5 Correct 5 ms 504 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 5 ms 504 KB Output is correct
11 Correct 5 ms 504 KB Output is correct
12 Correct 5 ms 508 KB Output is correct
13 Correct 5 ms 508 KB Output is correct
14 Correct 5 ms 504 KB Output is correct
15 Correct 5 ms 504 KB Output is correct
16 Correct 5 ms 504 KB Output is correct
17 Correct 5 ms 504 KB Output is correct
18 Correct 5 ms 504 KB Output is correct
19 Correct 5 ms 504 KB Output is correct
20 Correct 5 ms 504 KB Output is correct
21 Correct 5 ms 504 KB Output is correct
22 Correct 5 ms 504 KB Output is correct
23 Correct 5 ms 504 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 5 ms 504 KB Output is correct
26 Correct 6 ms 888 KB Output is correct
27 Correct 88 ms 20088 KB Output is correct
28 Correct 86 ms 20088 KB Output is correct
29 Correct 156 ms 24256 KB Output is correct
30 Correct 153 ms 24184 KB Output is correct
31 Correct 158 ms 24260 KB Output is correct
32 Correct 166 ms 24504 KB Output is correct
33 Correct 155 ms 25464 KB Output is correct
34 Correct 158 ms 25464 KB Output is correct
35 Correct 153 ms 25392 KB Output is correct
36 Correct 154 ms 25512 KB Output is correct
37 Correct 160 ms 25464 KB Output is correct
38 Correct 156 ms 27064 KB Output is correct
39 Correct 168 ms 26872 KB Output is correct
40 Correct 157 ms 27000 KB Output is correct
41 Correct 155 ms 27004 KB Output is correct
42 Correct 146 ms 28024 KB Output is correct
43 Correct 151 ms 28816 KB Output is correct
44 Correct 153 ms 28792 KB Output is correct
45 Correct 116 ms 31096 KB Output is correct
46 Correct 209 ms 16632 KB Output is correct
47 Correct 8566 ms 164712 KB Output is correct
48 Correct 8353 ms 192324 KB Output is correct
49 Correct 6696 ms 217208 KB Output is correct
50 Correct 7154 ms 217180 KB Output is correct
51 Correct 8322 ms 217236 KB Output is correct
52 Correct 4266 ms 217208 KB Output is correct
53 Correct 4269 ms 217440 KB Output is correct
54 Correct 4346 ms 217336 KB Output is correct
55 Correct 4322 ms 217200 KB Output is correct
56 Correct 4416 ms 231352 KB Output is correct
57 Correct 4549 ms 247040 KB Output is correct
58 Correct 4434 ms 263800 KB Output is correct
59 Correct 4407 ms 281736 KB Output is correct
60 Correct 4373 ms 300812 KB Output is correct
61 Correct 4328 ms 321072 KB Output is correct
62 Correct 4197 ms 342592 KB Output is correct
63 Correct 3992 ms 365352 KB Output is correct
64 Correct 2049 ms 409164 KB Output is correct
65 Correct 2017 ms 409080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 632 KB Output is correct
2 Correct 5 ms 504 KB Output is correct
3 Correct 5 ms 504 KB Output is correct
4 Correct 5 ms 504 KB Output is correct
5 Correct 5 ms 504 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 5 ms 504 KB Output is correct
10 Correct 5 ms 504 KB Output is correct
11 Correct 5 ms 504 KB Output is correct
12 Correct 5 ms 508 KB Output is correct
13 Correct 5 ms 508 KB Output is correct
14 Correct 5 ms 504 KB Output is correct
15 Correct 5 ms 504 KB Output is correct
16 Correct 5 ms 504 KB Output is correct
17 Correct 5 ms 504 KB Output is correct
18 Correct 5 ms 504 KB Output is correct
19 Correct 5 ms 504 KB Output is correct
20 Correct 5 ms 504 KB Output is correct
21 Correct 5 ms 504 KB Output is correct
22 Correct 5 ms 504 KB Output is correct
23 Correct 5 ms 504 KB Output is correct
24 Correct 5 ms 504 KB Output is correct
25 Correct 5 ms 504 KB Output is correct
26 Correct 6 ms 888 KB Output is correct
27 Correct 88 ms 20088 KB Output is correct
28 Correct 86 ms 20088 KB Output is correct
29 Correct 156 ms 24256 KB Output is correct
30 Correct 153 ms 24184 KB Output is correct
31 Correct 158 ms 24260 KB Output is correct
32 Correct 166 ms 24504 KB Output is correct
33 Correct 155 ms 25464 KB Output is correct
34 Correct 158 ms 25464 KB Output is correct
35 Correct 153 ms 25392 KB Output is correct
36 Correct 154 ms 25512 KB Output is correct
37 Correct 160 ms 25464 KB Output is correct
38 Correct 156 ms 27064 KB Output is correct
39 Correct 168 ms 26872 KB Output is correct
40 Correct 157 ms 27000 KB Output is correct
41 Correct 155 ms 27004 KB Output is correct
42 Correct 146 ms 28024 KB Output is correct
43 Correct 151 ms 28816 KB Output is correct
44 Correct 153 ms 28792 KB Output is correct
45 Correct 116 ms 31096 KB Output is correct
46 Correct 209 ms 16632 KB Output is correct
47 Correct 8566 ms 164712 KB Output is correct
48 Correct 8353 ms 192324 KB Output is correct
49 Correct 6696 ms 217208 KB Output is correct
50 Correct 7154 ms 217180 KB Output is correct
51 Correct 8322 ms 217236 KB Output is correct
52 Correct 4266 ms 217208 KB Output is correct
53 Correct 4269 ms 217440 KB Output is correct
54 Correct 4346 ms 217336 KB Output is correct
55 Correct 4322 ms 217200 KB Output is correct
56 Correct 4416 ms 231352 KB Output is correct
57 Correct 4549 ms 247040 KB Output is correct
58 Correct 4434 ms 263800 KB Output is correct
59 Correct 4407 ms 281736 KB Output is correct
60 Correct 4373 ms 300812 KB Output is correct
61 Correct 4328 ms 321072 KB Output is correct
62 Correct 4197 ms 342592 KB Output is correct
63 Correct 3992 ms 365352 KB Output is correct
64 Correct 2049 ms 409164 KB Output is correct
65 Correct 2017 ms 409080 KB Output is correct
66 Correct 89 ms 18040 KB Output is correct
67 Correct 913 ms 112504 KB Output is correct
68 Correct 1764 ms 390904 KB Output is correct
69 Correct 2297 ms 391944 KB Output is correct
70 Correct 2978 ms 409592 KB Output is correct
71 Correct 9698 ms 167072 KB Output is correct
72 Correct 9769 ms 194596 KB Output is correct
73 Correct 5455 ms 219988 KB Output is correct
74 Correct 5440 ms 220024 KB Output is correct
75 Correct 5524 ms 219896 KB Output is correct
76 Correct 7727 ms 219484 KB Output is correct
77 Correct 9693 ms 219704 KB Output is correct
78 Correct 12214 ms 219500 KB Output is correct
79 Correct 5569 ms 249884 KB Output is correct
80 Correct 5614 ms 266364 KB Output is correct
81 Correct 6842 ms 284072 KB Output is correct
82 Correct 5532 ms 303608 KB Output is correct
83 Correct 5786 ms 323060 KB Output is correct
84 Correct 5048 ms 368324 KB Output is correct
85 Correct 3246 ms 411728 KB Output is correct