답안 #524544

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
524544 2022-02-09T13:11:56 Z fatemetmhr Wild Boar (JOI18_wild_boar) C++17
62 / 100
8095 ms 160888 KB
//  ~Be name khoda~  //

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second
#define cl       clear
#define endll    '\n'

const int maxn  =  1e6   + 10;
const int maxn5 =  2e3   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const ll  inf   =  2e18;


ll h1[maxn5], h2[maxn5];
pair <ll, ll> dis[maxn5][2][maxn5];
int root[maxn5][2][maxn5];
int pa[maxn5];
set <pair<ll, pair<int, int>>> av;
vector <pair<int, int>> adj[maxn5];
int a[maxn5], b[maxn5];
ll c[maxn5];
int x[maxn];
pair <ll, ll> done[maxn];
int mxx[maxn];
int n;

inline void dij(int e, bool ty){
	for(int i = 0; i < n; i++){
		h1[i] = h2[i] = inf;
	}
	memset(pa, -1, sizeof pa);
	
	int uu = a[e], vu = b[e];
	if(ty){
		uu = b[e];
		vu = a[e];
	}
	
	bool ch = false;
		
	h1[vu] = 0;
	pa[vu] = e;

	av.clear();
	
	for(int i = 0; i < n; i++){
		av.insert({h1[i], {i, 0}});
		av.insert({h2[i], {i, 1}});
	}
	
	while(!av.empty()){
		int  v = av.begin() -> se.fi, ty = av.begin() -> se.se;
		av.erase(av.begin());
		if(ch){
			cout << v << ' ' << ty << ' ' << h1[v] << ' ' << h2[v] << ' ' << pa[v] << '\n';
		}
		if(ty == 0){
			if(h1[v] >= inf)
				continue;
			for(auto p : adj[v]) if(p.se != pa[v] && (v != uu || p.fi != vu)){
				int u = p.fi;
				ll val = h1[v] + c[p.se];
				if(val <= h1[u]){
					av.erase({h1[u], {u, 0}});
					av.erase({h2[u], {u, 1}});
					h2[u] = h1[u];
					h1[u] = val;
					pa[u] = p.se;
					av.insert({h1[u], {u, 0}});
					av.insert({h2[u], {u, 1}});
				}
				else if(val < h2[u]){
					av.erase({h2[u], {u, 1}});
					h2[u] = val;
					av.insert({h2[u], {u, 1}});
				}
			}
		}
		else{
			if(h2[v] >= inf)
				continue;
			int i = pa[v];
			int u = a[i] == v ? b[i] : a[i];
			if(v != uu || u != vu){
				ll val = h2[v] + c[i];
				if(val <= h1[u]){
					av.erase({h1[u], {u, 0}});
					av.erase({h2[u], {u, 1}});
					h2[u] = h1[u];
					h1[u] = val;
					pa[u] = i;
					av.insert({h1[u], {u, 0}});
					av.insert({h2[u], {u, 1}});
				}
				else if(val < h2[u]){
					av.erase({h2[u], {u, 1}});
					h2[u] = val;
					av.insert({h2[u], {u, 1}});
				}
			}
		}
	}
	
	for(int i = 0; i < n; i++){
		dis[e][ty][i] = {h1[i], h2[i]};
		root[e][ty][i] = pa[i];
	}
	
}

inline bool get_ty(int e, int u, int v){
	return a[e] == v;
}

