#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
template <typename X, typename Y> istream& operator>>(istream& is, pair<X, Y>& p) { return is >> p.first >> p.second; }
template <typename T> struct wedge {
int u, v; T w, d;
wedge reverse() const { return {v, u, w}; }
friend ostream& operator<<(ostream& os, const wedge& e) {
return os << "{u: " << e.u << ", v: " << e.v << ", w: " << e.w << "}";
}
};
template <bool Directed, typename TEdge, bool Index> struct graph {
using EType = TEdge;
vector<TEdge> edges;
vector<vector<int>> adj;
graph(int n) : adj(n + Index) {}
graph(int n, int m) : graph(n) { edges.reserve(m << not Directed); }
TEdge& operator()(int e) { return edges[e]; }
vector<int>& operator[](int u) { return adj[u]; }
int size() { return adj.size() - Index; }
void append(int u, const TEdge& e) {
adj[u].push_back(edges.size());
edges.push_back(e);
}
void add_edge(const TEdge& e) {
append(e.u, e);
if(not Directed) append(e.v, e.reverse());
}
};
const ll INF = lim<int>::max();
template <bool Directed, typename T, bool Index>
pair<vector<T>, vector<int>> sssp(graph<Directed, wedge<T>, Index>& g, const vector<int>& s) {
vector<int> vis(g.adj.size()), p(g.adj.size(), -1);
vector<T> d(g.adj.size(), INF);
priority_queue<pair<T, int>> pq;
for(int u : s) {
pq.push({d[u] = 0, u});
}
while(not pq.empty()) {
int u = pq.top().second; pq.pop();
if(not vis[u]) {
vis[u] = true;
for(int e : g[u]) {
if(not vis[g(e).v] and d[g(e).v] > d[u] + g(e).w) {
pq.push({-(d[g(e).v] = d[u] + g(e).w), g(p[g(e).v] = e).v});
}
}
}
}
return {d, p};
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
graph<1, wedge<ll>, 1> g(n, m), gt(n, m);
vector<int> u(m), v(m);
vector<ll> c(m), d(m);
for(int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> c[i] >> d[i];
g.add_edge({u[i], v[i], c[i], d[i]});
gt.add_edge({v[i], u[i], c[i], d[i]});
}
auto ds = sssp(g, {1}).first, dsr = sssp(gt, {1}).first;
auto dt = sssp(g, {n}).first, dtr = sssp(gt, {n}).first;
ll ans = ds[n] + dt[1];
if(m <= 1000) {
for(int i = 0; i < m; i++) {
graph<1, wedge<ll>, 1> g(n, m);
for(int j = 0; j < m; j++) {
if(j == i) {
g.add_edge({v[j], u[j], c[j]});
} else {
g.add_edge({u[j], v[j], c[j]});
}
}
ans = min(ans, sssp(g, {1}).first[n] + sssp(g, {n}).first[1] + d[i]);
}
} else {
for(int i = 0; i < m; i++) {
ans = min(ans,
min(ds[n], ds[v[i]] + c[i] + dtr[u[i]]) +
min(dt[1], dt[v[i]] + c[i] + dsr[u[i]]) +
d[i]
);
}
}
cout << (ans < INF ? ans : -1) << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
512 KB |
Output is correct |
2 |
Correct |
9 ms |
384 KB |
Output is correct |
3 |
Correct |
111 ms |
512 KB |
Output is correct |
4 |
Correct |
124 ms |
560 KB |
Output is correct |
5 |
Correct |
48 ms |
512 KB |
Output is correct |
6 |
Correct |
9 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
23 ms |
512 KB |
Output is correct |
10 |
Correct |
142 ms |
512 KB |
Output is correct |
11 |
Correct |
141 ms |
512 KB |
Output is correct |
12 |
Correct |
138 ms |
632 KB |
Output is correct |
13 |
Correct |
81 ms |
544 KB |
Output is correct |
14 |
Correct |
95 ms |
512 KB |
Output is correct |
15 |
Correct |
94 ms |
512 KB |
Output is correct |
16 |
Correct |
94 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
5496 KB |
Output is correct |
2 |
Correct |
28 ms |
5504 KB |
Output is correct |
3 |
Correct |
30 ms |
5752 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
85 ms |
512 KB |
Output is correct |
6 |
Correct |
23 ms |
384 KB |
Output is correct |
7 |
Correct |
13 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
30 ms |
5624 KB |
Output is correct |
10 |
Correct |
30 ms |
5632 KB |
Output is correct |
11 |
Correct |
29 ms |
5632 KB |
Output is correct |
12 |
Correct |
31 ms |
5752 KB |
Output is correct |
13 |
Correct |
30 ms |
5752 KB |
Output is correct |
14 |
Correct |
35 ms |
5752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
512 KB |
Output is correct |
2 |
Correct |
10 ms |
384 KB |
Output is correct |
3 |
Correct |
23 ms |
4352 KB |
Output is correct |
4 |
Correct |
9 ms |
384 KB |
Output is correct |
5 |
Correct |
28 ms |
5368 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
26 ms |
5376 KB |
Output is correct |
9 |
Correct |
26 ms |
5376 KB |
Output is correct |
10 |
Incorrect |
40 ms |
5504 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
512 KB |
Output is correct |
2 |
Correct |
9 ms |
384 KB |
Output is correct |
3 |
Correct |
111 ms |
512 KB |
Output is correct |
4 |
Correct |
124 ms |
560 KB |
Output is correct |
5 |
Correct |
48 ms |
512 KB |
Output is correct |
6 |
Correct |
9 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
23 ms |
512 KB |
Output is correct |
10 |
Correct |
142 ms |
512 KB |
Output is correct |
11 |
Correct |
141 ms |
512 KB |
Output is correct |
12 |
Correct |
138 ms |
632 KB |
Output is correct |
13 |
Correct |
81 ms |
544 KB |
Output is correct |
14 |
Correct |
95 ms |
512 KB |
Output is correct |
15 |
Correct |
94 ms |
512 KB |
Output is correct |
16 |
Correct |
94 ms |
512 KB |
Output is correct |
17 |
Correct |
29 ms |
5496 KB |
Output is correct |
18 |
Correct |
28 ms |
5504 KB |
Output is correct |
19 |
Correct |
30 ms |
5752 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
85 ms |
512 KB |
Output is correct |
22 |
Correct |
23 ms |
384 KB |
Output is correct |
23 |
Correct |
13 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
30 ms |
5624 KB |
Output is correct |
26 |
Correct |
30 ms |
5632 KB |
Output is correct |
27 |
Correct |
29 ms |
5632 KB |
Output is correct |
28 |
Correct |
31 ms |
5752 KB |
Output is correct |
29 |
Correct |
30 ms |
5752 KB |
Output is correct |
30 |
Correct |
35 ms |
5752 KB |
Output is correct |
31 |
Correct |
97 ms |
512 KB |
Output is correct |
32 |
Correct |
10 ms |
384 KB |
Output is correct |
33 |
Correct |
23 ms |
4352 KB |
Output is correct |
34 |
Correct |
9 ms |
384 KB |
Output is correct |
35 |
Correct |
28 ms |
5368 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
26 ms |
5376 KB |
Output is correct |
39 |
Correct |
26 ms |
5376 KB |
Output is correct |
40 |
Incorrect |
40 ms |
5504 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |