#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1000;
const int M = 100000;
const ll INF = 1e18;
int n, m;
vector<pair<int, int>> g[2][N];
ll C[M], D[M];
pair<int, int> edges[M];
vector<vector<ll>> dist, par;
vector<ll> dijkstra(int s, int b, bool save = 0) {
vector<ll> d(n + 1, INF);
vector<ll> p(n + 1, -1);
d[s] = 0;
bool processed[n + 1] = {};
while(1) {
s = -1;
for(int i = 1; i <= n; i++) {
if(processed[i]) continue;
if(s == -1 || d[s] > d[i])
s = i;
}
if(s == -1) break;
processed[s] = 1;
for(auto [to, i] : g[b][s]) {
if(d[to] > d[s] + C[i]) {
p[to] = i;
d[to] = d[s] + C[i];
}
}
}
if(save) {
dist.push_back(d);
par.push_back(p);
}
return d;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
edges[i] = {u, v};
cin >> C[i] >> D[i];
g[0][u].push_back({v, i});
g[1][v].push_back({u, i});
}
dijkstra(1, 0, 1);
dijkstra(n, 0, 1);
dijkstra(1, 1, 1);
dijkstra(n, 1, 1);
int cur = n;
set<int> way1, way2;
while(par[0][cur] != -1) {
way1.insert(par[0][cur]);
cur = edges[par[0][cur]].first;
}
if(cur != 1) way1.clear();
cur = 1;
while(par[1][cur] != -1) {
way2.insert(par[1][cur]);
cur = edges[par[1][cur]].first;
}
if(cur != n) way2.clear();
ll res = dist[0][n] + dist[1][1];
for(int i = 0; i < m; i++) {
ll ans = INF;
auto [u, v] = edges[i];
if(!way1.count(i) && !way2.count(i)) {
ans = min(ans, dist[1][v] + dist[2][u] + dist[0][n] + C[i] + D[i]);
ans = min(ans, dist[0][v] + dist[3][u] + dist[1][1] + C[i] + D[i]);
ans = min(ans, dist[0][v] + dist[3][u] + dist[1][v] + dist[2][u] + 2 * C[i] + D[i]);
}
else {
g[0][u].erase(find(g[0][u].begin(), g[0][u].end(), make_pair(v, i)));
g[0][v].push_back({u, i});
auto d1 = dijkstra(1, 0), d2 = dijkstra(n, 0);
ans = min(ans, d1[n] + d2[1] + D[i]);
g[0][u].push_back({v, i});
g[0][v].erase(find(g[0][v].begin(), g[0][v].end(), make_pair(u, i)));
}
res = min(res, ans);
}
if(res >= INF) res = -1;
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
432 KB |
Output is correct |
4 |
Correct |
5 ms |
476 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 |
1 ms |
264 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
37 ms |
492 KB |
Output is correct |
11 |
Correct |
54 ms |
496 KB |
Output is correct |
12 |
Correct |
50 ms |
480 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
388 KB |
Output is correct |
16 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
3880 KB |
Output is correct |
2 |
Correct |
17 ms |
3796 KB |
Output is correct |
3 |
Correct |
17 ms |
3976 KB |
Output is correct |
4 |
Correct |
4 ms |
468 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
22 ms |
4092 KB |
Output is correct |
10 |
Correct |
18 ms |
4004 KB |
Output is correct |
11 |
Correct |
18 ms |
3968 KB |
Output is correct |
12 |
Correct |
17 ms |
3972 KB |
Output is correct |
13 |
Correct |
23 ms |
4052 KB |
Output is correct |
14 |
Correct |
18 ms |
4068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
12 ms |
2900 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
14 ms |
3732 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
21 ms |
3712 KB |
Output is correct |
9 |
Correct |
14 ms |
3760 KB |
Output is correct |
10 |
Correct |
15 ms |
3680 KB |
Output is correct |
11 |
Correct |
14 ms |
3796 KB |
Output is correct |
12 |
Correct |
14 ms |
3716 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
372 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
376 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
15 ms |
3796 KB |
Output is correct |
20 |
Correct |
14 ms |
3800 KB |
Output is correct |
21 |
Correct |
14 ms |
3688 KB |
Output is correct |
22 |
Correct |
14 ms |
3672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
432 KB |
Output is correct |
4 |
Correct |
5 ms |
476 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 |
1 ms |
264 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
37 ms |
492 KB |
Output is correct |
11 |
Correct |
54 ms |
496 KB |
Output is correct |
12 |
Correct |
50 ms |
480 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
3 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
388 KB |
Output is correct |
16 |
Correct |
3 ms |
468 KB |
Output is correct |
17 |
Correct |
16 ms |
3880 KB |
Output is correct |
18 |
Correct |
17 ms |
3796 KB |
Output is correct |
19 |
Correct |
17 ms |
3976 KB |
Output is correct |
20 |
Correct |
4 ms |
468 KB |
Output is correct |
21 |
Correct |
3 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
22 ms |
4092 KB |
Output is correct |
26 |
Correct |
18 ms |
4004 KB |
Output is correct |
27 |
Correct |
18 ms |
3968 KB |
Output is correct |
28 |
Correct |
17 ms |
3972 KB |
Output is correct |
29 |
Correct |
23 ms |
4052 KB |
Output is correct |
30 |
Correct |
18 ms |
4068 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
12 ms |
2900 KB |
Output is correct |
34 |
Correct |
1 ms |
376 KB |
Output is correct |
35 |
Correct |
14 ms |
3732 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
21 ms |
3712 KB |
Output is correct |
39 |
Correct |
14 ms |
3760 KB |
Output is correct |
40 |
Correct |
15 ms |
3680 KB |
Output is correct |
41 |
Correct |
14 ms |
3796 KB |
Output is correct |
42 |
Correct |
14 ms |
3716 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
372 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
376 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
15 ms |
3796 KB |
Output is correct |
50 |
Correct |
14 ms |
3800 KB |
Output is correct |
51 |
Correct |
14 ms |
3688 KB |
Output is correct |
52 |
Correct |
14 ms |
3672 KB |
Output is correct |
53 |
Correct |
24 ms |
3796 KB |
Output is correct |
54 |
Correct |
19 ms |
3836 KB |
Output is correct |
55 |
Correct |
17 ms |
3756 KB |
Output is correct |
56 |
Correct |
3 ms |
468 KB |
Output is correct |
57 |
Correct |
2 ms |
468 KB |
Output is correct |
58 |
Correct |
83 ms |
3048 KB |
Output is correct |
59 |
Correct |
78 ms |
3032 KB |
Output is correct |
60 |
Correct |
101 ms |
3056 KB |
Output is correct |
61 |
Correct |
81 ms |
3048 KB |
Output is correct |
62 |
Correct |
76 ms |
3080 KB |
Output is correct |
63 |
Correct |
102 ms |
3060 KB |
Output is correct |
64 |
Correct |
114 ms |
3192 KB |
Output is correct |
65 |
Correct |
121 ms |
3240 KB |
Output is correct |
66 |
Correct |
172 ms |
3212 KB |
Output is correct |
67 |
Correct |
10 ms |
2984 KB |
Output is correct |
68 |
Correct |
18 ms |
4024 KB |
Output is correct |
69 |
Correct |
15 ms |
3936 KB |
Output is correct |
70 |
Correct |
19 ms |
3992 KB |
Output is correct |
71 |
Correct |
18 ms |
4004 KB |
Output is correct |
72 |
Correct |
17 ms |
4020 KB |
Output is correct |
73 |
Correct |
19 ms |
4040 KB |
Output is correct |
74 |
Correct |
17 ms |
3972 KB |
Output is correct |