#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
#define int ll
const int N = 256;
const int M = 50010;
vector<pii> A[N], T[N];
const int inf = 3e9+1000;
int w[M], p[M];
int dis0[N], disn[N], dis0T[N], disnT[N], disdard[N];
pii par[N];
pii edge[M];
bool is_path[M];
int n, m;
void dij(int s, int dis[], vector<pii> A[])
{
fill(dis, dis+n, inf);
fill(par, par+n, pii{-1, -1});
dis[s] = 0;
set<pii> st = {{0, s}};
while (st.size()) {
auto [d, v] = *st.begin();
st.erase(st.begin());
for (auto [u, e] : A[v]) {
int w = ::w[e];
if (d + w < dis[u]) {
st.erase({dis[u], u});
dis[u] = d+w;
par[u] = {v, e};
st.insert({dis[u], u});
}
}
}
}
void input()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n >> m;
Loop (e,0,m) {
int v, u;
cin >> v >> u >> w[e] >> p[e];
--v; --u;
A[v].push_back({u,e});
T[u].push_back({v,e});
edge[e] = {v, u};
}
Loop (i,0,n) {
sort(A[i].begin(), A[i].end(), [](pii x, pii y){
return w[x.second] < w[y.second];
});
}
}
signed main()
{
input();
int ans = 0;
dij(0, dis0, A);
dij(0, dis0T, T);
ans += dis0[n-1];
int v = n-1;
for (;;) {
auto [u, e] = par[v];
if (u == -1)
break;
is_path[e] = 1;
v = u;
}
dij(n-1, disn, A);
dij(n-1, disnT, T);
ans += disn[0];
v = 0;
for (;;) {
auto [u, e] = par[v];
if (u == -1)
break;
is_path[e] = 1;
v = u;
}
Loop (e,0,m) {
auto [v, u] = edge[e];
if (is_path[e]) {
int tmp = inf;
swap(tmp, w[e]);
w[m] = tmp;
A[u].push_back({v,m});
int x = p[e];
dij(0, disdard, A);
x += disdard[n-1];
dij(n-1, disdard, A);
x += disdard[0];
ans = min(ans, x);
A[u].pop_back();
w[e] = tmp;
} else {
int x = dis0[u] + w[e] + disnT[v] + disn[0];
int y = dis0[n-1] + disn[u] + w[e] + dis0T[v];
int z = dis0[u] + w[e] + disnT[v]
+ disn[u] + w[e] + dis0T[v];
ans = min(ans, p[e] + min({x, y, z}));
}
}
cout << (ans >= inf? -1: ans) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
27 ms |
408 KB |
Output is correct |
11 |
Correct |
35 ms |
424 KB |
Output is correct |
12 |
Correct |
32 ms |
340 KB |
Output is correct |
13 |
Correct |
1 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 |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
2772 KB |
Output is correct |
2 |
Correct |
30 ms |
2900 KB |
Output is correct |
3 |
Correct |
26 ms |
2828 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
21 ms |
2760 KB |
Output is correct |
10 |
Correct |
24 ms |
2772 KB |
Output is correct |
11 |
Correct |
28 ms |
2912 KB |
Output is correct |
12 |
Correct |
26 ms |
2748 KB |
Output is correct |
13 |
Correct |
24 ms |
2856 KB |
Output is correct |
14 |
Correct |
23 ms |
3016 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 |
17 ms |
2192 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
20 ms |
2836 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
18 ms |
2776 KB |
Output is correct |
9 |
Correct |
17 ms |
2780 KB |
Output is correct |
10 |
Correct |
20 ms |
2772 KB |
Output is correct |
11 |
Correct |
19 ms |
2764 KB |
Output is correct |
12 |
Correct |
18 ms |
2832 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
360 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
19 ms |
2820 KB |
Output is correct |
20 |
Correct |
20 ms |
2844 KB |
Output is correct |
21 |
Correct |
20 ms |
2736 KB |
Output is correct |
22 |
Correct |
19 ms |
2788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 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 |
3 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
27 ms |
408 KB |
Output is correct |
11 |
Correct |
35 ms |
424 KB |
Output is correct |
12 |
Correct |
32 ms |
340 KB |
Output is correct |
13 |
Correct |
1 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 |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
24 ms |
2772 KB |
Output is correct |
18 |
Correct |
30 ms |
2900 KB |
Output is correct |
19 |
Correct |
26 ms |
2828 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
21 ms |
2760 KB |
Output is correct |
26 |
Correct |
24 ms |
2772 KB |
Output is correct |
27 |
Correct |
28 ms |
2912 KB |
Output is correct |
28 |
Correct |
26 ms |
2748 KB |
Output is correct |
29 |
Correct |
24 ms |
2856 KB |
Output is correct |
30 |
Correct |
23 ms |
3016 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
17 ms |
2192 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
20 ms |
2836 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
18 ms |
2776 KB |
Output is correct |
39 |
Correct |
17 ms |
2780 KB |
Output is correct |
40 |
Correct |
20 ms |
2772 KB |
Output is correct |
41 |
Correct |
19 ms |
2764 KB |
Output is correct |
42 |
Correct |
18 ms |
2832 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
360 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
19 ms |
2820 KB |
Output is correct |
50 |
Correct |
20 ms |
2844 KB |
Output is correct |
51 |
Correct |
20 ms |
2736 KB |
Output is correct |
52 |
Correct |
19 ms |
2788 KB |
Output is correct |
53 |
Correct |
28 ms |
2724 KB |
Output is correct |
54 |
Correct |
27 ms |
2868 KB |
Output is correct |
55 |
Correct |
33 ms |
2796 KB |
Output is correct |
56 |
Correct |
2 ms |
340 KB |
Output is correct |
57 |
Correct |
2 ms |
340 KB |
Output is correct |
58 |
Correct |
157 ms |
2196 KB |
Output is correct |
59 |
Correct |
152 ms |
2260 KB |
Output is correct |
60 |
Correct |
196 ms |
2208 KB |
Output is correct |
61 |
Correct |
115 ms |
2260 KB |
Output is correct |
62 |
Correct |
148 ms |
2264 KB |
Output is correct |
63 |
Correct |
200 ms |
2260 KB |
Output is correct |
64 |
Correct |
688 ms |
2388 KB |
Output is correct |
65 |
Correct |
699 ms |
2440 KB |
Output is correct |
66 |
Correct |
943 ms |
2416 KB |
Output is correct |
67 |
Correct |
18 ms |
2388 KB |
Output is correct |
68 |
Correct |
22 ms |
3028 KB |
Output is correct |
69 |
Correct |
24 ms |
3064 KB |
Output is correct |
70 |
Correct |
27 ms |
3112 KB |
Output is correct |
71 |
Correct |
25 ms |
3020 KB |
Output is correct |
72 |
Correct |
25 ms |
2980 KB |
Output is correct |
73 |
Correct |
24 ms |
3156 KB |
Output is correct |
74 |
Correct |
26 ms |
3068 KB |
Output is correct |