답안 #535784

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
535784 2022-03-11T07:40:58 Z abc864197532 Olympic Bus (JOI20_ho_t4) C++17
100 / 100
488 ms 3964 KB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define pii pair<int,int>
#define X first
#define Y second
#define all(x) x.begin(), x.end()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T i, U ...j) {
	cout << i << ' ', abc(j...);
}
template <typename T> void printv(T l, T r) {
	for (; l != r; ++l)
		cout << *l << " \n"[l + 1 == r];
}
#ifdef Doludu
#define test(x...) abc("[" + string(#x) + "]", x)
#define owo freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#else
#define test(x...) void(0)
#define owo ios::sync_with_stdio(false), cin.tie(0)
#endif
const int N = 205, M = 50001;

array <int, 4> E[M];

int dis1[2][2][N][N]; // dis[j][k] -> from s or t to k, forbid j
vector <int> adj[N][2];
int n, m;

void build1(int ty, int s, int forbid) {
	for (int t : {0, 1}) {
		vector <bool> vis(n, false);
		for (int i = 0; i < n; ++i) {
			dis1[ty][t][forbid][i] = 1 << 30;
		}
		dis1[ty][t][forbid][s] = 0;
		if (s == forbid)
			continue;
		while (true) {
			int v = -1;
			for (int i = 0; i < n; ++i) if (!vis[i]) {
				if (v == -1 || dis1[ty][t][forbid][v] > dis1[ty][t][forbid][i])
					v = i;
			}
			if (v == -1 || dis1[ty][t][forbid][v] == 1 << 30)
				break;
			vis[v] = true;
			for (int id : adj[v][t]) {
				int u = E[id][0] ^ E[id][1] ^ v;
				if (u == forbid)
					continue;
				if (dis1[ty][t][forbid][u] > dis1[ty][t][forbid][v] + E[id][2])
					dis1[ty][t][forbid][u] = dis1[ty][t][forbid][v] + E[id][2];
			}
		}
	}
}

int dis2[2][M]; // from s to t, avoid edge (i, j)

void build_special(int forbid, int s, int t, int ty) {
	// ignore edge (i, j), run shortest path
	vector <int> dis(n, 1 << 30);
	vector <bool> vis(n, false);
	dis[s] = 0;
	while (true) {
		int v = -1;
		for (int i = 0; i < n; ++i) if (!vis[i]) {
			if (v == -1 || dis[v] > dis[i])
				v = i;
		}
		if (v == -1 || dis[v] == 1 << 30)
			break;
		vis[v] = true;
		for (int id : adj[v][0]) if (id != forbid) {
			int u = E[id][0] ^ E[id][1] ^ v;
			if (dis[u] > dis[v] + E[id][2])
				dis[u] = dis[v] + E[id][2];
		}
	}
	dis2[ty][forbid] = dis[t];
}

lli ans = 0;

void build2(int s, int t, int ty) {
	vector <int> dis(n, 1 << 30);
	vector <bool> vis(n, false);
	vector <int> rt(n, -1);
	dis[s] = 0;
	while (true) {
		int v = -1;
		for (int i = 0; i < n; ++i) if (!vis[i]) {
			if (v == -1 || dis[v] > dis[i])
				v = i;
		}
		if (v == -1 || dis[v] == 1 << 30)
			break;
		vis[v] = true;
		for (int id : adj[v][0]) {
			int u = E[id][0] ^ E[id][1] ^ v;
			if (dis[u] > dis[v] + E[id][2])
				dis[u] = dis[v] + E[id][2], rt[u] = id;
		}
	}
	ans += dis[t];
	vector <bool> in_path(m, false);
	if (dis[t] != 1 << 30) {
		int now = t;
		while (now != s) {
			in_path[rt[now]] = true;
			now = E[rt[now]][0] ^ E[rt[now]][1] ^ now;
		}
	}
	for (int i = 0; i < m; ++i) {
		if (in_path[i])
			build_special(i, s, t, ty);
		else
			dis2[ty][i] = dis[t];
	}
}

