#include "bits/stdc++.h"
using namespace std;
#define ar array
#define int long long
const int N = 205, M = 5e4 + 5;
vector<ar<int, 3>> edges[N];
int a[M], b[M], c[M], p[M];
int d[N], par[N], T[N][N], used[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, m; cin>>n>>m;
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
T[i][j] = 1e10;
} T[i][i] = 0;
}
for(int i=0;i<m;i++){
cin>>a[i]>>b[i]>>c[i]>>p[i];
edges[a[i]].push_back({b[i], c[i], i});
T[a[i]][b[i]] = min(T[a[i]][b[i]], c[i]);
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
T[i][j] = min(T[i][j], T[i][k] + T[k][j]);
}
}
}
vector<int> tot;
auto djk = [&](int u){
for(int i=1;i<=n;i++) d[i] = 1e10;
memset(par, -1, sizeof par);
memset(used, 0, sizeof used);
d[u] = 0;
while(~u){
used[u] = 1;
int p = -1;
for(auto x : edges[u]){
if(d[x[0]] > d[u] + x[1]){
d[x[0]] = d[u] + x[1];
par[x[0]] = x[2];
}
}
for(int i=1;i<=n;i++){
if(used[i]) continue;
if(p == -1 || d[i] < d[p]) p = i;
}
u = p;
}
for(int i=1;i<=n;i++){
if(~par[i]) tot.push_back(par[i]);
}
};
ar<int, 2> D {};
djk(1), D[0] = d[n];
djk(n), D[1] = d[1];
int res = D[0] + D[1];
auto check = [&](int in){
swap(a[in], b[in]);
for(int i=1;i<=n;i++) edges[i].clear();
for(int i=0;i<m;i++){
edges[a[i]].push_back({b[i], c[i], -1});
}
int D = 0;
djk(1), D += d[n];
djk(n), D += d[1];
res = min(res, D + p[in]);
swap(a[in], b[in]);
};
sort(tot.begin(), tot.end());
tot.erase(unique(tot.begin(), tot.end()), tot.end());
vector<int> is(m);
for(auto x : tot){
check(x);
is[x] = 1;
}
for(int i=0;i<m;i++){
if(is[i]) continue;
int st = min(T[1][b[i]] + T[a[i]][n] + c[i], D[0]), ts = min(T[n][b[i]] + c[i] + T[a[i]][1], D[1]);
res = min(res, st + ts + p[i]);
}
if(res >= 1e10) cout<<-1<<"\n";
else cout<<res<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
732 KB |
Output is correct |
2 |
Correct |
9 ms |
588 KB |
Output is correct |
3 |
Correct |
60 ms |
716 KB |
Output is correct |
4 |
Correct |
63 ms |
716 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
11 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
48 ms |
736 KB |
Output is correct |
11 |
Correct |
62 ms |
736 KB |
Output is correct |
12 |
Correct |
57 ms |
736 KB |
Output is correct |
13 |
Correct |
33 ms |
732 KB |
Output is correct |
14 |
Correct |
50 ms |
716 KB |
Output is correct |
15 |
Correct |
51 ms |
752 KB |
Output is correct |
16 |
Correct |
48 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
259 ms |
4540 KB |
Output is correct |
2 |
Correct |
274 ms |
4600 KB |
Output is correct |
3 |
Correct |
268 ms |
4524 KB |
Output is correct |
4 |
Correct |
59 ms |
816 KB |
Output is correct |
5 |
Correct |
49 ms |
732 KB |
Output is correct |
6 |
Correct |
9 ms |
700 KB |
Output is correct |
7 |
Correct |
14 ms |
700 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
145 ms |
4500 KB |
Output is correct |
10 |
Correct |
140 ms |
4428 KB |
Output is correct |
11 |
Correct |
224 ms |
4440 KB |
Output is correct |
12 |
Correct |
220 ms |
4612 KB |
Output is correct |
13 |
Correct |
212 ms |
4548 KB |
Output is correct |
14 |
Correct |
192 ms |
4768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
740 KB |
Output is correct |
2 |
Correct |
23 ms |
588 KB |
Output is correct |
3 |
Correct |
258 ms |
3528 KB |
Output is correct |
4 |
Correct |
17 ms |
684 KB |
Output is correct |
5 |
Correct |
304 ms |
4424 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
171 ms |
4428 KB |
Output is correct |
9 |
Correct |
187 ms |
4520 KB |
Output is correct |
10 |
Correct |
252 ms |
4588 KB |
Output is correct |
11 |
Correct |
247 ms |
4396 KB |
Output is correct |
12 |
Correct |
274 ms |
4664 KB |
Output is correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
588 KB |
Output is correct |
16 |
Correct |
1 ms |
588 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
0 ms |
588 KB |
Output is correct |
19 |
Correct |
271 ms |
4696 KB |
Output is correct |
20 |
Correct |
249 ms |
4292 KB |
Output is correct |
21 |
Correct |
251 ms |
4548 KB |
Output is correct |
22 |
Correct |
241 ms |
4348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
732 KB |
Output is correct |
2 |
Correct |
9 ms |
588 KB |
Output is correct |
3 |
Correct |
60 ms |
716 KB |
Output is correct |
4 |
Correct |
63 ms |
716 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
11 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
716 KB |
Output is correct |
10 |
Correct |
48 ms |
736 KB |
Output is correct |
11 |
Correct |
62 ms |
736 KB |
Output is correct |
12 |
Correct |
57 ms |
736 KB |
Output is correct |
13 |
Correct |
33 ms |
732 KB |
Output is correct |
14 |
Correct |
50 ms |
716 KB |
Output is correct |
15 |
Correct |
51 ms |
752 KB |
Output is correct |
16 |
Correct |
48 ms |
856 KB |
Output is correct |
17 |
Correct |
259 ms |
4540 KB |
Output is correct |
18 |
Correct |
274 ms |
4600 KB |
Output is correct |
19 |
Correct |
268 ms |
4524 KB |
Output is correct |
20 |
Correct |
59 ms |
816 KB |
Output is correct |
21 |
Correct |
49 ms |
732 KB |
Output is correct |
22 |
Correct |
9 ms |
700 KB |
Output is correct |
23 |
Correct |
14 ms |
700 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
145 ms |
4500 KB |
Output is correct |
26 |
Correct |
140 ms |
4428 KB |
Output is correct |
27 |
Correct |
224 ms |
4440 KB |
Output is correct |
28 |
Correct |
220 ms |
4612 KB |
Output is correct |
29 |
Correct |
212 ms |
4548 KB |
Output is correct |
30 |
Correct |
192 ms |
4768 KB |
Output is correct |
31 |
Correct |
49 ms |
740 KB |
Output is correct |
32 |
Correct |
23 ms |
588 KB |
Output is correct |
33 |
Correct |
258 ms |
3528 KB |
Output is correct |
34 |
Correct |
17 ms |
684 KB |
Output is correct |
35 |
Correct |
304 ms |
4424 KB |
Output is correct |
36 |
Correct |
1 ms |
588 KB |
Output is correct |
37 |
Correct |
1 ms |
588 KB |
Output is correct |
38 |
Correct |
171 ms |
4428 KB |
Output is correct |
39 |
Correct |
187 ms |
4520 KB |
Output is correct |
40 |
Correct |
252 ms |
4588 KB |
Output is correct |
41 |
Correct |
247 ms |
4396 KB |
Output is correct |
42 |
Correct |
274 ms |
4664 KB |
Output is correct |
43 |
Correct |
1 ms |
588 KB |
Output is correct |
44 |
Correct |
1 ms |
588 KB |
Output is correct |
45 |
Correct |
1 ms |
588 KB |
Output is correct |
46 |
Correct |
1 ms |
588 KB |
Output is correct |
47 |
Correct |
1 ms |
588 KB |
Output is correct |
48 |
Correct |
0 ms |
588 KB |
Output is correct |
49 |
Correct |
271 ms |
4696 KB |
Output is correct |
50 |
Correct |
249 ms |
4292 KB |
Output is correct |
51 |
Correct |
251 ms |
4548 KB |
Output is correct |
52 |
Correct |
241 ms |
4348 KB |
Output is correct |
53 |
Correct |
244 ms |
4292 KB |
Output is correct |
54 |
Correct |
270 ms |
4528 KB |
Output is correct |
55 |
Correct |
252 ms |
4556 KB |
Output is correct |
56 |
Correct |
65 ms |
748 KB |
Output is correct |
57 |
Correct |
54 ms |
736 KB |
Output is correct |
58 |
Correct |
165 ms |
3548 KB |
Output is correct |
59 |
Correct |
177 ms |
3588 KB |
Output is correct |
60 |
Correct |
238 ms |
3568 KB |
Output is correct |
61 |
Correct |
163 ms |
3532 KB |
Output is correct |
62 |
Correct |
177 ms |
3560 KB |
Output is correct |
63 |
Correct |
241 ms |
3496 KB |
Output is correct |
64 |
Correct |
216 ms |
3656 KB |
Output is correct |
65 |
Correct |
222 ms |
3780 KB |
Output is correct |
66 |
Correct |
304 ms |
3788 KB |
Output is correct |
67 |
Correct |
14 ms |
3364 KB |
Output is correct |
68 |
Correct |
162 ms |
4484 KB |
Output is correct |
69 |
Correct |
142 ms |
4556 KB |
Output is correct |
70 |
Correct |
229 ms |
4572 KB |
Output is correct |
71 |
Correct |
242 ms |
4368 KB |
Output is correct |
72 |
Correct |
229 ms |
4364 KB |
Output is correct |
73 |
Correct |
229 ms |
4344 KB |
Output is correct |
74 |
Correct |
228 ms |
4512 KB |
Output is correct |