답안 #442785

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
442785 2021-07-09T04:46:54 Z wind_reaper Olympic Bus (JOI20_ho_t4) C++17
100 / 100
127 ms 3688 KB
#include <bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
*/

using namespace std;
// using namespace __gnu_pbds;
using namespace chrono;

// mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
/*
template <class T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
*/

//***************** CONSTANTS *****************

const int MXN = 200;
const int MXM = 50000;
const int64_t INF = 1'500'000'000'000'000;

//***************** GLOBAL VARIABLES *****************

vector<array<int, 2>> g[2][MXN];
int U[MXM], V[MXM], w[MXM], rev[MXM];
int N, M;

//***************** AUXILIARY STRUCTS *****************

void dijkstra(int S, int T, int idx, int64_t dist[], vector<bool>& used){
	int prev[N]; memset(prev, -1, sizeof prev);
	
	if(!idx)
		used.resize(M, false);
	
	vector<bool> vis(N, false);

	for(int i = 0; i <= N; i++) dist[i] = INF;
	dist[S] = 0;

	while(true){
		int u = N;
		for(int i = 0; i < N; i++)
			if(!vis[i] && dist[i] < dist[u])
				u = i;

		if(u == N)
			break;

		for(auto& [v, i] : g[idx][u])
			if(dist[v] > dist[u] + w[i])
				dist[v] = dist[u] + w[i], prev[v] = i;

		vis[u] = 1;
	}
	
	if(idx) return;

	for(int cur = T; prev[cur] != -1; cur = U[prev[cur]])
		used[prev[cur]] = 1;
}

int64_t dijkstra(int S, int T, int idx){
	vector<bool> vis(N, 0);
	int64_t dist[N + 1];
	for(int i = 0; i <= N; i++)
		dist[i] = INF;

	dist[S] = 0;

	while(true){
		int u = N;
		for(int i = 0; i < N; i++)
			if(!vis[i] && dist[i] < dist[u])
				u = i;

		if(u == N)
			break;

		for(auto& [v, i] : g[0][u])
			if(i != idx){
				dist[v] = min(dist[v], dist[u] + w[i]);
			}

		if(V[idx] == u)
			dist[U[idx]] = min(dist[U[idx]], dist[u] + w[idx]);

		vis[u] = 1;
	}

	return dist[T];
}

//***************** MAIN BODY *****************

void solve(){
	cin >> N >> M;

	for(int i = 0; i < M; i++){
		cin >> U[i] >> V[i] >> w[i] >> rev[i];
		--U[i], --V[i];
		g[0][U[i]].push_back({V[i], i});
		g[1][V[i]].push_back({U[i], i});
	}

	vector<bool> used[2], tmp;
	int64_t from[2][N + 1], to[2][N + 1];

	dijkstra(0, N - 1, 0, from[0], used[0]);
	dijkstra(N - 1, 0, 0, from[1], used[1]);
	dijkstra(0, N - 1, 1, to[0], tmp);
	dijkstra(N - 1, 0, 1, to[1], tmp);

	int64_t ans = from[0][N - 1] + from[1][0];

	for(int i = 0; i < M; i++){
		int64_t temp = rev[i];

		if(used[0][i]) 
			temp += dijkstra(0, N - 1, i);
		else temp += min(from[0][N - 1], from[0][V[i]] + to[1][U[i]] + w[i]);

		if(used[1][i])
			temp += dijkstra(N - 1, 0, i);
		else temp += min(from[1][0], from[1][V[i]] + to[0][U[i]] + w[i]);

		ans = min(ans, temp);
	}

	cout << (ans >= INF ? -1 : ans) << '\n';
}

//***************** *****************

int32_t main(){
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);

	#ifdef LOCAL
		auto begin = high_resolution_clock::now();
	#endif

	int tc = 1;
	// cin >> tc; 
	for (int t = 0; t < tc; t++)
		solve();

	#ifdef LOCAL 
		auto end = high_resolution_clock::now();
		cout << fixed << setprecision(4);
		cout << "Execution Time: " << duration_cast<duration<double>>(end - begin).count() << "seconds" << endl;
	#endif

	return 0;
}

