Submission #652598

# Submission time Handle Problem Language Result Execution time Memory
652598 2022-10-23T13:14:48 Z ymm Olympic Bus (JOI20_ho_t4) C++17
100 / 100
922 ms 3664 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int N = 256;
const int M = 50010;
vector<pii> A[N], T[N];
const int inf = 1.5e9+1000;
int w[M], p[M];
int dis0[N], disn[N], dis0T[N], disnT[N], disdard[N];
pii par[N];
pii edge[M];
bool is_path[M];
int n, m;

void dij(int s, int dis[], vector<pii> A[])
{
	fill(dis, dis+n, inf);
	fill(par, par+n, pii{-1, -1});
	dis[s] = 0;
	set<pii> st = {{0, s}};
	while (st.size()) {
		auto [d, v] = *st.begin();
		st.erase(st.begin());
		for (auto [u, e] : A[v]) {
			int w = ::w[e];
			if (d + w < dis[u]) {
				st.erase({dis[u], u});
				dis[u] = d+w;
				par[u] = {v, e};
				st.insert({dis[u], u});
			}
		}
	}
}

#define int ll

void input()
{
	cin.tie(0) -> sync_with_stdio(false);
	cin >> n >> m;
	Loop (e,0,m) {
		int v, u;
		cin >> v >> u >> w[e] >> p[e];
		--v; --u;
		A[v].push_back({u,e});
		T[u].push_back({v,e});
		edge[e] = {v, u};
	}
	Loop (i,0,n) {
		sort(A[i].begin(), A[i].end(), [](pii x, pii y){
			return w[x.second] < w[y.second];
		});
	}
}

signed main()
{
	input();
	int ans = 0;
	dij(0, dis0, A);
	dij(0, dis0T, T);
	ans += dis0[n-1];
	int v = n-1;
	for (;;) {
		auto [u, e] = par[v];
		if (u == -1)
			break;
		is_path[e] = 1;
		v = u;
	}
	dij(n-1, disn, A);
	dij(n-1, disnT, T);
	ans += disn[0];
	v = 0;
	for (;;) {
		auto [u, e] = par[v];
		if (u == -1)
			break;
		is_path[e] = 1;
		v = u;
	}
	Loop (e,0,m) {
		auto [v, u] = edge[e];
		if (is_path[e]) {
			signed tmp = inf;
			swap(tmp, w[e]);
			w[m] = tmp;
			A[u].push_back({v,m});
			int x = p[e];
			dij(0, disdard, A);
			x += disdard[n-1];
			dij(n-1, disdard, A);
			x += disdard[0];
			ans = min(ans, x);
			A[u].pop_back();
			w[e] = tmp;
		} else {
			int x = (int)dis0[u] + (int)w[e] + (int)disnT[v] + (int)disn[0];
			int y = (int)dis0[n-1] + (int)disn[u] + (int)w[e] + (int)dis0T[v];
			int z =   (int)dis0[u] + (int)w[e] + (int)disnT[v]
			        + (int)disn[u] + (int)w[e] + (int)dis0T[v];
			ans = min(ans, p[e] + min({x, y, z}));
		}
	}
	cout << (ans >= inf? -1: ans) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 396 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 352 KB Output is correct
10 Correct 25 ms 340 KB Output is correct
11 Correct 31 ms 420 KB Output is correct
12 Correct 32 ms 420 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 3064 KB Output is correct
2 Correct 26 ms 3068 KB Output is correct
3 Correct 28 ms 3044 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 2 ms 396 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 20 ms 3060 KB Output is correct
10 Correct 24 ms 3020 KB Output is correct
11 Correct 26 ms 3148 KB Output is correct
12 Correct 26 ms 3012 KB Output is correct
13 Correct 27 ms 3044 KB Output is correct
14 Correct 26 ms 3216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 15 ms 2460 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 19 ms 2960 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 24 ms 3020 KB Output is correct
9 Correct 19 ms 3020 KB Output is correct
10 Correct 19 ms 3056 KB Output is correct
11 Correct 21 ms 3036 KB Output is correct
12 Correct 20 ms 3036 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 18 ms 3132 KB Output is correct
20 Correct 19 ms 3036 KB Output is correct
21 Correct 17 ms 3076 KB Output is correct
22 Correct 20 ms 3020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 2 ms 396 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 352 KB Output is correct
10 Correct 25 ms 340 KB Output is correct
11 Correct 31 ms 420 KB Output is correct
12 Correct 32 ms 420 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 2 ms 356 KB Output is correct
17 Correct 25 ms 3064 KB Output is correct
18 Correct 26 ms 3068 KB Output is correct
19 Correct 28 ms 3044 KB Output is correct
20 Correct 3 ms 468 KB Output is correct
21 Correct 2 ms 396 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 20 ms 3060 KB Output is correct
26 Correct 24 ms 3020 KB Output is correct
27 Correct 26 ms 3148 KB Output is correct
28 Correct 26 ms 3012 KB Output is correct
29 Correct 27 ms 3044 KB Output is correct
30 Correct 26 ms 3216 KB Output is correct
31 Correct 2 ms 340 KB Output is correct
32 Correct 1 ms 340 KB Output is correct
33 Correct 15 ms 2460 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 19 ms 2960 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 24 ms 3020 KB Output is correct
39 Correct 19 ms 3020 KB Output is correct
40 Correct 19 ms 3056 KB Output is correct
41 Correct 21 ms 3036 KB Output is correct
42 Correct 20 ms 3036 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 344 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 18 ms 3132 KB Output is correct
50 Correct 19 ms 3036 KB Output is correct
51 Correct 17 ms 3076 KB Output is correct
52 Correct 20 ms 3020 KB Output is correct
53 Correct 27 ms 3488 KB Output is correct
54 Correct 28 ms 3376 KB Output is correct
55 Correct 28 ms 3384 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 2 ms 468 KB Output is correct
58 Correct 118 ms 2756 KB Output is correct
59 Correct 139 ms 2648 KB Output is correct
60 Correct 202 ms 2756 KB Output is correct
61 Correct 110 ms 2652 KB Output is correct
62 Correct 147 ms 2772 KB Output is correct
63 Correct 184 ms 2764 KB Output is correct
64 Correct 651 ms 2896 KB Output is correct
65 Correct 720 ms 2944 KB Output is correct
66 Correct 922 ms 2920 KB Output is correct
67 Correct 17 ms 2580 KB Output is correct
68 Correct 23 ms 3532 KB Output is correct
69 Correct 26 ms 3480 KB Output is correct
70 Correct 30 ms 3584 KB Output is correct
71 Correct 28 ms 3504 KB Output is correct
72 Correct 27 ms 3580 KB Output is correct
73 Correct 29 ms 3588 KB Output is correct
74 Correct 29 ms 3664 KB Output is correct