int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	
	int m, t, l; cin >> n >> m >> t >> l;
	for(int i = 0; i < m; i++){
		cin >> a[i] >> b[i] >> c[i];
		a[i]--; b[i]--;
		adj[a[i]].pb({b[i], i});
		adj[b[i]].pb({a[i], i});
	}
	
	for(int i = 0; i < m; i++){
		dij(i, 0);
		dij(i, 1);
	}

	for(int i = 0; i < l; i++){
		cin >> x[i];
		x[i]--;
	}
	int a, b; cin >> a >> b;
	a--; b--;
	x[a] = b;
	
	/*
	for(int i = 0; i < m; i++) for(int j = 0; j < n; j++){
		cout << ::a[i] << ' ' << ::b[i] << " * " << j << ' ' << dis[i][0][j].fi << ' ' << dis[i][0][j].se << endl;
		cout << dis[i][1][j].fi << ' ' << dis[i][1][j].se << ' ' << root[i][1][j] << endl;
	}
	//*/
	

	for(int i = 0; i < l; i++){
		if(i == 0){
			done[0] = {0, 0};
			mxx[0] = adj[x[0]][0].se;
		}
		else{
			int u = x[i - 1], v = x[i];
			done[i].fi = inf;
			int opt = mxx[i - 1];
			for(auto p : adj[u]){
				int z = p.fi, id = p.se;
				ll val, val2;
				if(id != mxx[i - 1]){
					val = done[i - 1].fi + dis[id][get_ty(id, u, z)][v].fi + c[id];
					val2 = done[i - 1].fi + dis[id][get_ty(id, u, z)][v].se + c[id];
				}
				else{
					val = done[i - 1].se + dis[id][get_ty(id, u, z)][v].fi + c[id];
					val2 = done[i - 1].se + dis[id][get_ty(id, u, z)][v].se + c[id];
				}
				if(val <= done[i].fi){
					opt = id;
					done[i].fi = val;
					done[i].se = val2;
					mxx[i] = root[id][get_ty(id, u, z)][v];
				}
			
			}
			
			for(auto p : adj[u]){
				int z = p.fi, id = p.se;
				ll val = (root[id][get_ty(id, u, z)][v] == mxx[i] ? dis[id][get_ty(id, u, z)][v].se : dis[id][get_ty(id, u, z)][v].fi) + c[id];
				//cout << "alright " << z << ' ' << id << ' ' << val << ' ' << done[i].se << endl;
				if(id != mxx[i - 1]){
					val += done[i - 1].fi;
				}
				else{
					val += done[i - 1].se;
				}
				if(val <= done[i].se){
					done[i].se = val;
				}
			
			}
		}
		//cout << i << ' ' << x[i] << ' ' << done[i].fi << ' ' << done[i].se << ' ' << mxx[i] << endl;
	}
	
	cout << (done[l - 1].fi == inf ? -1 : done[l - 1].fi) << endl;
	
	
}

/*
4 4 4 3
1 2 1
2 3 1
1 3 1
1 4 1
2
1
2
2 4
*/










Compilation message

wild_boar.cpp: In function 'int main()':
wild_boar.cpp:167:8: warning: variable 'opt' set but not used [-Wunused-but-set-variable]
  167 |    int opt = mxx[i - 1];
      |        ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 500 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 504 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 504 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 500 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 504 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 504 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 3 ms 1100 KB Output is correct