/*
If code gives a WA, check for the following : 
1. I/O format

2. Are you clearing all global variables in between tests if multitests are a thing

3. Can you definitively prove the logic
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 3 ms 332 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 46 ms 372 KB Output is correct
11 Correct 45 ms 372 KB Output is correct
12 Correct 46 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 3 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 3 ms 392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 2380 KB Output is correct
2 Correct 27 ms 3396 KB Output is correct
3 Correct 28 ms 3500 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 3 ms 344 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 26 ms 3468 KB Output is correct
10 Correct 25 ms 3524 KB Output is correct
11 Correct 29 ms 3620 KB Output is correct
12 Correct 28 ms 3524 KB Output is correct
13 Correct 28 ms 3532 KB Output is correct
14 Correct 28 ms 3688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 20 ms 1868 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 25 ms 3324 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 26 ms 3316 KB Output is correct
9 Correct 23 ms 3228 KB Output is correct
10 Correct 29 ms 3304 KB Output is correct
11 Correct 25 ms 3276 KB Output is correct
12 Correct 28 ms 3376 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 25 ms 3372 KB Output is correct
20 Correct 25 ms 3276 KB Output is correct
21 Correct 27 ms 3280 KB Output is correct
22 Correct 25 ms 3320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 3 ms 332 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 46 ms 372 KB Output is correct
11 Correct 45 ms 372 KB Output is correct
12 Correct 46 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 3 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 3 ms 392 KB Output is correct
17 Correct 26 ms 2380 KB Output is correct
18 Correct 27 ms 3396 KB Output is correct
19 Correct 28 ms 3500 KB Output is correct
20 Correct 3 ms 468 KB Output is correct
21 Correct 3 ms 344 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 26 ms 3468 KB Output is correct
26 Correct 25 ms 3524 KB Output is correct
27 Correct 29 ms 3620 KB Output is correct
28 Correct 28 ms 3524 KB Output is correct
29 Correct 28 ms 3532 KB Output is correct
30 Correct 28 ms 3688 KB Output is correct
31 Correct 3 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 20 ms 1868 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 25 ms 3324 KB Output is correct
36 Correct 1 ms 332 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Correct 26 ms 3316 KB Output is correct
39 Correct 23 ms 3228 KB Output is correct
40 Correct 29 ms 3304 KB Output is correct
41 Correct 25 ms 3276 KB Output is correct
42 Correct 28 ms 3376 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 1 ms 332 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 1 ms 332 KB Output is correct
47 Correct 1 ms 332 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 25 ms 3372 KB Output is correct
50 Correct 25 ms 3276 KB Output is correct
51 Correct 27 ms 3280 KB Output is correct
52 Correct 25 ms 3320 KB Output is correct
53 Correct 27 ms 3368 KB Output is correct
54 Correct 30 ms 3348 KB Output is correct
55 Correct 29 ms 3280 KB Output is correct
56 Correct 3 ms 332 KB Output is correct
57 Correct 2 ms 332 KB Output is correct
58 Correct 124 ms 2720 KB Output is correct
59 Correct 123 ms 2696 KB Output is correct
60 Correct 127 ms 2624 KB Output is correct
61 Correct 107 ms 2708 KB Output is correct
62 Correct 124 ms 2636 KB Output is correct
63 Correct 124 ms 2712 KB Output is correct
64 Correct 125 ms 2848 KB Output is correct
65 Correct 115 ms 3000 KB Output is correct
66 Correct 103 ms 2892 KB Output is correct
67 Correct 18 ms 2624 KB Output is correct
68 Correct 25 ms 3532 KB Output is correct
69 Correct 25 ms 3492 KB Output is correct
70 Correct 29 ms 3524 KB Output is correct
71 Correct 33 ms 3640 KB Output is correct
72 Correct 27 ms 3492 KB Output is correct
73 Correct 28 ms 3556 KB Output is correct
74 Correct 28 ms 3540 KB Output is correct