/*************************************
* author: marvinthang *
* created: 07.12.2023 16:37:49 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i-- > 0; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; }
// end of template
const int INF = (int) 1e9 + 7;
struct Edge {
int u, v, c, d;
Edge(int u = 0, int v = 0, int c = 0, int d = 0): u(u), v(v), c(c), d(d) {}
};
void process(void) {
int n, m; cin >> n >> m;
vector <vector <int>> adj(n), radj(n);
vector <Edge> edges;
REP(i, m) {
int u, v, c, d; cin >> u >> v >> c >> d;
--u; --v;
edges.emplace_back(u, v, c, d);
adj[u].push_back(i);
radj[v].push_back(i);
}
vector <int> dist1, distn;
auto solve = [&] (int s, int t, vector <int> &res) {
vector <int> dists, distt, trace(n);
vector <bool> used(n);
auto Dijkstra = [&] (int s, vector <int> &dist, const vector <vector <int>> &adj) {
dist.assign(n, INF);
fill(ALL(used), false);
dist[s] = trace[s] = 0;
REP(love, n - 1) {
int u = -1;
REP(i, n) if (!used[i] && (u == -1 || dist[i] < dist[u])) u = i;
used[u] = true;
for (int i: adj[u]) {
int v = edges[i].u ^ edges[i].v ^ u;
if (minimize(dist[v], dist[u] + edges[i].c)) trace[v] = i;
}
}
};
res.assign(m + 1, INF);
Dijkstra(t, distt, radj);
Dijkstra(s, dists, adj);
res[m] = dists[t];
vector <bool> in_path(m);
if (res[m] < INF) {
int u = t;
while (u != s) {
in_path[trace[u]] = true;
u = edges[trace[u]].u;
}
}
vector <int> distm;
REP(i, m) {
auto [u, v, c, d] = edges[i];
if (!in_path[i]) {
res[i] = min(res[m], dists[v] + distt[u] + c);
} else {
adj[u].erase(find(ALL(adj[u]), i));
Dijkstra(s, distm, adj);
res[i] = distm[t];
adj[u].push_back(i);
}
}
};
solve(0, n - 1, dist1);
solve(n - 1, 0, distn);
int res = INF + INF;
REP(i, m + 1) if (dist1[i] < INF && distn[i] < INF) minimize(res, dist1[i] + distn[i] + (i == m ? 0 : edges[i].d));
cout << (res >= INF + INF ? -1 : res) << '\n';
}
int main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr);
file("JOI20_ho_t4");
// int t; cin >> t; while (t--)
process();
// cerr << "Time elapsed: " << TIME << " s.\n";
return (0^0);
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:30:61: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:117:2: note: in expansion of macro 'file'
117 | file("JOI20_ho_t4");
| ^~~~
ho_t4.cpp:30:94: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:117:2: note: in expansion of macro 'file'
117 | file("JOI20_ho_t4");
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
31 ms |
348 KB |
Output is correct |
11 |
Correct |
31 ms |
532 KB |
Output is correct |
12 |
Correct |
31 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
2508 KB |
Output is correct |
2 |
Correct |
15 ms |
2256 KB |
Output is correct |
3 |
Correct |
15 ms |
2132 KB |
Output is correct |
4 |
Correct |
2 ms |
344 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
14 ms |
3400 KB |
Output is correct |
10 |
Correct |
15 ms |
3400 KB |
Output is correct |
11 |
Correct |
16 ms |
3328 KB |
Output is correct |
12 |
Correct |
16 ms |
3376 KB |
Output is correct |
13 |
Correct |
16 ms |
3280 KB |
Output is correct |
14 |
Correct |
16 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
480 KB |
Output is correct |
3 |
Correct |
11 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
14 ms |
2256 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
13 ms |
2196 KB |
Output is correct |
9 |
Correct |
13 ms |
3076 KB |
Output is correct |
10 |
Correct |
14 ms |
3024 KB |
Output is correct |
11 |
Correct |
14 ms |
3172 KB |
Output is correct |
12 |
Correct |
14 ms |
3140 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
456 KB |
Output is correct |
19 |
Correct |
14 ms |
3152 KB |
Output is correct |
20 |
Correct |
14 ms |
3024 KB |
Output is correct |
21 |
Correct |
15 ms |
3008 KB |
Output is correct |
22 |
Correct |
14 ms |
3024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
31 ms |
348 KB |
Output is correct |
11 |
Correct |
31 ms |
532 KB |
Output is correct |
12 |
Correct |
31 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
348 KB |
Output is correct |
17 |
Correct |
18 ms |
2508 KB |
Output is correct |
18 |
Correct |
15 ms |
2256 KB |
Output is correct |
19 |
Correct |
15 ms |
2132 KB |
Output is correct |
20 |
Correct |
2 ms |
344 KB |
Output is correct |
21 |
Correct |
2 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
14 ms |
3400 KB |
Output is correct |
26 |
Correct |
15 ms |
3400 KB |
Output is correct |
27 |
Correct |
16 ms |
3328 KB |
Output is correct |
28 |
Correct |
16 ms |
3376 KB |
Output is correct |
29 |
Correct |
16 ms |
3280 KB |
Output is correct |
30 |
Correct |
16 ms |
3412 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
480 KB |
Output is correct |
33 |
Correct |
11 ms |
1748 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
14 ms |
2256 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
13 ms |
2196 KB |
Output is correct |
39 |
Correct |
13 ms |
3076 KB |
Output is correct |
40 |
Correct |
14 ms |
3024 KB |
Output is correct |
41 |
Correct |
14 ms |
3172 KB |
Output is correct |
42 |
Correct |
14 ms |
3140 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
456 KB |
Output is correct |
49 |
Correct |
14 ms |
3152 KB |
Output is correct |
50 |
Correct |
14 ms |
3024 KB |
Output is correct |
51 |
Correct |
15 ms |
3008 KB |
Output is correct |
52 |
Correct |
14 ms |
3024 KB |
Output is correct |
53 |
Correct |
16 ms |
3272 KB |
Output is correct |
54 |
Correct |
16 ms |
3280 KB |
Output is correct |
55 |
Correct |
16 ms |
3244 KB |
Output is correct |
56 |
Correct |
2 ms |
344 KB |
Output is correct |
57 |
Correct |
1 ms |
348 KB |
Output is correct |
58 |
Correct |
83 ms |
2816 KB |
Output is correct |
59 |
Correct |
77 ms |
2772 KB |
Output is correct |
60 |
Correct |
78 ms |
2756 KB |
Output is correct |
61 |
Correct |
64 ms |
3024 KB |
Output is correct |
62 |
Correct |
78 ms |
2772 KB |
Output is correct |
63 |
Correct |
81 ms |
2732 KB |
Output is correct |
64 |
Correct |
130 ms |
2772 KB |
Output is correct |
65 |
Correct |
122 ms |
2648 KB |
Output is correct |
66 |
Correct |
121 ms |
2660 KB |
Output is correct |
67 |
Correct |
11 ms |
2624 KB |
Output is correct |
68 |
Correct |
15 ms |
3396 KB |
Output is correct |
69 |
Correct |
15 ms |
3280 KB |
Output is correct |
70 |
Correct |
17 ms |
3300 KB |
Output is correct |
71 |
Correct |
16 ms |
3276 KB |
Output is correct |
72 |
Correct |
16 ms |
3280 KB |
Output is correct |
73 |
Correct |
16 ms |
3396 KB |
Output is correct |
74 |
Correct |
17 ms |
3280 KB |
Output is correct |