#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt,fma")
#include <bits/stdc++.h>
using namespace std;
struct cmp {
bool operator()(const pair<int, int>& a, const pair<int, int>& b) const {
return a.first > b.first;
}
};
const int N = 200, M = 50000;
vector<int> graph[2][N];
int deg[N], rev_deg[N], path[N], dist[N], _from[M], _to[M], weight[M], cost[M];
int dist_w[4][M][N], dist_i[4][N];
int n, m;
int from(int g, int id) {
return g == 0 ? _from[id] : _to[id];
}
int to(int g, int id) {
return g == 0 ? _to[id] : _from[id];
}
void dijkstra(int start, int g, bool flag, int del = -1) {
fill(dist, dist + n, INT_MAX);
if (flag) fill(path, path + n, -1);
dist[start] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> q;
q.emplace(0, start);
while (!q.empty()) {
auto [d, u] = q.top();
q.pop();
if (dist[u] > d) {
continue;
}
for (int e : graph[g][u]) {
if (e != del && dist[u] + weight[e] < dist[to(g, e)]) {
dist[to(g, e)] = dist[u] + weight[e];
if (flag) path[to(g, e)] = e;
q.emplace(dist[to(g, e)], to(g, e));
}
}
}
}
void _solve(int g, int start, int it) {
dijkstra(start, g, true);
memcpy(dist_i[it], dist, sizeof dist);
for (int i = 0; i < n; i++) {
if (path[i] != -1) {
dijkstra(start, g, false, path[i]);
memcpy(dist_w[it][path[i]], dist, sizeof dist);
}
}
}
int Get(int it, int id, int u) {
return dist_w[it][id][u] == -1 ? dist_i[it][u] : dist_w[it][id][u];
}
void solve() {
cin >> n >> m;
memset(dist_w, -1, sizeof dist_w);
for (int i = 0; i < m; i++) {
cin >> _from[i] >> _to[i] >> weight[i] >> cost[i];
_from[i]--, _to[i]--;
deg[_from[i]]++, rev_deg[_to[i]]++;
}
for (int i = 0; i < n; i++) {
graph[0][i].reserve(deg[i]);
graph[1][i].reserve(rev_deg[i]);
}
for (int i = 0; i < m; i++) {
graph[0][_from[i]].push_back(i);
graph[1][_to[i]].push_back(i);
}
_solve(0, 0, 0);
_solve(0, n - 1, 1);
_solve(1, 0, 2);
_solve(1, n - 1, 3);
int ans = INT_MAX;
if (dist_i[0][n - 1] != INT_MAX && dist_i[1][0] != INT_MAX) {
ans = dist_i[0][n - 1] + dist_i[1][0];
}
for (int u = 0; u < n; u++) {
for (int id : graph[0][u]) {
int AB = INT_MAX, BA = INT_MAX;
AB = min(AB, Get(0, id, n - 1));
if (Get(0, id, to(0, id)) != INT_MAX && Get(3, id, from(0, id)) != INT_MAX) {
AB = min(AB, Get(0, id, to(0, id)) + Get(3, id, from(0, id)) + weight[id]);
}
BA = min(BA, Get(1, id, 0));
if (Get(1, id, to(0, id)) != INT_MAX && Get(2, id, from(0, id)) != INT_MAX) {
BA = min(BA, Get(1, id, to(0, id)) + Get(2, id, from(0, id)) + weight[id]);
}
if (AB == INT_MAX || BA == INT_MAX) {
continue;
}
ans = min(ans, AB + BA + cost[id]);
}
}
cout << (ans == INT_MAX ? -1 : ans) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
156892 KB |
Output is correct |
2 |
Correct |
62 ms |
156852 KB |
Output is correct |
3 |
Correct |
80 ms |
156908 KB |
Output is correct |
4 |
Correct |
79 ms |
156816 KB |
Output is correct |
5 |
Correct |
62 ms |
156904 KB |
Output is correct |
6 |
Correct |
63 ms |
156892 KB |
Output is correct |
7 |
Correct |
64 ms |
156808 KB |
Output is correct |
8 |
Correct |
61 ms |
156784 KB |
Output is correct |
9 |
Correct |
65 ms |
156892 KB |
Output is correct |
10 |
Correct |
88 ms |
156892 KB |
Output is correct |
11 |
Correct |
92 ms |
156924 KB |
Output is correct |
12 |
Correct |
92 ms |
156928 KB |
Output is correct |
13 |
Correct |
66 ms |
156924 KB |
Output is correct |
14 |
Correct |
70 ms |
156872 KB |
Output is correct |
15 |
Correct |
71 ms |
156876 KB |
Output is correct |
16 |
Correct |
72 ms |
156876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
476 ms |
158156 KB |
Output is correct |
2 |
Correct |
558 ms |
158072 KB |
Output is correct |
3 |
Correct |
536 ms |
158068 KB |
Output is correct |
4 |
Correct |
83 ms |
156948 KB |
Output is correct |
5 |
Correct |
68 ms |
156916 KB |
Output is correct |
6 |
Correct |
62 ms |
156820 KB |
Output is correct |
7 |
Correct |
69 ms |
156900 KB |
Output is correct |
8 |
Correct |
62 ms |
156848 KB |
Output is correct |
9 |
Correct |
137 ms |
158020 KB |
Output is correct |
10 |
Correct |
138 ms |
158032 KB |
Output is correct |
11 |
Correct |
302 ms |
157940 KB |
Output is correct |
12 |
Correct |
316 ms |
158068 KB |
Output is correct |
13 |
Correct |
314 ms |
158060 KB |
Output is correct |
14 |
Correct |
417 ms |
158060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
156832 KB |
Output is correct |
2 |
Correct |
66 ms |
156856 KB |
Output is correct |
3 |
Correct |
159 ms |
157820 KB |
Output is correct |
4 |
Correct |
66 ms |
156800 KB |
Output is correct |
5 |
Correct |
161 ms |
157988 KB |
Output is correct |
6 |
Correct |
62 ms |
156764 KB |
Output is correct |
7 |
Correct |
68 ms |
156804 KB |
Output is correct |
8 |
Correct |
96 ms |
158036 KB |
Output is correct |
9 |
Correct |
97 ms |
157964 KB |
Output is correct |
10 |
Correct |
133 ms |
158060 KB |
Output is correct |
11 |
Correct |
128 ms |
157992 KB |
Output is correct |
12 |
Correct |
160 ms |
158016 KB |
Output is correct |
13 |
Correct |
62 ms |
156780 KB |
Output is correct |
14 |
Correct |
64 ms |
156820 KB |
Output is correct |
15 |
Correct |
63 ms |
156796 KB |
Output is correct |
16 |
Correct |
63 ms |
156856 KB |
Output is correct |
17 |
Correct |
73 ms |
156788 KB |
Output is correct |
18 |
Correct |
63 ms |
156812 KB |
Output is correct |
19 |
Correct |
132 ms |
157988 KB |
Output is correct |
20 |
Correct |
139 ms |
158084 KB |
Output is correct |
21 |
Correct |
132 ms |
158060 KB |
Output is correct |
22 |
Correct |
148 ms |
157948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
156892 KB |
Output is correct |
2 |
Correct |
62 ms |
156852 KB |
Output is correct |
3 |
Correct |
80 ms |
156908 KB |
Output is correct |
4 |
Correct |
79 ms |
156816 KB |
Output is correct |
5 |
Correct |
62 ms |
156904 KB |
Output is correct |
6 |
Correct |
63 ms |
156892 KB |
Output is correct |
7 |
Correct |
64 ms |
156808 KB |
Output is correct |
8 |
Correct |
61 ms |
156784 KB |
Output is correct |
9 |
Correct |
65 ms |
156892 KB |
Output is correct |
10 |
Correct |
88 ms |
156892 KB |
Output is correct |
11 |
Correct |
92 ms |
156924 KB |
Output is correct |
12 |
Correct |
92 ms |
156928 KB |
Output is correct |
13 |
Correct |
66 ms |
156924 KB |
Output is correct |
14 |
Correct |
70 ms |
156872 KB |
Output is correct |
15 |
Correct |
71 ms |
156876 KB |
Output is correct |
16 |
Correct |
72 ms |
156876 KB |
Output is correct |
17 |
Correct |
476 ms |
158156 KB |
Output is correct |
18 |
Correct |
558 ms |
158072 KB |
Output is correct |
19 |
Correct |
536 ms |
158068 KB |
Output is correct |
20 |
Correct |
83 ms |
156948 KB |
Output is correct |
21 |
Correct |
68 ms |
156916 KB |
Output is correct |
22 |
Correct |
62 ms |
156820 KB |
Output is correct |
23 |
Correct |
69 ms |
156900 KB |
Output is correct |
24 |
Correct |
62 ms |
156848 KB |
Output is correct |
25 |
Correct |
137 ms |
158020 KB |
Output is correct |
26 |
Correct |
138 ms |
158032 KB |
Output is correct |
27 |
Correct |
302 ms |
157940 KB |
Output is correct |
28 |
Correct |
316 ms |
158068 KB |
Output is correct |
29 |
Correct |
314 ms |
158060 KB |
Output is correct |
30 |
Correct |
417 ms |
158060 KB |
Output is correct |
31 |
Correct |
67 ms |
156832 KB |
Output is correct |
32 |
Correct |
66 ms |
156856 KB |
Output is correct |
33 |
Correct |
159 ms |
157820 KB |
Output is correct |
34 |
Correct |
66 ms |
156800 KB |
Output is correct |
35 |
Correct |
161 ms |
157988 KB |
Output is correct |
36 |
Correct |
62 ms |
156764 KB |
Output is correct |
37 |
Correct |
68 ms |
156804 KB |
Output is correct |
38 |
Correct |
96 ms |
158036 KB |
Output is correct |
39 |
Correct |
97 ms |
157964 KB |
Output is correct |
40 |
Correct |
133 ms |
158060 KB |
Output is correct |
41 |
Correct |
128 ms |
157992 KB |
Output is correct |
42 |
Correct |
160 ms |
158016 KB |
Output is correct |
43 |
Correct |
62 ms |
156780 KB |
Output is correct |
44 |
Correct |
64 ms |
156820 KB |
Output is correct |
45 |
Correct |
63 ms |
156796 KB |
Output is correct |
46 |
Correct |
63 ms |
156856 KB |
Output is correct |
47 |
Correct |
73 ms |
156788 KB |
Output is correct |
48 |
Correct |
63 ms |
156812 KB |
Output is correct |
49 |
Correct |
132 ms |
157988 KB |
Output is correct |
50 |
Correct |
139 ms |
158084 KB |
Output is correct |
51 |
Correct |
132 ms |
158060 KB |
Output is correct |
52 |
Correct |
148 ms |
157948 KB |
Output is correct |
53 |
Correct |
562 ms |
157984 KB |
Output is correct |
54 |
Correct |
656 ms |
158064 KB |
Output is correct |
55 |
Correct |
540 ms |
158156 KB |
Output is correct |
56 |
Correct |
83 ms |
156808 KB |
Output is correct |
57 |
Correct |
80 ms |
156844 KB |
Output is correct |
58 |
Correct |
488 ms |
157840 KB |
Output is correct |
59 |
Correct |
463 ms |
157852 KB |
Output is correct |
60 |
Correct |
445 ms |
157920 KB |
Output is correct |
61 |
Correct |
428 ms |
157852 KB |
Output is correct |
62 |
Correct |
436 ms |
157856 KB |
Output is correct |
63 |
Correct |
469 ms |
157772 KB |
Output is correct |
64 |
Execution timed out |
1088 ms |
158304 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |