//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;
constexpr ll INF64 = 1'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;
random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());
template<typename T>
bool assign_max(T& a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool assign_min(T& a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
T square(T a) {
return a * a;
}
template<>
struct std::hash<pair<ll, ll>> {
ll operator() (pair<ll, ll> p) const {
return ((__int128)p.first * MOD + p.second) % INF64;
}
};
pair<vector<ll>, vector<ll>> get_dist(ll v, vector<tuple<ll, ll, ll, ll>>& arr, ll n) {
vector<vector<ll>> to(n, vector<ll>(n, INF64));
for (auto[a, b, c, d] : arr) {
assign_min(to[a][b], c);
}
vector<ll> dist(n, INF64);
vector<ll> p(n, -1);
dist[v] = 0;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> q;
q.push(make_pair(0ll, v));
while (!q.empty()) {
auto[d, x] = q.top();
q.pop();
if (d != dist[x]) {
continue;
}
for (ll i = 0; i < n; i++) {
if (assign_min(dist[i], d + to[x][i])) {
p[i] = x;
q.push(make_pair(dist[i], i));
}
}
}
return make_pair(dist, p);
}
vector<ll> get_way(ll v, vector<ll>& dist, vector<ll>& p, vector<vector<ll>>& rarr, vector<tuple<ll, ll, ll, ll>>& arr) {
ll now = v;
ll nd = dist[v];
vector<ll> ans;
if (p[now] == -1) {
return ans;
}
while (p[now] != -1) {
for (auto i : rarr[now]) {
if (nd == get<2>(arr[i]) + dist[get<0>(arr[i])] && get<0>(arr[i]) == p[now]) {
ans.push_back(i);
nd -= get<2>(arr[i]);
now = get<0>(arr[i]);
break;
}
}
}
reverse(ans.begin(), ans.end());
return ans;
}
void solve() {
ll n, m;
cin >> n >> m;
vector<tuple<ll, ll, ll, ll>> arr(m);
for (auto&[a, b, c, d] : arr) {
cin >> a >> b >> c >> d;
a--;
b--;
}
vector<vector<ll>> dists(n), ps(n);
for (ll i = 0; i < n; i++) {
auto[d, p] = get_dist(i, arr, n);
dists[i] = d;
ps[i] = p;
}
vector<vector<ll>> rarr(n);
for (ll i = 0; i < m; i++) {
rarr[get<1>(arr[i])].push_back(i);
}
vector<ll> w0n = get_way(n - 1, dists[0], ps[0], rarr, arr), wn0 = get_way(0, dists[n - 1], ps[n - 1], rarr, arr);
ll ans = dists[0][n - 1] + dists[n - 1][0];
vector<ll> d0r(m, -1), dnr(m, -1);
for (auto i : w0n) {
swap(get<0>(arr[i]), get<1>(arr[i]));
d0r[i] = get_dist(0, arr, n).first[n - 1];
swap(get<0>(arr[i]), get<1>(arr[i]));
}
for (ll i = 0; i < m; i++) {
if (d0r[i] == -1) {
d0r[i] = min(dists[0][n - 1], dists[0][get<1>(arr[i])] + get<2>(arr[i]) + dists[get<0>(arr[i])][n - 1]);
}
}
for (auto i : wn0) {
swap(get<0>(arr[i]), get<1>(arr[i]));
dnr[i] = get_dist(n - 1, arr, n).first[0];
swap(get<0>(arr[i]), get<1>(arr[i]));
}
for (ll i = 0; i < m; i++) {
if (dnr[i] == -1) {
dnr[i] = min(dists[n - 1][0], dists[n - 1][get<1>(arr[i])] + get<2>(arr[i]) + dists[get<0>(arr[i])][0]);
}
}
for (ll i = 0; i < m; i++) {
assign_min(ans, d0r[i] + dnr[i] + get<3>(arr[i]));
}
cout << (ans >= INF64 ? -1 : ans) << '\n';
}
int main() {
if (IS_FILE) {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
if (IS_TEST_CASES) {
cin >> t;
}
for (ll i = 0; i < t; i++) {
solve();
}
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:147:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
147 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
ho_t4.cpp:148:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | freopen("", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
1368 KB |
Output is correct |
2 |
Correct |
7 ms |
1260 KB |
Output is correct |
3 |
Correct |
19 ms |
1364 KB |
Output is correct |
4 |
Correct |
19 ms |
1324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
6 ms |
1236 KB |
Output is correct |
7 |
Correct |
0 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 |
52 ms |
1364 KB |
Output is correct |
11 |
Correct |
59 ms |
1384 KB |
Output is correct |
12 |
Correct |
58 ms |
1484 KB |
Output is correct |
13 |
Correct |
14 ms |
1252 KB |
Output is correct |
14 |
Correct |
15 ms |
1364 KB |
Output is correct |
15 |
Correct |
15 ms |
1368 KB |
Output is correct |
16 |
Correct |
15 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
5316 KB |
Output is correct |
2 |
Correct |
67 ms |
5260 KB |
Output is correct |
3 |
Correct |
68 ms |
5452 KB |
Output is correct |
4 |
Correct |
19 ms |
1356 KB |
Output is correct |
5 |
Correct |
15 ms |
1364 KB |
Output is correct |
6 |
Correct |
9 ms |
1236 KB |
Output is correct |
7 |
Correct |
5 ms |
1236 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
63 ms |
5096 KB |
Output is correct |
10 |
Correct |
62 ms |
5068 KB |
Output is correct |
11 |
Correct |
80 ms |
5468 KB |
Output is correct |
12 |
Correct |
67 ms |
5464 KB |
Output is correct |
13 |
Correct |
71 ms |
5424 KB |
Output is correct |
14 |
Correct |
67 ms |
5660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
1368 KB |
Output is correct |
2 |
Correct |
7 ms |
1236 KB |
Output is correct |
3 |
Correct |
71 ms |
4324 KB |
Output is correct |
4 |
Correct |
9 ms |
1236 KB |
Output is correct |
5 |
Correct |
87 ms |
5196 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
82 ms |
4952 KB |
Output is correct |
9 |
Correct |
81 ms |
4812 KB |
Output is correct |
10 |
Correct |
72 ms |
5192 KB |
Output is correct |
11 |
Correct |
77 ms |
5200 KB |
Output is correct |
12 |
Correct |
74 ms |
5196 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 |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
74 ms |
5180 KB |
Output is correct |
20 |
Correct |
73 ms |
5184 KB |
Output is correct |
21 |
Correct |
74 ms |
5200 KB |
Output is correct |
22 |
Correct |
74 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
1368 KB |
Output is correct |
2 |
Correct |
7 ms |
1260 KB |
Output is correct |
3 |
Correct |
19 ms |
1364 KB |
Output is correct |
4 |
Correct |
19 ms |
1324 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
6 ms |
1236 KB |
Output is correct |
7 |
Correct |
0 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 |
52 ms |
1364 KB |
Output is correct |
11 |
Correct |
59 ms |
1384 KB |
Output is correct |
12 |
Correct |
58 ms |
1484 KB |
Output is correct |
13 |
Correct |
14 ms |
1252 KB |
Output is correct |
14 |
Correct |
15 ms |
1364 KB |
Output is correct |
15 |
Correct |
15 ms |
1368 KB |
Output is correct |
16 |
Correct |
15 ms |
1356 KB |
Output is correct |
17 |
Correct |
66 ms |
5316 KB |
Output is correct |
18 |
Correct |
67 ms |
5260 KB |
Output is correct |
19 |
Correct |
68 ms |
5452 KB |
Output is correct |
20 |
Correct |
19 ms |
1356 KB |
Output is correct |
21 |
Correct |
15 ms |
1364 KB |
Output is correct |
22 |
Correct |
9 ms |
1236 KB |
Output is correct |
23 |
Correct |
5 ms |
1236 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
63 ms |
5096 KB |
Output is correct |
26 |
Correct |
62 ms |
5068 KB |
Output is correct |
27 |
Correct |
80 ms |
5468 KB |
Output is correct |
28 |
Correct |
67 ms |
5464 KB |
Output is correct |
29 |
Correct |
71 ms |
5424 KB |
Output is correct |
30 |
Correct |
67 ms |
5660 KB |
Output is correct |
31 |
Correct |
16 ms |
1368 KB |
Output is correct |
32 |
Correct |
7 ms |
1236 KB |
Output is correct |
33 |
Correct |
71 ms |
4324 KB |
Output is correct |
34 |
Correct |
9 ms |
1236 KB |
Output is correct |
35 |
Correct |
87 ms |
5196 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
82 ms |
4952 KB |
Output is correct |
39 |
Correct |
81 ms |
4812 KB |
Output is correct |
40 |
Correct |
72 ms |
5192 KB |
Output is correct |
41 |
Correct |
77 ms |
5200 KB |
Output is correct |
42 |
Correct |
74 ms |
5196 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 |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
212 KB |
Output is correct |
49 |
Correct |
74 ms |
5180 KB |
Output is correct |
50 |
Correct |
73 ms |
5184 KB |
Output is correct |
51 |
Correct |
74 ms |
5200 KB |
Output is correct |
52 |
Correct |
74 ms |
5212 KB |
Output is correct |
53 |
Correct |
98 ms |
5304 KB |
Output is correct |
54 |
Correct |
96 ms |
5288 KB |
Output is correct |
55 |
Correct |
93 ms |
5244 KB |
Output is correct |
56 |
Correct |
19 ms |
1364 KB |
Output is correct |
57 |
Correct |
18 ms |
1364 KB |
Output is correct |
58 |
Correct |
207 ms |
4912 KB |
Output is correct |
59 |
Correct |
238 ms |
4632 KB |
Output is correct |
60 |
Correct |
226 ms |
4504 KB |
Output is correct |
61 |
Correct |
189 ms |
4512 KB |
Output is correct |
62 |
Correct |
213 ms |
4512 KB |
Output is correct |
63 |
Correct |
216 ms |
4496 KB |
Output is correct |
64 |
Correct |
799 ms |
4952 KB |
Output is correct |
65 |
Correct |
734 ms |
5040 KB |
Output is correct |
66 |
Correct |
742 ms |
5028 KB |
Output is correct |
67 |
Correct |
10 ms |
3412 KB |
Output is correct |
68 |
Correct |
87 ms |
5124 KB |
Output is correct |
69 |
Correct |
87 ms |
5080 KB |
Output is correct |
70 |
Correct |
97 ms |
5428 KB |
Output is correct |
71 |
Correct |
96 ms |
5416 KB |
Output is correct |
72 |
Correct |
104 ms |
5464 KB |
Output is correct |
73 |
Correct |
96 ms |
5468 KB |
Output is correct |
74 |
Correct |
98 ms |
5460 KB |
Output is correct |