#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define Get(x, i, j) (x[i].empty() ? x.back()[j] : x[i][j])
#include <bits/stdc++.h>
using namespace std;
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
struct edge {
int from, to, weight, cost, id;
edge() {}
edge(int _from, int _to, int _weight, int _cost, int _id) : from(_from), to(_to), weight(_weight), cost(_cost), id(_id) {}
};
struct node {
int distance, parent;
node() {
distance = INT_MAX;
parent = -1;
}
node(int _distance, int _parent) : distance(_distance), parent(_parent) {}
};
vector<node> dijkstra(vector<vector<edge>> graph, int start) {
int n = (int)graph.size();
vector<node> answer(n);
min_heap<pair<int, int>> q;
q.emplace(0, start);
answer[start].distance = 0;
while (!q.empty()) {
auto [d, u] = q.top();
q.pop();
if (answer[u].distance > d) {
continue;
}
for (auto [from, to, weight, cost, id] : graph[u]) {
if (answer[from].distance + weight < answer[to].distance) {
answer[to] = node(answer[from].distance + weight, id);
q.emplace(answer[to].distance, to);
}
}
}
return answer;
}
vector<vector<edge>> transpose(vector<vector<edge>> graph) {
int n = (int)graph.size();
vector<vector<edge>> new_graph(n);
for (int u = 0; u < n; u++) {
for (auto [from, to, weight, cost, id] : graph[u]) {
new_graph[to].emplace_back(to, from, weight, cost, id);
}
}
return new_graph;
}
vector<vector<int>> find_shortest_paths_without_each_edge(vector<vector<edge>> graph, int start) {
int n = (int)graph.size();
int m = 0;
for (int u = 0; u < n; u++) {
for (auto [from, to, weight, cost, id] : graph[u]) {
m = max(m, 1 + id);
}
}
vector<node> result = dijkstra(graph, start);
vector<int> dist(n);
for (int i = 0; i < n; i++) {
dist[i] = result[i].distance;
}
vector<vector<int>> answer(m);
for (auto [distance, parent] : result) {
if (parent != -1) {
if (1.0 * clock() / CLOCKS_PER_SEC >= 0.95) {
cout << -1 << "\n";
exit(0);
}
vector<vector<edge>> new_graph = graph;
bool flag = true;
for (int u = 0; u < n; u++) {
for (int i = 0; i < (int)new_graph[u].size(); i++) {
auto [from, to, weight, cost, id] = new_graph[u][i];
if (id == parent) {
new_graph[u].erase(new_graph[u].begin() + i);
flag = false;
}
}
}
vector<node> new_result = dijkstra(new_graph, start);
answer[parent] = vector<int>(n);
for (int i = 0; i < n; i++) {
answer[parent][i] = new_result[i].distance;
}
}
}
answer.push_back(dist);
return answer;
}
void solve() {
int n, m;
cin >> n >> m;
vector<vector<edge>> graph(n);
for (int i = 0; i < m; i++) {
int from, to, weight, cost;
cin >> from >> to >> weight >> cost;
from--, to--;
graph[from].emplace_back(from, to, weight, cost, i);
}
vector<vector<edge>> rev_graph = transpose(graph);
vector<vector<int>> result_a = find_shortest_paths_without_each_edge(graph, 0);
vector<vector<int>> result_b = find_shortest_paths_without_each_edge(graph, n - 1);
vector<vector<int>> rev_result_a = find_shortest_paths_without_each_edge(rev_graph, 0);
vector<vector<int>> rev_result_b = find_shortest_paths_without_each_edge(rev_graph, n - 1);
int ans = INT_MAX;
if (result_a.back()[n - 1] != INT_MAX && result_b.back()[0] != INT_MAX) {
ans = result_a.back()[n - 1] + result_b.back()[0];
}
for (int u = 0; u < n; u++) {
for (auto [from, to, weight, cost, id] : graph[u]) {
int AB = INT_MAX, BA = INT_MAX;
AB = min(AB, Get(result_a, id, n - 1));
if (Get(result_a, id, to) != INT_MAX && Get(rev_result_b, id, from) != INT_MAX) {
AB = min(AB, Get(result_a, id, to) + Get(rev_result_b, id, from) + weight);
}
BA = min(BA, Get(result_b, id, 0));
if (Get(result_b, id, to) != INT_MAX && Get(rev_result_a, id, from) != INT_MAX) {
BA = min(BA, Get(result_b, id, to) + Get(rev_result_a, id, from) + weight);
}
if (AB == INT_MAX || BA == INT_MAX) {
continue;
}
ans = min(ans, AB + BA + cost);
}
}
cout << (ans == INT_MAX ? -1 : ans) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
Compilation message
ho_t4.cpp: In function 'std::vector<std::vector<int> > find_shortest_paths_without_each_edge(std::vector<std::vector<edge> >, int)':
ho_t4.cpp:83:12: warning: variable 'flag' set but not used [-Wunused-but-set-variable]
83 | bool flag = true;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
1140 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
51 ms |
1196 KB |
Output is correct |
4 |
Correct |
65 ms |
1152 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
6 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
64 ms |
1156 KB |
Output is correct |
11 |
Correct |
56 ms |
1136 KB |
Output is correct |
12 |
Correct |
60 ms |
1128 KB |
Output is correct |
13 |
Correct |
18 ms |
916 KB |
Output is correct |
14 |
Correct |
40 ms |
1048 KB |
Output is correct |
15 |
Correct |
40 ms |
1000 KB |
Output is correct |
16 |
Correct |
51 ms |
1080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
722 ms |
12624 KB |
Output is correct |
2 |
Correct |
731 ms |
12720 KB |
Output is correct |
3 |
Correct |
676 ms |
12740 KB |
Output is correct |
4 |
Correct |
45 ms |
1356 KB |
Output is correct |
5 |
Correct |
34 ms |
1084 KB |
Output is correct |
6 |
Correct |
10 ms |
680 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
199 ms |
12600 KB |
Output is correct |
10 |
Correct |
202 ms |
12416 KB |
Output is correct |
11 |
Correct |
431 ms |
12364 KB |
Output is correct |
12 |
Correct |
487 ms |
12520 KB |
Output is correct |
13 |
Correct |
400 ms |
12336 KB |
Output is correct |
14 |
Correct |
436 ms |
12844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
1228 KB |
Output is correct |
2 |
Correct |
9 ms |
624 KB |
Output is correct |
3 |
Correct |
368 ms |
9848 KB |
Output is correct |
4 |
Correct |
6 ms |
468 KB |
Output is correct |
5 |
Correct |
393 ms |
12496 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
168 ms |
12460 KB |
Output is correct |
9 |
Correct |
196 ms |
12448 KB |
Output is correct |
10 |
Correct |
268 ms |
12280 KB |
Output is correct |
11 |
Correct |
238 ms |
12296 KB |
Output is correct |
12 |
Correct |
242 ms |
12608 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
216 KB |
Output is correct |
19 |
Correct |
255 ms |
12696 KB |
Output is correct |
20 |
Correct |
279 ms |
12476 KB |
Output is correct |
21 |
Correct |
238 ms |
12452 KB |
Output is correct |
22 |
Correct |
235 ms |
12328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
1140 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
51 ms |
1196 KB |
Output is correct |
4 |
Correct |
65 ms |
1152 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
6 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
64 ms |
1156 KB |
Output is correct |
11 |
Correct |
56 ms |
1136 KB |
Output is correct |
12 |
Correct |
60 ms |
1128 KB |
Output is correct |
13 |
Correct |
18 ms |
916 KB |
Output is correct |
14 |
Correct |
40 ms |
1048 KB |
Output is correct |
15 |
Correct |
40 ms |
1000 KB |
Output is correct |
16 |
Correct |
51 ms |
1080 KB |
Output is correct |
17 |
Correct |
722 ms |
12624 KB |
Output is correct |
18 |
Correct |
731 ms |
12720 KB |
Output is correct |
19 |
Correct |
676 ms |
12740 KB |
Output is correct |
20 |
Correct |
45 ms |
1356 KB |
Output is correct |
21 |
Correct |
34 ms |
1084 KB |
Output is correct |
22 |
Correct |
10 ms |
680 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
199 ms |
12600 KB |
Output is correct |
26 |
Correct |
202 ms |
12416 KB |
Output is correct |
27 |
Correct |
431 ms |
12364 KB |
Output is correct |
28 |
Correct |
487 ms |
12520 KB |
Output is correct |
29 |
Correct |
400 ms |
12336 KB |
Output is correct |
30 |
Correct |
436 ms |
12844 KB |
Output is correct |
31 |
Correct |
45 ms |
1228 KB |
Output is correct |
32 |
Correct |
9 ms |
624 KB |
Output is correct |
33 |
Correct |
368 ms |
9848 KB |
Output is correct |
34 |
Correct |
6 ms |
468 KB |
Output is correct |
35 |
Correct |
393 ms |
12496 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
168 ms |
12460 KB |
Output is correct |
39 |
Correct |
196 ms |
12448 KB |
Output is correct |
40 |
Correct |
268 ms |
12280 KB |
Output is correct |
41 |
Correct |
238 ms |
12296 KB |
Output is correct |
42 |
Correct |
242 ms |
12608 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
216 KB |
Output is correct |
49 |
Correct |
255 ms |
12696 KB |
Output is correct |
50 |
Correct |
279 ms |
12476 KB |
Output is correct |
51 |
Correct |
238 ms |
12452 KB |
Output is correct |
52 |
Correct |
235 ms |
12328 KB |
Output is correct |
53 |
Correct |
799 ms |
12404 KB |
Output is correct |
54 |
Correct |
792 ms |
12368 KB |
Output is correct |
55 |
Correct |
807 ms |
12584 KB |
Output is correct |
56 |
Correct |
50 ms |
1160 KB |
Output is correct |
57 |
Correct |
53 ms |
1132 KB |
Output is correct |
58 |
Correct |
661 ms |
10124 KB |
Output is correct |
59 |
Correct |
756 ms |
10016 KB |
Output is correct |
60 |
Correct |
703 ms |
10064 KB |
Output is correct |
61 |
Correct |
611 ms |
10092 KB |
Output is correct |
62 |
Correct |
712 ms |
10136 KB |
Output is correct |
63 |
Correct |
637 ms |
10036 KB |
Output is correct |
64 |
Incorrect |
954 ms |
6256 KB |
Output isn't correct |
65 |
Halted |
0 ms |
0 KB |
- |