27 Correct 21 ms 5036 KB Output is correct
28 Correct 21 ms 5032 KB Output is correct
29 Correct 96 ms 8980 KB Output is correct
30 Correct 115 ms 8956 KB Output is correct
31 Correct 136 ms 8932 KB Output is correct
32 Correct 132 ms 8868 KB Output is correct
33 Correct 119 ms 9696 KB Output is correct
34 Correct 149 ms 9684 KB Output is correct
35 Correct 158 ms 9480 KB Output is correct
36 Correct 149 ms 9476 KB Output is correct
37 Correct 122 ms 9764 KB Output is correct
38 Correct 157 ms 10268 KB Output is correct
39 Correct 155 ms 10252 KB Output is correct
40 Correct 140 ms 10260 KB Output is correct
41 Correct 141 ms 10308 KB Output is correct
42 Correct 220 ms 10460 KB Output is correct
43 Correct 162 ms 10816 KB Output is correct
44 Correct 172 ms 10872 KB Output is correct
45 Correct 119 ms 11496 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 500 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 504 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 504 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 3 ms 1100 KB Output is correct
27 Correct 21 ms 5036 KB Output is correct
28 Correct 21 ms 5032 KB Output is correct
29 Correct 96 ms 8980 KB Output is correct
30 Correct 115 ms 8956 KB Output is correct
31 Correct 136 ms 8932 KB Output is correct
32 Correct 132 ms 8868 KB Output is correct
33 Correct 119 ms 9696 KB Output is correct
34 Correct 149 ms 9684 KB Output is correct
35 Correct 158 ms 9480 KB Output is correct
36 Correct 149 ms 9476 KB Output is correct
37 Correct 122 ms 9764 KB Output is correct
38 Correct 157 ms 10268 KB Output is correct
39 Correct 155 ms 10252 KB Output is correct
40 Correct 140 ms 10260 KB Output is correct
41 Correct 141 ms 10308 KB Output is correct
42 Correct 220 ms 10460 KB Output is correct
43 Correct 162 ms 10816 KB Output is correct
44 Correct 172 ms 10872 KB Output is correct
45 Correct 119 ms 11496 KB Output is correct
46 Correct 270 ms 12412 KB Output is correct
47 Correct 3664 ms 74344 KB Output is correct
48 Correct 4448 ms 97848 KB Output is correct
49 Correct 5251 ms 113492 KB Output is correct
50 Correct 5180 ms 113228 KB Output is correct
51 Correct 5316 ms 113244 KB Output is correct
52 Correct 5326 ms 113572 KB Output is correct
53 Correct 5373 ms 113360 KB Output is correct
54 Correct 5262 ms 113364 KB Output is correct
55 Correct 5246 ms 113504 KB Output is correct
56 Correct 5662 ms 119804 KB Output is correct
57 Correct 6025 ms 126080 KB Output is correct
58 Correct 6845 ms 132204 KB Output is correct
59 Correct 6757 ms 138668 KB Output is correct
60 Correct 6966 ms 144832 KB Output is correct
61 Correct 7358 ms 151100 KB Output is correct
62 Correct 7697 ms 157340 KB Output is correct
63 Correct 8095 ms 160788 KB Output is correct
64 Correct 5286 ms 160804 KB Output is correct
65 Correct 5445 ms 160888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 460 KB Output is correct
2 Correct 1 ms 460 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 460 KB Output is correct
6 Correct 1 ms 588 KB Output is correct
7 Correct 1 ms 500 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 460 KB Output is correct
12 Correct 1 ms 460 KB Output is correct
13 Correct 1 ms 460 KB Output is correct
14 Correct 1 ms 460 KB Output is correct
15 Correct 1 ms 460 KB Output is correct
16 Correct 1 ms 588 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 504 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 2 ms 504 KB Output is correct
21 Correct 1 ms 460 KB Output is correct
22 Correct 1 ms 504 KB Output is correct
23 Correct 1 ms 460 KB Output is correct
24 Correct 1 ms 588 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 3 ms 1100 KB Output is correct
27 Correct 21 ms 5036 KB Output is correct
28 Correct 21 ms 5032 KB Output is correct
29 Correct 96 ms 8980 KB Output is correct
30 Correct 115 ms 8956 KB Output is correct
31 Correct 136 ms 8932 KB Output is correct
32 Correct 132 ms 8868 KB Output is correct
33 Correct 119 ms 9696 KB Output is correct
34 Correct 149 ms 9684 KB Output is correct
35 Correct 158 ms 9480 KB Output is correct
36 Correct 149 ms 9476 KB Output is correct
37 Correct 122 ms 9764 KB Output is correct
38 Correct 157 ms 10268 KB Output is correct
39 Correct 155 ms 10252 KB Output is correct
40 Correct 140 ms 10260 KB Output is correct
41 Correct 141 ms 10308 KB Output is correct
42 Correct 220 ms 10460 KB Output is correct
43 Correct 162 ms 10816 KB Output is correct
44 Correct 172 ms 10872 KB Output is correct
45 Correct 119 ms 11496 KB Output is correct
46 Correct 270 ms 12412 KB Output is correct
47 Correct 3664 ms 74344 KB Output is correct
48 Correct 4448 ms 97848 KB Output is correct
49 Correct 5251 ms 113492 KB Output is correct
50 Correct 5180 ms 113228 KB Output is correct
51 Correct 5316 ms 113244 KB Output is correct
52 Correct 5326 ms 113572 KB Output is correct
53 Correct 5373 ms 113360 KB Output is correct
54 Correct 5262 ms 113364 KB Output is correct
55 Correct 5246 ms 113504 KB Output is correct
56 Correct 5662 ms 119804 KB Output is correct
57 Correct 6025 ms 126080 KB Output is correct
58 Correct 6845 ms 132204 KB Output is correct
59 Correct 6757 ms 138668 KB Output is correct
60 Correct 6966 ms 144832 KB Output is correct
61 Correct 7358 ms 151100 KB Output is correct
62 Correct 7697 ms 157340 KB Output is correct
63 Correct 8095 ms 160788 KB Output is correct
64 Correct 5286 ms 160804 KB Output is correct
65 Correct 5445 ms 160888 KB Output is correct
66 Incorrect 16 ms 3652 KB Output isn't correct
67 Halted 0 ms 0 KB -