/*
If you're looking at my code for solution,
your brute-force code with a bit optimization is the solution.
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int E = 5e4 + 4;
const int mn = 202;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int direct[E], a[E], b[E], cost[E], revCost[E], n;
ll distanc[mn][mn], dist[mn];
vector<tt> adj[mn];
bool vis[mn];
ll dijkstra (int s, int t) {
for (int i = 1; i <= n; i++) dist[i] = LLONG_MAX, vis[i] = 0;
dist[s] = 0;
priority_queue<pl> pq; pq.emplace(0, s);
while (pq.size()) {
int u = pq.top().second; pq.pop();
if (vis[u]) continue;
if (u == t) return dist[t];
vis[u] = 1;
for (tt it : adj[u]) {
int v, id, way; tie(v, id, way) = it;
if (way != direct[id]) continue;
if (dist[u] + cost[id] < dist[v]) {
dist[v] = dist[u] + cost[id];
pq.emplace(-dist[v], v);
}
}
}
return LLONG_MAX;
}
ll add (ll a, ll b) {
return (max(a, b) == LLONG_MAX ? LLONG_MAX : a + b);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int m; cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
distanc[i][j] = (i == j ? 0 : LLONG_MAX);
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> cost[i] >> revCost[i];
adj[a[i]].emplace_back(b[i], i, 0);
adj[b[i]].emplace_back(a[i], i, 1);
distanc[a[i]][b[i]] = min(distanc[a[i]][b[i]], (ll)cost[i]);
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
distanc[i][j] = min(distanc[i][j], add(distanc[i][k], distanc[k][j]));
vector<int> cand(m);
for (int i = 0; i < m; i++) cand[i] = i;
shuffle(all(cand), rng);
ll ans = add(distanc[1][n], distanc[n][1]);
for (int i : cand) {
ll tryOTN = min(distanc[1][n], add(add(distanc[1][b[i]], cost[i]), distanc[a[i]][n]));
ll tryNTO = min(distanc[n][1], add(add(distanc[n][b[i]], cost[i]), distanc[a[i]][1]));
if (add(add(tryOTN, tryNTO), revCost[i]) < ans) { // only optimize when possible
direct[i] = 1;
ll dist = add(dijkstra(1, n), dijkstra(n, 1));
ans = min(ans, add(dist, revCost[i]));
direct[i] = 0;
}
}
cout << (ans == LLONG_MAX ? -1 : ans);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
860 KB |
Output is correct |
2 |
Correct |
9 ms |
828 KB |
Output is correct |
3 |
Correct |
11 ms |
860 KB |
Output is correct |
4 |
Correct |
9 ms |
900 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
9 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
12 ms |
860 KB |
Output is correct |
11 |
Correct |
17 ms |
912 KB |
Output is correct |
12 |
Correct |
10 ms |
860 KB |
Output is correct |
13 |
Correct |
10 ms |
860 KB |
Output is correct |
14 |
Correct |
9 ms |
860 KB |
Output is correct |
15 |
Correct |
9 ms |
860 KB |
Output is correct |
16 |
Correct |
9 ms |
900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
4432 KB |
Output is correct |
2 |
Correct |
22 ms |
4444 KB |
Output is correct |
3 |
Correct |
23 ms |
4708 KB |
Output is correct |
4 |
Correct |
10 ms |
856 KB |
Output is correct |
5 |
Correct |
9 ms |
744 KB |
Output is correct |
6 |
Correct |
9 ms |
600 KB |
Output is correct |
7 |
Correct |
9 ms |
832 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
26 ms |
4532 KB |
Output is correct |
10 |
Correct |
27 ms |
4764 KB |
Output is correct |
11 |
Correct |
23 ms |
4700 KB |
Output is correct |
12 |
Correct |
22 ms |
4836 KB |
Output is correct |
13 |
Correct |
23 ms |
4944 KB |
Output is correct |
14 |
Correct |
26 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
860 KB |
Output is correct |
2 |
Correct |
9 ms |
828 KB |
Output is correct |
3 |
Correct |
19 ms |
3672 KB |
Output is correct |
4 |
Correct |
9 ms |
604 KB |
Output is correct |
5 |
Correct |
22 ms |
4428 KB |
Output is correct |
6 |
Correct |
0 ms |
480 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
28 ms |
4436 KB |
Output is correct |
9 |
Correct |
28 ms |
4444 KB |
Output is correct |
10 |
Correct |
23 ms |
4444 KB |
Output is correct |
11 |
Correct |
22 ms |
4448 KB |
Output is correct |
12 |
Correct |
24 ms |
4696 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
21 ms |
4700 KB |
Output is correct |
20 |
Correct |
22 ms |
4436 KB |
Output is correct |
21 |
Correct |
25 ms |
4436 KB |
Output is correct |
22 |
Correct |
23 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
860 KB |
Output is correct |
2 |
Correct |
9 ms |
828 KB |
Output is correct |
3 |
Correct |
11 ms |
860 KB |
Output is correct |
4 |
Correct |
9 ms |
900 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
9 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
12 ms |
860 KB |
Output is correct |
11 |
Correct |
17 ms |
912 KB |
Output is correct |
12 |
Correct |
10 ms |
860 KB |
Output is correct |
13 |
Correct |
10 ms |
860 KB |
Output is correct |
14 |
Correct |
9 ms |
860 KB |
Output is correct |
15 |
Correct |
9 ms |
860 KB |
Output is correct |
16 |
Correct |
9 ms |
900 KB |
Output is correct |
17 |
Correct |
21 ms |
4432 KB |
Output is correct |
18 |
Correct |
22 ms |
4444 KB |
Output is correct |
19 |
Correct |
23 ms |
4708 KB |
Output is correct |
20 |
Correct |
10 ms |
856 KB |
Output is correct |
21 |
Correct |
9 ms |
744 KB |
Output is correct |
22 |
Correct |
9 ms |
600 KB |
Output is correct |
23 |
Correct |
9 ms |
832 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
26 ms |
4532 KB |
Output is correct |
26 |
Correct |
27 ms |
4764 KB |
Output is correct |
27 |
Correct |
23 ms |
4700 KB |
Output is correct |
28 |
Correct |
22 ms |
4836 KB |
Output is correct |
29 |
Correct |
23 ms |
4944 KB |
Output is correct |
30 |
Correct |
26 ms |
4956 KB |
Output is correct |
31 |
Correct |
10 ms |
860 KB |
Output is correct |
32 |
Correct |
9 ms |
828 KB |
Output is correct |
33 |
Correct |
19 ms |
3672 KB |
Output is correct |
34 |
Correct |
9 ms |
604 KB |
Output is correct |
35 |
Correct |
22 ms |
4428 KB |
Output is correct |
36 |
Correct |
0 ms |
480 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
28 ms |
4436 KB |
Output is correct |
39 |
Correct |
28 ms |
4444 KB |
Output is correct |
40 |
Correct |
23 ms |
4444 KB |
Output is correct |
41 |
Correct |
22 ms |
4448 KB |
Output is correct |
42 |
Correct |
24 ms |
4696 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 |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
21 ms |
4700 KB |
Output is correct |
50 |
Correct |
22 ms |
4436 KB |
Output is correct |
51 |
Correct |
25 ms |
4436 KB |
Output is correct |
52 |
Correct |
23 ms |
4444 KB |
Output is correct |
53 |
Correct |
21 ms |
4268 KB |
Output is correct |
54 |
Correct |
23 ms |
4324 KB |
Output is correct |
55 |
Correct |
29 ms |
4256 KB |
Output is correct |
56 |
Correct |
9 ms |
856 KB |
Output is correct |
57 |
Correct |
10 ms |
664 KB |
Output is correct |
58 |
Correct |
25 ms |
3628 KB |
Output is correct |
59 |
Correct |
29 ms |
3672 KB |
Output is correct |
60 |
Correct |
30 ms |
3668 KB |
Output is correct |
61 |
Correct |
20 ms |
3676 KB |
Output is correct |
62 |
Correct |
106 ms |
3772 KB |
Output is correct |
63 |
Correct |
231 ms |
3848 KB |
Output is correct |
64 |
Correct |
64 ms |
4224 KB |
Output is correct |
65 |
Correct |
180 ms |
4348 KB |
Output is correct |
66 |
Correct |
333 ms |
4300 KB |
Output is correct |
67 |
Correct |
10 ms |
3020 KB |
Output is correct |
68 |
Correct |
32 ms |
4692 KB |
Output is correct |
69 |
Correct |
33 ms |
4824 KB |
Output is correct |
70 |
Correct |
23 ms |
4424 KB |
Output is correct |
71 |
Correct |
23 ms |
4444 KB |
Output is correct |
72 |
Correct |
27 ms |
4576 KB |
Output is correct |
73 |
Correct |
24 ms |
4752 KB |
Output is correct |
74 |
Correct |
23 ms |
4688 KB |
Output is correct |