int main () {
	owo;
	cin >> n >> m;
	int s = 0, t = n - 1;
	for (int i = 0, u, v, c, d; i < m; ++i) {
		cin >> u >> v >> c >> d, --u, --v;
		E[i] = {u, v, c, d};
		adj[u][0].pb(i);
		adj[v][1].pb(i);
	}
	for (int i = 0; i < n; ++i) {
		build1(0, s, i);
		build1(1, t, i);
	}
	build2(s, t, 0), build2(t, s, 1);
	for (int i = 0; i < m; ++i) {
		int u = E[i][0], v = E[i][1];
		lli rev = E[i][3];
		lli st = min((long long)dis1[0][0][u][v] + E[i][2] + dis1[1][1][v][u], 1ll * dis2[0][i]);
		lli ts = min((long long)dis1[1][0][u][v] + E[i][2] + dis1[0][1][v][u], 1ll * dis2[1][i]);
		ans = min(ans, rev + st + ts);
	}
	if (ans >= 1 << 30)
		ans = -1;
	cout << ans << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 1000 KB Output is correct
2 Correct 11 ms 980 KB Output is correct
3 Correct 89 ms 964 KB Output is correct
4 Correct 99 ms 952 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 23 ms 1016 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 121 ms 1004 KB Output is correct
11 Correct 122 ms 1020 KB Output is correct
12 Correct 122 ms 1024 KB Output is correct
13 Correct 45 ms 1024 KB Output is correct
14 Correct 67 ms 1048 KB Output is correct
15 Correct 72 ms 928 KB Output is correct
16 Correct 73 ms 952 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 214 ms 2704 KB Output is correct
2 Correct 226 ms 2796 KB Output is correct
3 Correct 230 ms 2780 KB Output is correct
4 Correct 85 ms 1028 KB Output is correct
5 Correct 80 ms 932 KB Output is correct
6 Correct 30 ms 980 KB Output is correct
7 Correct 7 ms 944 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 107 ms 2744 KB Output is correct
10 Correct 104 ms 2800 KB Output is correct
11 Correct 180 ms 2776 KB Output is correct
12 Correct 182 ms 2764 KB Output is correct
13 Correct 171 ms 2772 KB Output is correct
14 Correct 174 ms 2820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 940 KB Output is correct
2 Correct 34 ms 984 KB Output is correct
3 Correct 180 ms 2384 KB Output is correct
4 Correct 25 ms 908 KB Output is correct
5 Correct 202 ms 2764 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 113 ms 2736 KB Output is correct
9 Correct 105 ms 2792 KB Output is correct
10 Correct 163 ms 2876 KB Output is correct
11 Correct 163 ms 2756 KB Output is correct
12 Correct 162 ms 2816 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 264 KB Output is correct
19 Correct 160 ms 2956 KB Output is correct
20 Correct 162 ms 2824 KB Output is correct
21 Correct 165 ms 2764 KB Output is correct
22 Correct 165 ms 2716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 1000 KB Output is correct
2 Correct 11 ms 980 KB Output is correct
3 Correct 89 ms 964 KB Output is correct
4 Correct 99 ms 952 KB Output is correct
5 Correct 2 ms 468 KB Output is correct
6 Correct 23 ms 1016 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 121 ms 1004 KB Output is correct
11 Correct 122 ms 1020 KB Output is correct
12 Correct 122 ms 1024 KB Output is correct
13 Correct 45 ms 1024 KB Output is correct
14 Correct 67 ms 1048 KB Output is correct
15 Correct 72 ms 928 KB Output is correct
16 Correct 73 ms 952 KB Output is correct
17 Correct 214 ms 2704 KB Output is correct
18 Correct 226 ms 2796 KB Output is correct
19 Correct 230 ms 2780 KB Output is correct
20 Correct 85 ms 1028 KB Output is correct
21 Correct 80 ms 932 KB Output is correct
22 Correct 30 ms 980 KB Output is correct
23 Correct 7 ms 944 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 107 ms 2744 KB Output is correct
26 Correct 104 ms 2800 KB Output is correct
27 Correct 180 ms 2776 KB Output is correct
28 Correct 182 ms 2764 KB Output is correct
29 Correct 171 ms 2772 KB Output is correct
30 Correct 174 ms 2820 KB Output is correct
31 Correct 87 ms 940 KB Output is correct
32 Correct 34 ms 984 KB Output is correct
33 Correct 180 ms 2384 KB Output is correct
34 Correct 25 ms 908 KB Output is correct
35 Correct 202 ms 2764 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 113 ms 2736 KB Output is correct
39 Correct 105 ms 2792 KB Output is correct
40 Correct 163 ms 2876 KB Output is correct
41 Correct 163 ms 2756 KB Output is correct
42 Correct 162 ms 2816 KB Output is correct
43 Correct 0 ms 340 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 264 KB Output is correct
49 Correct 160 ms 2956 KB Output is correct
50 Correct 162 ms 2824 KB Output is correct
51 Correct 165 ms 2764 KB Output is correct
52 Correct 165 ms 2716 KB Output is correct
53 Correct 219 ms 2720 KB Output is correct
54 Correct 226 ms 2892 KB Output is correct
55 Correct 236 ms 2736 KB Output is correct
56 Correct 84 ms 1100 KB Output is correct
57 Correct 82 ms 992 KB Output is correct
58 Correct 272 ms 2392 KB Output is correct
59 Correct 267 ms 2360 KB Output is correct
60 Correct 282 ms 2412 KB Output is correct
61 Correct 249 ms 2356 KB Output is correct
62 Correct 268 ms 2380 KB Output is correct
63 Correct 268 ms 2352 KB Output is correct
64 Correct 488 ms 2480 KB Output is correct
65 Correct 450 ms 3284 KB Output is correct
66 Correct 416 ms 3392 KB Output is correct
67 Correct 13 ms 2640 KB Output is correct
68 Correct 113 ms 3932 KB Output is correct
69 Correct 107 ms 3888 KB Output is correct
70 Correct 176 ms 3940 KB Output is correct
71 Correct 187 ms 3964 KB Output is correct
72 Correct 174 ms 3932 KB Output is correct
73 Correct 177 ms 3948 KB Output is correct
74 Correct 180 ms 3884 KB Output is correct