#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
ll N, M, w[50005], d[50005], ans = 1e18;
pll edge[50005];
bool recalcto[50005], recalcback[50005];
vector<ll> to, torev, back, backrev, recalctodis, recalcbackdis;
vector<int> tofrom, backfrom, tmp;
vector<int> E[205], rE[205];
pll get_edge(int id, int u)
{
if (edge[id].F == u)
return pll{edge[id].S, w[id]};
else
return pll{edge[id].F, w[id]};
}
void dijkstra(int begin, int N, vector<ll> &dis, vector<int> &from, vector<int> E[])
{
dis = vector<ll>(N + 1, 1e18);
dis[begin] = 0;
from.resize(N + 1);
priority_queue<pll, vector<pll>, greater<pll>> pq;
pq.push({0, begin});
while (!pq.empty())
{
auto [d, u] = pq.top();
pq.pop();
if (d > dis[u])
continue;
for (auto id : E[u])
{
auto [v, w] = get_edge(id, u);
if (dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
pq.push({dis[v], v});
from[v] = id;
}
}
}
}
signed main()
{
cin >> N >> M;
for (int i = 1; i <= M; i++)
{
ll U, V, C, D;
cin >> U >> V >> C >> D;
edge[i] = {U, V};
w[i] = C;
d[i] = D;
E[U].emplace_back(i);
rE[V].emplace_back(i);
}
dijkstra(N, N, torev, tmp, rE);
dijkstra(1, N, to, tofrom, E);
dijkstra(1, N, backrev, tmp, rE);
dijkstra(N, N, back, backfrom, E);
//for (int i = 1; i <= N; i++)
// cout << to[i] << " \n"[i == N];
//for (int i = 1; i <= N; i++)
// cout << back[i] << " \n"[i == N];
if (to[N] < 1e18)
{
int cur = N;
while (cur != 1)
{
int id = tofrom[cur];
recalcto[id] = 1;
cur = get_edge(id, cur).F;
}
}
if (back[1] < 1e18)
{
int cur = 1;
while (cur != N)
{
int id = backfrom[cur];
recalcback[id] = 1;
cur = get_edge(id, cur).F;
}
}
ans = to[N] + back[1];
//cerr << ans << '\n';
for (int i = 1; i <= M; i++)
{
//cerr << i << " = ";
ll res = d[i];
if (recalcto[i])
{
//cout << " recalcto ";
tie(edge[M + 1], w[M + 1]) = tie(edge[i], w[i]);
w[i] = 1e18;
E[edge[M + 1].S].emplace_back(M + 1);
dijkstra(1, N, recalctodis, tmp, E);
E[edge[M + 1].S].pop_back();
tie(edge[i], w[i]) = tie(edge[M + 1], w[M + 1]);
res += recalctodis[N];
}
else
res += min(to[N], to[edge[i].S] + w[i] + torev[edge[i].F]);
//cerr << res << " ";
if (recalcback[i])
{
//cout << " recalcback ";
tie(edge[M + 1], w[M + 1]) = tie(edge[i], w[i]);
w[i] = 1e18;
E[edge[M + 1].S].emplace_back(M + 1);
dijkstra(N, N, recalcbackdis, tmp, E);
E[edge[M + 1].S].pop_back();
tie(edge[i], w[i]) = tie(edge[M + 1], w[M + 1]);
res += recalcbackdis[1];
}
else
res += min(back[1], back[edge[i].S] + w[i] + backrev[edge[i].F]);
//cerr << res << "\n";
ans = min(ans, res);
}
cout << (ans >= 1e18 ? -1 : ans) << '\n';
}
Compilation message
ho_t4.cpp: In function 'void dijkstra(int, int, std::vector<long long int>&, std::vector<int>&, std::vector<int>*)':
ho_t4.cpp:33:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
33 | auto [d, u] = pq.top();
| ^
ho_t4.cpp:39:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | auto [v, w] = get_edge(id, u);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
18 ms |
428 KB |
Output is correct |
11 |
Correct |
16 ms |
432 KB |
Output is correct |
12 |
Correct |
16 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
3504 KB |
Output is correct |
2 |
Correct |
54 ms |
3528 KB |
Output is correct |
3 |
Correct |
60 ms |
3688 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
316 KB |
Output is correct |
9 |
Correct |
53 ms |
3688 KB |
Output is correct |
10 |
Correct |
54 ms |
3668 KB |
Output is correct |
11 |
Correct |
58 ms |
3748 KB |
Output is correct |
12 |
Correct |
59 ms |
3680 KB |
Output is correct |
13 |
Correct |
55 ms |
3720 KB |
Output is correct |
14 |
Correct |
55 ms |
3804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
40 ms |
2752 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
49 ms |
3488 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
46 ms |
3372 KB |
Output is correct |
9 |
Correct |
46 ms |
3484 KB |
Output is correct |
10 |
Correct |
51 ms |
3436 KB |
Output is correct |
11 |
Correct |
51 ms |
3404 KB |
Output is correct |
12 |
Correct |
47 ms |
3464 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
46 ms |
3448 KB |
Output is correct |
20 |
Correct |
53 ms |
3392 KB |
Output is correct |
21 |
Correct |
59 ms |
3372 KB |
Output is correct |
22 |
Correct |
47 ms |
3404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
312 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
2 ms |
340 KB |
Output is correct |
10 |
Correct |
18 ms |
428 KB |
Output is correct |
11 |
Correct |
16 ms |
432 KB |
Output is correct |
12 |
Correct |
16 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
52 ms |
3504 KB |
Output is correct |
18 |
Correct |
54 ms |
3528 KB |
Output is correct |
19 |
Correct |
60 ms |
3688 KB |
Output is correct |
20 |
Correct |
3 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
316 KB |
Output is correct |
25 |
Correct |
53 ms |
3688 KB |
Output is correct |
26 |
Correct |
54 ms |
3668 KB |
Output is correct |
27 |
Correct |
58 ms |
3748 KB |
Output is correct |
28 |
Correct |
59 ms |
3680 KB |
Output is correct |
29 |
Correct |
55 ms |
3720 KB |
Output is correct |
30 |
Correct |
55 ms |
3804 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
316 KB |
Output is correct |
33 |
Correct |
40 ms |
2752 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
49 ms |
3488 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
46 ms |
3372 KB |
Output is correct |
39 |
Correct |
46 ms |
3484 KB |
Output is correct |
40 |
Correct |
51 ms |
3436 KB |
Output is correct |
41 |
Correct |
51 ms |
3404 KB |
Output is correct |
42 |
Correct |
47 ms |
3464 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
46 ms |
3448 KB |
Output is correct |
50 |
Correct |
53 ms |
3392 KB |
Output is correct |
51 |
Correct |
59 ms |
3372 KB |
Output is correct |
52 |
Correct |
47 ms |
3404 KB |
Output is correct |
53 |
Correct |
56 ms |
3596 KB |
Output is correct |
54 |
Correct |
68 ms |
3544 KB |
Output is correct |
55 |
Correct |
51 ms |
3544 KB |
Output is correct |
56 |
Correct |
2 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
380 KB |
Output is correct |
58 |
Correct |
131 ms |
2864 KB |
Output is correct |
59 |
Correct |
137 ms |
3080 KB |
Output is correct |
60 |
Correct |
143 ms |
2964 KB |
Output is correct |
61 |
Correct |
109 ms |
2976 KB |
Output is correct |
62 |
Correct |
134 ms |
2936 KB |
Output is correct |
63 |
Correct |
145 ms |
2872 KB |
Output is correct |
64 |
Correct |
719 ms |
3564 KB |
Output is correct |
65 |
Correct |
662 ms |
3568 KB |
Output is correct |
66 |
Correct |
618 ms |
3676 KB |
Output is correct |
67 |
Correct |
41 ms |
2888 KB |
Output is correct |
68 |
Correct |
63 ms |
3656 KB |
Output is correct |
69 |
Correct |
55 ms |
3616 KB |
Output is correct |
70 |
Correct |
57 ms |
3708 KB |
Output is correct |
71 |
Correct |
67 ms |
3712 KB |
Output is correct |
72 |
Correct |
57 ms |
3644 KB |
Output is correct |
73 |
Correct |
56 ms |
3696 KB |
Output is correct |
74 |
Correct |
56 ms |
3660 KB |
Output is correct |