#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 |
6 ms |
388 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
4 ms |
468 KB |
Output is correct |
5 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
37 ms |
468 KB |
Output is correct |
11 |
Correct |
52 ms |
488 KB |
Output is correct |
12 |
Correct |
46 ms |
468 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 |
380 KB |
Output is correct |
16 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
3832 KB |
Output is correct |
2 |
Correct |
17 ms |
3864 KB |
Output is correct |
3 |
Correct |
17 ms |
4012 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
4 ms |
392 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 |
372 KB |
Output is correct |
9 |
Correct |
14 ms |
4052 KB |
Output is correct |
10 |
Correct |
16 ms |
3964 KB |
Output is correct |
11 |
Correct |
23 ms |
4104 KB |
Output is correct |
12 |
Correct |
17 ms |
4008 KB |
Output is correct |
13 |
Correct |
17 ms |
3944 KB |
Output is correct |
14 |
Correct |
17 ms |
4180 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 |
18 ms |
2956 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
15 ms |
3792 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
13 ms |
3796 KB |
Output is correct |
9 |
Correct |
13 ms |
3668 KB |
Output is correct |
10 |
Correct |
14 ms |
3720 KB |
Output is correct |
11 |
Correct |
14 ms |
3736 KB |
Output is correct |
12 |
Correct |
14 ms |
3744 KB |
Output is correct |
13 |
Correct |
1 ms |
360 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
376 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
16 ms |
3836 KB |
Output is correct |
20 |
Correct |
14 ms |
3796 KB |
Output is correct |
21 |
Correct |
14 ms |
3796 KB |
Output is correct |
22 |
Correct |
14 ms |
3776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
388 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
4 ms |
468 KB |
Output is correct |
5 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
37 ms |
468 KB |
Output is correct |
11 |
Correct |
52 ms |
488 KB |
Output is correct |
12 |
Correct |
46 ms |
468 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 |
380 KB |
Output is correct |
16 |
Correct |
3 ms |
468 KB |
Output is correct |
17 |
Correct |
17 ms |
3832 KB |
Output is correct |
18 |
Correct |
17 ms |
3864 KB |
Output is correct |
19 |
Correct |
17 ms |
4012 KB |
Output is correct |
20 |
Correct |
3 ms |
468 KB |
Output is correct |
21 |
Correct |
4 ms |
392 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 |
372 KB |
Output is correct |
25 |
Correct |
14 ms |
4052 KB |
Output is correct |
26 |
Correct |
16 ms |
3964 KB |
Output is correct |
27 |
Correct |
23 ms |
4104 KB |
Output is correct |
28 |
Correct |
17 ms |
4008 KB |
Output is correct |
29 |
Correct |
17 ms |
3944 KB |
Output is correct |
30 |
Correct |
17 ms |
4180 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
18 ms |
2956 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
15 ms |
3792 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
13 ms |
3796 KB |
Output is correct |
39 |
Correct |
13 ms |
3668 KB |
Output is correct |
40 |
Correct |
14 ms |
3720 KB |
Output is correct |
41 |
Correct |
14 ms |
3736 KB |
Output is correct |
42 |
Correct |
14 ms |
3744 KB |
Output is correct |
43 |
Correct |
1 ms |
360 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
376 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
16 ms |
3836 KB |
Output is correct |
50 |
Correct |
14 ms |
3796 KB |
Output is correct |
51 |
Correct |
14 ms |
3796 KB |
Output is correct |
52 |
Correct |
14 ms |
3776 KB |
Output is correct |
53 |
Correct |
16 ms |
3772 KB |
Output is correct |
54 |
Correct |
18 ms |
3724 KB |
Output is correct |
55 |
Correct |
17 ms |
3776 KB |
Output is correct |
56 |
Correct |
3 ms |
468 KB |
Output is correct |
57 |
Correct |
2 ms |
384 KB |
Output is correct |
58 |
Correct |
74 ms |
3056 KB |
Output is correct |
59 |
Correct |
90 ms |
3032 KB |
Output is correct |
60 |
Correct |
102 ms |
2972 KB |
Output is correct |
61 |
Correct |
68 ms |
3028 KB |
Output is correct |
62 |
Correct |
77 ms |
3028 KB |
Output is correct |
63 |
Correct |
133 ms |
3068 KB |
Output is correct |
64 |
Correct |
113 ms |
3156 KB |
Output is correct |
65 |
Correct |
130 ms |
3236 KB |
Output is correct |
66 |
Correct |
159 ms |
3180 KB |
Output is correct |
67 |
Correct |
10 ms |
2896 KB |
Output is correct |
68 |
Correct |
14 ms |
3936 KB |
Output is correct |
69 |
Correct |
14 ms |
3960 KB |
Output is correct |
70 |
Correct |
24 ms |
4064 KB |
Output is correct |
71 |
Correct |
17 ms |
3924 KB |
Output is correct |
72 |
Correct |
16 ms |
3976 KB |
Output is correct |
73 |
Correct |
17 ms |
3976 KB |
Output is correct |
74 |
Correct |
18 ms |
4016 KB |
Output is correct |