// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #include <x86intrin.h>
#include <bits/stdc++.h>
#include <chrono>
#include <random>
// @author: Vlapos
namespace operators
{
template <typename T1, typename T2>
std::istream &operator>>(std::istream &in, std::pair<T1, T2> &x)
{
in >> x.first >> x.second;
return in;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &out, std::pair<T1, T2> x)
{
out << x.first << " " << x.second;
return out;
}
template <typename T1>
std::istream &operator>>(std::istream &in, std::vector<T1> &x)
{
for (auto &i : x)
in >> i;
return in;
}
template <typename T1>
std::ostream &operator<<(std::ostream &out, std::vector<T1> &x)
{
for (auto &i : x)
out << i << " ";
return out;
}
template <typename T1>
std::ostream &operator<<(std::ostream &out, std::set<T1> &x)
{
for (auto &i : x)
out << i << " ";
return out;
}
}
// name spaces
using namespace std;
using namespace operators;
// end of name spaces
// defines
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define f first
#define s second
#define uint unsigned int
#define all(vc) vc.begin(), vc.end()
// end of defines
// usefull stuff
void boost()
{
ios_base ::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
inline int getbit(int &x, int &bt) { return (x >> bt) & 1; }
const int dx4[4] = {-1, 0, 0, 1};
const int dy4[4] = {0, -1, 1, 0};
const int dx8[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy8[8] = {-1, -0, 1, -1, 1, -1, 0, 1};
const ll INF = (1e15) + 500;
const int BIG = (2e9) + 500;
const int MOD7 = (1e9) + 7;
const int MOD9 = (1e9) + 9;
const uint MODFFT = 998244353;
// #define int ll
struct test
{
vector<vector<pll>> graph, invgraph;
vector<int> components;
int n, m;
void merge(pll &a, ll val)
{
if (a.f > val)
{
a.s = a.f;
a.f = val;
}
else
a.s = min(a.s, val);
}
vector<int> used;
void dijkstra(vector<ll> &dists, int st, int U, int V, ll C)
{
for (int i = 0; i < n; ++i)
dists[i] = INF, used[i] = 0;
dists[st] = 0;
for (int i = 0; i < n; ++i)
{
int v = -1;
for (int i = 0; i < n; ++i)
if (!used[i] and (v == -1 or dists[v] > dists[i]))
v = i;
if (v == -1)
break;
used[v] = 1;
for (int i = 0; i < n; ++i)
if (!used[i])
{
ll cost;
if (U == v and V == i and graph[v][i].f == C)
cost = graph[v][i].s;
else
cost = graph[v][i].f;
if (U == i and V == v)
cost = min(cost, C);
dists[i] = min(dists[i], dists[v] + cost);
}
}
}
void invdijkstra(vector<ll> &dists, int st, int U, int V, ll C)
{
for (int i = 0; i < n; ++i)
dists[i] = INF, used[i] = 0;
dists[st] = 0;
for (int i = 0; i < n; ++i)
{
int v = -1;
for (int i = 0; i < n; ++i)
if (!used[i] and (v == -1 or dists[v] > dists[i]))
v = i;
if (v == -1)
break;
used[v] = 1;
for (int i = 0; i < n; ++i)
if (!used[i])
{
ll cost = (U == v and V == i and invgraph[v][i].f == C ? invgraph[v][i].s : invgraph[v][i].f);
dists[i] = min(dists[i], dists[v] + cost);
}
}
}
vector<pii> E;
vector<int> WAS;
bool dfs(int v, int fn, vector<ll> &dist)
{
WAS[v] = 1;
if (v == fn)
return true;
for (int tov = 0; tov < n; ++tov)
if (!WAS[tov] and dist[v] == dist[tov] + graph[v][tov].f and dfs(tov, fn, dist))
{
E.pb({v, tov});
return true;
}
return false;
}
void solve(int testcase)
{
boost();
cin >> n >> m;
WAS.resize(n);
used.resize(n);
graph.resize(n, vector<pll>(n, {INF, INF}));
invgraph.resize(n, vector<pll>(n, {INF, INF}));
vector<pair<pii, pii>> edges;
for (int i = 0; i < m; ++i)
{
int v, tov, c, d;
cin >> v >> tov >> c >> d;
--v, --tov;
merge(graph[v][tov], c);
merge(invgraph[tov][v], c);
edges.pb({{d, c}, {v, tov}});
}
sort(all(edges));
vector<ll> dist0(n, INF), distN(n, INF), to0(n, INF), toN(n, INF);
dijkstra(dist0, 0, 0, 0, 0);
dijkstra(distN, n - 1, 0, 0, 0);
invdijkstra(to0, 0, 0, 0, 0);
invdijkstra(toN, n - 1, 0, 0, 0);
vector<ll> bf(n);
ll res = INF;
if (dist0[n - 1] == INF and distN[n - 1] == INF)
{
for (auto [e1, e2] : edges)
{
auto [v, tov] = e2;
auto [d, c] = e1;
res = min(res, d + dist0[tov] + c + toN[v] + distN[tov] + c + to0[v]);
}
}
else
{
map<pair<pii, ll>, ll> bad;
int CC = 0;
if (dist0[n - 1] != INF)
{
WAS.assign(n, 0);
E.clear();
assert(dfs(0, n - 1, toN));
// reverse(all(E));
// cout << E << " " << dist0[n - 1] << "!!\n";
for (auto [v, tov] : E)
{
CC++;
// if (CC > 2 * n + 10)
// exit(0);
ll curRes = 0;
dijkstra(bf, 0, v, tov, graph[v][tov].f);
curRes += bf[n - 1];
dijkstra(bf, n - 1, v, tov, graph[v][tov].f);
curRes += bf[0];
bad[{{v, tov}, graph[v][tov].f}] = curRes;
}
}
if (distN[0] != INF)
{
WAS.assign(n, 0);
E.clear();
assert(dfs(n - 1, 0, to0));
// reverse(all(E));
// cout << E << " " << distN[0] << "!!\n";
for (auto [v, tov] : E)
{
// if (CC > 2 * n + 10)
// exit(0);
ll curRes = 0;
dijkstra(bf, 0, v, tov, graph[v][tov].f);
curRes += bf[n - 1];
dijkstra(bf, n - 1, v, tov, graph[v][tov].f);
curRes += bf[0];
bad[{{v, tov}, graph[v][tov].f}] = curRes;
}
}
ll way0N = dist0[n - 1];
ll wayN0 = distN[0];
res = min(res, way0N + wayN0);
for (auto [e1, e2] : edges)
{
auto [v, tov] = e2;
auto [d, c] = e1;
if (bad.find({{v, tov}, c}) != bad.end())
res = min(res, d + bad[{{v, tov}, c}]);
else
res = min(res, d + min(way0N, dist0[tov] + c + toN[v]) + min(wayN0, distN[tov] + c + to0[v]));
}
}
if (res == INF)
cout << "-1\n";
else
cout << res << "\n";
}
};
main()
{
boost();
int q = 1;
// cin >> q;
for (int i = 0; i < q; i++)
{
test t;
t.solve(i);
}
return 0;
}
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//
// //
// Coded by Der_Vlἀpos //
// //
//[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]//
Compilation message
ho_t4.cpp:297:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
297 | main()
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
1624 KB |
Output is correct |
2 |
Correct |
2 ms |
1628 KB |
Output is correct |
3 |
Correct |
5 ms |
1628 KB |
Output is correct |
4 |
Correct |
6 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
114 ms |
2024 KB |
Output is correct |
11 |
Correct |
111 ms |
1624 KB |
Output is correct |
12 |
Correct |
115 ms |
2024 KB |
Output is correct |
13 |
Correct |
2 ms |
1628 KB |
Output is correct |
14 |
Correct |
5 ms |
1780 KB |
Output is correct |
15 |
Correct |
4 ms |
1628 KB |
Output is correct |
16 |
Correct |
5 ms |
1784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
3540 KB |
Output is correct |
2 |
Correct |
22 ms |
3688 KB |
Output is correct |
3 |
Correct |
20 ms |
3792 KB |
Output is correct |
4 |
Correct |
4 ms |
1624 KB |
Output is correct |
5 |
Correct |
6 ms |
1748 KB |
Output is correct |
6 |
Correct |
2 ms |
1628 KB |
Output is correct |
7 |
Correct |
2 ms |
1628 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
18 ms |
3796 KB |
Output is correct |
10 |
Correct |
21 ms |
3796 KB |
Output is correct |
11 |
Correct |
23 ms |
3668 KB |
Output is correct |
12 |
Correct |
22 ms |
3660 KB |
Output is correct |
13 |
Correct |
20 ms |
3792 KB |
Output is correct |
14 |
Correct |
22 ms |
3796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
1624 KB |
Output is correct |
2 |
Correct |
7 ms |
1628 KB |
Output is correct |
3 |
Correct |
56 ms |
3540 KB |
Output is correct |
4 |
Correct |
1 ms |
1628 KB |
Output is correct |
5 |
Correct |
64 ms |
3540 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
17 ms |
3540 KB |
Output is correct |
9 |
Correct |
19 ms |
3536 KB |
Output is correct |
10 |
Correct |
62 ms |
3540 KB |
Output is correct |
11 |
Correct |
25 ms |
3540 KB |
Output is correct |
12 |
Correct |
22 ms |
3540 KB |
Output is correct |
13 |
Correct |
0 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 |
392 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
57 ms |
3616 KB |
Output is correct |
20 |
Correct |
36 ms |
3540 KB |
Output is correct |
21 |
Correct |
22 ms |
3576 KB |
Output is correct |
22 |
Correct |
31 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
1624 KB |
Output is correct |
2 |
Correct |
2 ms |
1628 KB |
Output is correct |
3 |
Correct |
5 ms |
1628 KB |
Output is correct |
4 |
Correct |
6 ms |
1628 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
1628 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
114 ms |
2024 KB |
Output is correct |
11 |
Correct |
111 ms |
1624 KB |
Output is correct |
12 |
Correct |
115 ms |
2024 KB |
Output is correct |
13 |
Correct |
2 ms |
1628 KB |
Output is correct |
14 |
Correct |
5 ms |
1780 KB |
Output is correct |
15 |
Correct |
4 ms |
1628 KB |
Output is correct |
16 |
Correct |
5 ms |
1784 KB |
Output is correct |
17 |
Correct |
20 ms |
3540 KB |
Output is correct |
18 |
Correct |
22 ms |
3688 KB |
Output is correct |
19 |
Correct |
20 ms |
3792 KB |
Output is correct |
20 |
Correct |
4 ms |
1624 KB |
Output is correct |
21 |
Correct |
6 ms |
1748 KB |
Output is correct |
22 |
Correct |
2 ms |
1628 KB |
Output is correct |
23 |
Correct |
2 ms |
1628 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
18 ms |
3796 KB |
Output is correct |
26 |
Correct |
21 ms |
3796 KB |
Output is correct |
27 |
Correct |
23 ms |
3668 KB |
Output is correct |
28 |
Correct |
22 ms |
3660 KB |
Output is correct |
29 |
Correct |
20 ms |
3792 KB |
Output is correct |
30 |
Correct |
22 ms |
3796 KB |
Output is correct |
31 |
Correct |
31 ms |
1624 KB |
Output is correct |
32 |
Correct |
7 ms |
1628 KB |
Output is correct |
33 |
Correct |
56 ms |
3540 KB |
Output is correct |
34 |
Correct |
1 ms |
1628 KB |
Output is correct |
35 |
Correct |
64 ms |
3540 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
452 KB |
Output is correct |
38 |
Correct |
17 ms |
3540 KB |
Output is correct |
39 |
Correct |
19 ms |
3536 KB |
Output is correct |
40 |
Correct |
62 ms |
3540 KB |
Output is correct |
41 |
Correct |
25 ms |
3540 KB |
Output is correct |
42 |
Correct |
22 ms |
3540 KB |
Output is correct |
43 |
Correct |
0 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 |
392 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
57 ms |
3616 KB |
Output is correct |
50 |
Correct |
36 ms |
3540 KB |
Output is correct |
51 |
Correct |
22 ms |
3576 KB |
Output is correct |
52 |
Correct |
31 ms |
3540 KB |
Output is correct |
53 |
Correct |
20 ms |
3540 KB |
Output is correct |
54 |
Correct |
22 ms |
3540 KB |
Output is correct |
55 |
Correct |
24 ms |
3540 KB |
Output is correct |
56 |
Correct |
5 ms |
1628 KB |
Output is correct |
57 |
Correct |
3 ms |
1628 KB |
Output is correct |
58 |
Correct |
126 ms |
3488 KB |
Output is correct |
59 |
Correct |
129 ms |
3536 KB |
Output is correct |
60 |
Correct |
125 ms |
3540 KB |
Output is correct |
61 |
Correct |
105 ms |
3540 KB |
Output is correct |
62 |
Correct |
134 ms |
3536 KB |
Output is correct |
63 |
Correct |
129 ms |
3540 KB |
Output is correct |
64 |
Correct |
146 ms |
3540 KB |
Output is correct |
65 |
Correct |
129 ms |
3536 KB |
Output is correct |
66 |
Correct |
123 ms |
3540 KB |
Output is correct |
67 |
Correct |
13 ms |
2336 KB |
Output is correct |
68 |
Correct |
19 ms |
3792 KB |
Output is correct |
69 |
Correct |
20 ms |
3792 KB |
Output is correct |
70 |
Correct |
21 ms |
3796 KB |
Output is correct |
71 |
Correct |
21 ms |
3796 KB |
Output is correct |
72 |
Correct |
23 ms |
4040 KB |
Output is correct |
73 |
Correct |
20 ms |
3796 KB |
Output is correct |
74 |
Correct |
22 ms |
3692 KB |
Output is correct |