Submission #684956

# Submission time Handle Problem Language Result Execution time Memory
684956 2023-01-22T23:00:09 Z ethening Olympic Bus (JOI20_ho_t4) C++17
100 / 100
776 ms 3456 KB
#include "bits/stdc++.h"
#include <functional>
#include <queue>
#include <vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int MAXN = 200;
const int MAXM = 50000;

const ll INF = 1000'000'000'000'000ll;

int n, m;

struct edge {
	int fr, to;
	ll wcost;
	ll scost;
	bool crit;
};

edge E[MAXM + 5];
// vector<int> v[MAXN + 5], v2[MAXN + 5];

vector<vector<int>> adj;
vector<vector<int>> adj2;

int get_edge(int a, int b, int swapEg) {
	int ret = -1;
	if (swapEg != -1 && E[swapEg].fr == b && E[swapEg].to == a) {
		ret = swapEg;
	}
	if (adj[a][b] != -1 && adj[a][b] != swapEg) {
		if (ret == -1 || E[adj[a][b]].wcost < E[ret].wcost) {
			ret = adj[a][b];
		}
	}
	if (adj2[a][b] != -1 && adj2[a][b] != swapEg) {
		if (ret == -1 || E[adj2[a][b]].wcost < E[ret].wcost) {
			ret = adj2[a][b];
		}
	}
	return ret;
}

