#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 12, MOD = (int)1e9 + 7;
#define int ll
const ll inf = 1e16;
int n,m;
int U[N],V[N],C[N],D[N];
void dijkstra(int ver,vector<ll> &d,vector<int> &pr) {
d.resize(n);
pr.resize(n);
for(int i = 0;i < n;i++) {
d[i] = inf;
}
d[ver] = 0;
vector<int> vis(n,0);
vector<vector<int>> g;g.resize(n);
for(int i = 0;i < m;++i) {
g[U[i]].push_back(i);
}
for(int i = 0;i < n;i++) {
int v = -1;
for(int j = 0;j < n;j++) {
if(vis[j]) continue;
if(v == -1 || d[j] < d[v]) {
v = j;
}
}
vis[v] = 1;
for(int t:g[v]) {
int to = V[t],w = C[t];
if(d[to] > d[v] + w) {
d[to] = d[v] + w;
pr[to] = t;
}
}
}
}
vector<ll> d[N];
vector<int> p[N];
ll res = inf;
bool in[N];
void test() {
cin >> n >> m;
for(int i = 0;i < m;i++) {
cin >> U[i] >> V[i] >> C[i] >> D[i];
U[i]--;V[i]--;
}
for(int i = 0;i < n;++i) {
dijkstra(i,d[i],p[i]);
}
res = d[0][n-1] + d[n - 1][0];
vector<ll> bfd;
vector<int> bfp;
// for(int j = 0;j < m;j++) {
// in[j] = 1;
// swap(V[j],U[j]);
// ll nv = D[j];
// dijkstra(0,bfd,bfp);
// nv += bfd[n - 1];
// dijkstra(n - 1,bfd,bfp);
// nv += bfd[0];
// res = min(res,nv);
// swap(V[j],U[j]);
// }
auto make = [&](int j){
in[j] = 1;
swap(V[j],U[j]);
ll nv = D[j];
dijkstra(0,bfd,bfp);
nv += bfd[n - 1];
dijkstra(n - 1,bfd,bfp);
nv += bfd[0];
res = min(res,nv);
swap(V[j],U[j]);
};
for(int i = 0;i < n;i++) {
if(d[0][i] != inf && i != 0) {
// cout << i << ' ' << p[0][i] << "x\n";
make(p[0][i]);
}
if(d[n - 1][i] != inf && i != n - 1) {
make(p[n - 1][i]);
}
}
for(int i = 0;i < m;i++) {
if(in[i]) continue;
swap(U[i],V[i]);
ll val = min(d[0][n - 1],d[0][U[i]] + d[V[i]][n - 1] + C[i]) + D[i];
val += min(d[n - 1][0],d[n - 1][U[i]] + d[V[i]][0] + C[i]);
// cout << i << ' ' << val << ' ' << min(d[0][n-1],d[0][U[i]] + d[V[i]][n - 1] + C[i]) << ' ' << d[n - 1][U[i]] + d[V[i]][0] + C[i] << '\n';
res = min(res,val);
}
if(res >= inf) res = -1;
cout << res << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
5724 KB |
Output is correct |
2 |
Correct |
18 ms |
5724 KB |
Output is correct |
3 |
Correct |
91 ms |
5904 KB |
Output is correct |
4 |
Correct |
92 ms |
5720 KB |
Output is correct |
5 |
Correct |
4 ms |
5212 KB |
Output is correct |
6 |
Correct |
19 ms |
5664 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
95 ms |
5868 KB |
Output is correct |
11 |
Correct |
96 ms |
5724 KB |
Output is correct |
12 |
Correct |
92 ms |
5724 KB |
Output is correct |
13 |
Correct |
57 ms |
5720 KB |
Output is correct |
14 |
Correct |
72 ms |
5724 KB |
Output is correct |
15 |
Correct |
72 ms |
5692 KB |
Output is correct |
16 |
Correct |
76 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
578 ms |
9228 KB |
Output is correct |
2 |
Correct |
588 ms |
9448 KB |
Output is correct |
3 |
Correct |
601 ms |
9684 KB |
Output is correct |
4 |
Correct |
104 ms |
5724 KB |
Output is correct |
5 |
Correct |
77 ms |
5872 KB |
Output is correct |
6 |
Correct |
18 ms |
5724 KB |
Output is correct |
7 |
Correct |
22 ms |
5724 KB |
Output is correct |
8 |
Correct |
3 ms |
4952 KB |
Output is correct |
9 |
Correct |
355 ms |
9360 KB |
Output is correct |
10 |
Correct |
340 ms |
9620 KB |
Output is correct |
11 |
Correct |
476 ms |
9384 KB |
Output is correct |
12 |
Correct |
476 ms |
9512 KB |
Output is correct |
13 |
Correct |
460 ms |
9384 KB |
Output is correct |
14 |
Correct |
470 ms |
9580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
5640 KB |
Output is correct |
2 |
Correct |
29 ms |
5724 KB |
Output is correct |
3 |
Correct |
462 ms |
8372 KB |
Output is correct |
4 |
Correct |
24 ms |
5724 KB |
Output is correct |
5 |
Correct |
578 ms |
9112 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
5168 KB |
Output is correct |
8 |
Correct |
324 ms |
9108 KB |
Output is correct |
9 |
Correct |
384 ms |
9116 KB |
Output is correct |
10 |
Correct |
481 ms |
9168 KB |
Output is correct |
11 |
Correct |
463 ms |
9120 KB |
Output is correct |
12 |
Correct |
524 ms |
9212 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
5164 KB |
Output is correct |
18 |
Correct |
2 ms |
5172 KB |
Output is correct |
19 |
Correct |
550 ms |
9164 KB |
Output is correct |
20 |
Correct |
495 ms |
9136 KB |
Output is correct |
21 |
Correct |
470 ms |
9172 KB |
Output is correct |
22 |
Correct |
520 ms |
9120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
5724 KB |
Output is correct |
2 |
Correct |
18 ms |
5724 KB |
Output is correct |
3 |
Correct |
91 ms |
5904 KB |
Output is correct |
4 |
Correct |
92 ms |
5720 KB |
Output is correct |
5 |
Correct |
4 ms |
5212 KB |
Output is correct |
6 |
Correct |
19 ms |
5664 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
95 ms |
5868 KB |
Output is correct |
11 |
Correct |
96 ms |
5724 KB |
Output is correct |
12 |
Correct |
92 ms |
5724 KB |
Output is correct |
13 |
Correct |
57 ms |
5720 KB |
Output is correct |
14 |
Correct |
72 ms |
5724 KB |
Output is correct |
15 |
Correct |
72 ms |
5692 KB |
Output is correct |
16 |
Correct |
76 ms |
5972 KB |
Output is correct |
17 |
Correct |
578 ms |
9228 KB |
Output is correct |
18 |
Correct |
588 ms |
9448 KB |
Output is correct |
19 |
Correct |
601 ms |
9684 KB |
Output is correct |
20 |
Correct |
104 ms |
5724 KB |
Output is correct |
21 |
Correct |
77 ms |
5872 KB |
Output is correct |
22 |
Correct |
18 ms |
5724 KB |
Output is correct |
23 |
Correct |
22 ms |
5724 KB |
Output is correct |
24 |
Correct |
3 ms |
4952 KB |
Output is correct |
25 |
Correct |
355 ms |
9360 KB |
Output is correct |
26 |
Correct |
340 ms |
9620 KB |
Output is correct |
27 |
Correct |
476 ms |
9384 KB |
Output is correct |
28 |
Correct |
476 ms |
9512 KB |
Output is correct |
29 |
Correct |
460 ms |
9384 KB |
Output is correct |
30 |
Correct |
470 ms |
9580 KB |
Output is correct |
31 |
Correct |
100 ms |
5640 KB |
Output is correct |
32 |
Correct |
29 ms |
5724 KB |
Output is correct |
33 |
Correct |
462 ms |
8372 KB |
Output is correct |
34 |
Correct |
24 ms |
5724 KB |
Output is correct |
35 |
Correct |
578 ms |
9112 KB |
Output is correct |
36 |
Correct |
3 ms |
4952 KB |
Output is correct |
37 |
Correct |
2 ms |
5168 KB |
Output is correct |
38 |
Correct |
324 ms |
9108 KB |
Output is correct |
39 |
Correct |
384 ms |
9116 KB |
Output is correct |
40 |
Correct |
481 ms |
9168 KB |
Output is correct |
41 |
Correct |
463 ms |
9120 KB |
Output is correct |
42 |
Correct |
524 ms |
9212 KB |
Output is correct |
43 |
Correct |
2 ms |
4956 KB |
Output is correct |
44 |
Correct |
2 ms |
4956 KB |
Output is correct |
45 |
Correct |
2 ms |
4956 KB |
Output is correct |
46 |
Correct |
2 ms |
4956 KB |
Output is correct |
47 |
Correct |
2 ms |
5164 KB |
Output is correct |
48 |
Correct |
2 ms |
5172 KB |
Output is correct |
49 |
Correct |
550 ms |
9164 KB |
Output is correct |
50 |
Correct |
495 ms |
9136 KB |
Output is correct |
51 |
Correct |
470 ms |
9172 KB |
Output is correct |
52 |
Correct |
520 ms |
9120 KB |
Output is correct |
53 |
Correct |
572 ms |
9292 KB |
Output is correct |
54 |
Correct |
564 ms |
9168 KB |
Output is correct |
55 |
Correct |
648 ms |
9188 KB |
Output is correct |
56 |
Correct |
94 ms |
5724 KB |
Output is correct |
57 |
Correct |
92 ms |
5724 KB |
Output is correct |
58 |
Correct |
480 ms |
8504 KB |
Output is correct |
59 |
Correct |
518 ms |
8528 KB |
Output is correct |
60 |
Correct |
465 ms |
8504 KB |
Output is correct |
61 |
Correct |
462 ms |
8512 KB |
Output is correct |
62 |
Correct |
499 ms |
8520 KB |
Output is correct |
63 |
Correct |
462 ms |
8520 KB |
Output is correct |
64 |
Correct |
596 ms |
8920 KB |
Output is correct |
65 |
Correct |
553 ms |
8524 KB |
Output is correct |
66 |
Correct |
584 ms |
8544 KB |
Output is correct |
67 |
Correct |
20 ms |
7984 KB |
Output is correct |
68 |
Correct |
355 ms |
9348 KB |
Output is correct |
69 |
Correct |
375 ms |
9852 KB |
Output is correct |
70 |
Correct |
505 ms |
9380 KB |
Output is correct |
71 |
Correct |
444 ms |
9364 KB |
Output is correct |
72 |
Correct |
487 ms |
9568 KB |
Output is correct |
73 |
Correct |
485 ms |
9736 KB |
Output is correct |
74 |
Correct |
473 ms |
9660 KB |
Output is correct |