void dijk(int source, vector<ll>& dist) {
	dist.assign(n + 5, INF);
	vector<bool> vis(n + 5, false);
	typedef pair<ll, int> pli;
	priority_queue<pli, vector<pli>, greater<pli>> Q;
	dist[source] = 0;
	vector<int> arrEdge(n + 5, 0);
	for (int k = 1; k <= n; k++) {
		int a = -1;
		ll min = INF;
		for (int i = 1; i <= n; i++) {
			if (!vis[i] && dist[i] < min) {
				a = i;
				min = dist[i];
			}
		}
		if (a == -1) break;
		vis[a] = true;
		for (int b = 1; b <= n; b++) {
			int eg = get_edge(a, b, -1);
			if (eg == -1) continue;
			if (!vis[b] && dist[a] + E[eg].wcost < dist[b]) {
				dist[b] = dist[a] + E[eg].wcost;
				arrEdge[b] = eg;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		if (arrEdge[i] != 0) {
			E[arrEdge[i]].crit = true;
		}
	}
}

void dijk2(int dest, vector<ll>& dist) {
	dist.assign(n + 5, INF);
	vector<bool> vis(n + 5, false);
	typedef pair<ll, int> pli;
	priority_queue<pli, vector<pli>, greater<pli>> Q;
	dist[dest] = 0;
	vector<int> arrEdge(n + 5, 0);
	for (int k = 1; k <= n; k++) {
		int a = -1;
		ll min = INF;
		for (int i = 1; i <= n; i++) {
			if (!vis[i] && dist[i] < min) {
				a = i;
				min = dist[i];
			}
		}
		if (a == -1) break;
		vis[a] = true;
		for (int b = 1; b <= n; b++) {
			int eg = get_edge(b, a, -1);
			if (eg == -1) continue;
			if (!vis[b] && dist[a] + E[eg].wcost < dist[b]) {
				dist[b] = dist[a] + E[eg].wcost;
				arrEdge[b] = eg;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		if (arrEdge[i] != 0) {
			E[arrEdge[i]].crit = true;
		}
	}
}

void dijk3(int source, vector<ll>& dist, int swapEg) {
	dist.assign(n + 5, INF);
	vector<bool> vis(n + 5, false);
	typedef pair<ll, int> pli;
	priority_queue<pli, vector<pli>, greater<pli>> Q;
	dist[source] = 0;
	for (int k = 1; k <= n; k++) {
		int a = -1;
		ll min = INF;
		for (int i = 1; i <= n; i++) {
			if (!vis[i] && dist[i] < min) {
				a = i;
				min = dist[i];
			}
		}
		if (a == -1) break;
		vis[a] = true;
		for (int b = 1; b <= n; b++) {
			int eg = get_edge(a, b, swapEg);
			if (eg == -1) continue;
			if (!vis[b] && dist[a] + E[eg].wcost < dist[b]) {
				dist[b] = dist[a] + E[eg].wcost;
			}
		}
	}
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		cin >> E[i].fr >> E[i].to >> E[i].wcost >> E[i].scost;
		E[i].crit = false;
		// v[E[i].fr].push_back(i);
		// v2[E[i].to].push_back(i);
	}

	adj.assign(n + 5, vector<int>(n + 5, -1));
	adj2.assign(n + 5, vector<int>(n + 5, -1));

	for (int i = 1; i <= m; i++) {
		if (adj[E[i].fr][E[i].to] == -1) {
			adj[E[i].fr][E[i].to] = i;
			continue;
		}
		if (adj2[E[i].fr][E[i].to] == -1) {
			adj2[E[i].fr][E[i].to] = i;
			if (E[adj2[E[i].fr][E[i].to]].wcost < E[adj[E[i].fr][E[i].to]].wcost) {
				swap(adj[E[i].fr][E[i].to], adj2[E[i].fr][E[i].to]);
			}
			continue;
		}
		if (E[i].wcost < E[adj2[E[i].fr][E[i].to]].wcost) {
			adj2[E[i].fr][E[i].to] = i;
			if (E[adj2[E[i].fr][E[i].to]].wcost < E[adj[E[i].fr][E[i].to]].wcost) {
				swap(adj[E[i].fr][E[i].to], adj2[E[i].fr][E[i].to]);
			}
		}
	}

	vector<ll> distfr1, distton, distfrn, distto1;
	dijk(1, distfr1);
	dijk(n, distfrn);
	dijk2(1, distto1);
	dijk2(n, distton);

	// cout << "!!!" << endl;
	// for (int i = 1; i <= n; i++) {
	// 	cout << i << " " << distfr1[i] << endl;
	// }
	// cout << "!!!" << endl;
	// for (int i = 1; i <= n; i++) {
	// 	cout << i << " " << distfrn[i] << endl;
	// }
	// cout << "!!!" << endl;
	// for (int i = 1; i <= n; i++) {
	// 	cout << i << " " << distto1[i] << endl;
	// }
	// cout << "!!!" << endl;
	// for (int i = 1; i <= n; i++) {
	// 	cout << i << " " << distton[i] << endl;
	// }
	// cout << "!!!" << endl;

	ll ans = INF;
	ans = min(ans, distfr1[n] + distfrn[1]);

	for (int i = 1; i <= m; i++) {
		if (E[i].crit) continue;
		ll tmp = min(distfr1[n], distfr1[E[i].to] + E[i].wcost + distton[E[i].fr]);
		ll tmp2 = min(distfrn[1], distfrn[E[i].to] + E[i].wcost + distto1[E[i].fr]);
		// cout << i << " " <<  tmp + tmp2 << "\n";
		ans = min(ans, tmp + tmp2 + E[i].scost);
	}
	// cout << ans << endl;

	for (int i = 1; i <= m; i++) {
		if (!E[i].crit) continue;
		vector<ll> dist2fr1(n + 5, INF), dist2frn(n + 5, INF);
		dijk3(1, dist2fr1, i);
		dijk3(n, dist2frn, i);
		ans = min(ans, dist2fr1[n] + dist2frn[1] + E[i].scost);
	}
	if (ans == INF) ans = -1;
	cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 171 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 231 ms 696 KB Output is correct
4 Correct 241 ms 692 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 132 ms 692 KB Output is correct
11 Correct 157 ms 696 KB Output is correct
12 Correct 141 ms 688 KB Output is correct
13 Correct 82 ms 692 KB Output is correct
14 Correct 152 ms 696 KB Output is correct
15 Correct 162 ms 688 KB Output is correct
16 Correct 145 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 548 ms 2224 KB Output is correct
2 Correct 582 ms 2220 KB Output is correct
3 Correct 553 ms 2216 KB Output is correct
4 Correct 224 ms 732 KB Output is correct
5 Correct 157 ms 696 KB Output is correct
6 Correct 3 ms 592 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 162 ms 2224 KB Output is correct
10 Correct 144 ms 3284 KB Output is correct
11 Correct 326 ms 3284 KB Output is correct
12 Correct 317 ms 3388 KB Output is correct
13 Correct 294 ms 3376 KB Output is correct
14 Correct 284 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 171 ms 596 KB Output is correct
2 Correct 18 ms 596 KB Output is correct
3 Correct 776 ms 1884 KB Output is correct
4 Correct 8 ms 596 KB Output is correct
5 Correct 756 ms 2252 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 248 ms 2132 KB Output is correct
9 Correct 271 ms 3244 KB Output is correct
10 Correct 415 ms 3160 KB Output is correct
11 Correct 421 ms 3172 KB Output is correct
12 Correct 431 ms 3020 KB Output is correct
13 Correct 0 ms 324 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 224 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 425 ms 3156 KB Output is correct
20 Correct 419 ms 3260 KB Output is correct
21 Correct 426 ms 3156 KB Output is correct
22 Correct 426 ms 3148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 171 ms 596 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 231 ms 696 KB Output is correct
4 Correct 241 ms 692 KB Output is correct
5 Correct 3 ms 340 KB Output is correct
6 Correct 3 ms 596 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 132 ms 692 KB Output is correct
11 Correct 157 ms 696 KB Output is correct
12 Correct 141 ms 688 KB Output is correct
13 Correct 82 ms 692 KB Output is correct
14 Correct 152 ms 696 KB Output is correct
15 Correct 162 ms 688 KB Output is correct
16 Correct 145 ms 596 KB Output is correct
17 Correct 548 ms 2224 KB Output is correct
18 Correct 582 ms 2220 KB Output is correct
19 Correct 553 ms 2216 KB Output is correct
20 Correct 224 ms 732 KB Output is correct
21 Correct 157 ms 696 KB Output is correct
22 Correct 3 ms 592 KB Output is correct
23 Correct 2 ms 596 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 162 ms 2224 KB Output is correct
26 Correct 144 ms 3284 KB Output is correct
27 Correct 326 ms 3284 KB Output is correct
28 Correct 317 ms 3388 KB Output is correct
29 Correct 294 ms 3376 KB Output is correct
30 Correct 284 ms 3456 KB Output is correct
31 Correct 171 ms 596 KB Output is correct
32 Correct 18 ms 596 KB Output is correct
33 Correct 776 ms 1884 KB Output is correct
34 Correct 8 ms 596 KB Output is correct
35 Correct 756 ms 2252 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 248 ms 2132 KB Output is correct
39 Correct 271 ms 3244 KB Output is correct
40 Correct 415 ms 3160 KB Output is correct
41 Correct 421 ms 3172 KB Output is correct
42 Correct 431 ms 3020 KB Output is correct
43 Correct 0 ms 324 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct
46 Correct 1 ms 212 KB Output is correct
47 Correct 0 ms 224 KB Output is correct
48 Correct 1 ms 212 KB Output is correct
49 Correct 425 ms 3156 KB Output is correct
50 Correct 419 ms 3260 KB Output is correct
51 Correct 426 ms 3156 KB Output is correct
52 Correct 426 ms 3148 KB Output is correct
53 Correct 636 ms 3240 KB Output is correct
54 Correct 668 ms 3196 KB Output is correct
55 Correct 608 ms 3208 KB Output is correct
56 Correct 244 ms 844 KB Output is correct
57 Correct 215 ms 724 KB Output is correct
58 Correct 288 ms 2728 KB Output is correct
59 Correct 332 ms 2840 KB Output is correct
60 Correct 469 ms 2772 KB Output is correct
61 Correct 291 ms 2748 KB Output is correct
62 Correct 326 ms 2732 KB Output is correct
63 Correct 454 ms 2644 KB Output is correct
64 Correct 215 ms 2720 KB Output is correct
65 Correct 234 ms 2724 KB Output is correct
66 Correct 288 ms 2644 KB Output is correct
67 Correct 12 ms 2272 KB Output is correct
68 Correct 190 ms 3388 KB Output is correct
69 Correct 187 ms 3292 KB Output is correct
70 Correct 364 ms 3384 KB Output is correct
71 Correct 361 ms 3404 KB Output is correct
72 Correct 356 ms 3380 KB Output is correct
73 Correct 380 ms 3388 KB Output is correct
74 Correct 361 ms 3276 KB Output is correct