#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]);
}
};
//~ const int inf = 2e9;
//~ auto bashka = [&](int t, int u){
//~ priority_queue<ar<int, 2>, vector<ar<int, 2>>, greater<ar<int, 2>>> q;
//~ q.push({0, u});
//~ vector<ar<int, 2>> d(n + 1, {inf, inf});
//~ memset(par, -1, sizeof par);
//~ d[u][0] = 0;
//~ while(!q.empty()){
//~ int u = q.top()[1], D = q.top()[0]; q.pop();
//~ if(u <= n){
//~ if(D > d[u][0]) continue;
//~ for(auto x : edges[t][u]){
//~ if(d[x[0]][0] > d[u][0] + x[1]){
//~ d[x[0]][0] = d[u][0] + x[1];
//~ q.push({d[x[0]][0], x[0]});
//~ }
//~ }
//~ for(auto x : edges[t^1][u]){
//~ if(d[x[0]][1] > d[u][0] + x[1]){
//~ d[x[0]][1] = d[u][0] + x[1];
//~ q.push({d[x[0]][1], x[0] + n});
//~ }
//~ }
//~ } else {
//~ }
//~ }
//~ vector<int> tt;
//~ for(int i=1;i<=n;i++){
//~ if(~par[i]) tt.push_back(par[i]);
//~ } return tt;
//~ };
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 |
756 KB |
Output is correct |
2 |
Correct |
9 ms |
628 KB |
Output is correct |
3 |
Correct |
66 ms |
756 KB |
Output is correct |
4 |
Correct |
62 ms |
756 KB |
Output is correct |
5 |
Correct |
2 ms |
708 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 |
49 ms |
752 KB |
Output is correct |
11 |
Correct |
61 ms |
716 KB |
Output is correct |
12 |
Correct |
63 ms |
752 KB |
Output is correct |
13 |
Correct |
34 ms |
720 KB |
Output is correct |
14 |
Correct |
52 ms |
772 KB |
Output is correct |
15 |
Correct |
48 ms |
716 KB |
Output is correct |
16 |
Correct |
48 ms |
772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
263 ms |
4728 KB |
Output is correct |
2 |
Correct |
287 ms |
4744 KB |
Output is correct |
3 |
Correct |
267 ms |
4528 KB |
Output is correct |
4 |
Correct |
62 ms |
848 KB |
Output is correct |
5 |
Correct |
48 ms |
772 KB |
Output is correct |
6 |
Correct |
10 ms |
716 KB |
Output is correct |
7 |
Correct |
14 ms |
580 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
146 ms |
4680 KB |
Output is correct |
10 |
Correct |
146 ms |
4792 KB |
Output is correct |
11 |
Correct |
221 ms |
4672 KB |
Output is correct |
12 |
Correct |
218 ms |
4804 KB |
Output is correct |
13 |
Correct |
207 ms |
4912 KB |
Output is correct |
14 |
Correct |
202 ms |
5084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
716 KB |
Output is correct |
2 |
Correct |
27 ms |
692 KB |
Output is correct |
3 |
Correct |
254 ms |
3652 KB |
Output is correct |
4 |
Correct |
15 ms |
588 KB |
Output is correct |
5 |
Correct |
296 ms |
4532 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
174 ms |
4532 KB |
Output is correct |
9 |
Correct |
185 ms |
4612 KB |
Output is correct |
10 |
Correct |
250 ms |
4688 KB |
Output is correct |
11 |
Correct |
238 ms |
4504 KB |
Output is correct |
12 |
Correct |
255 ms |
4796 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 |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
252 ms |
4748 KB |
Output is correct |
20 |
Correct |
238 ms |
4492 KB |
Output is correct |
21 |
Correct |
249 ms |
4680 KB |
Output is correct |
22 |
Correct |
240 ms |
4468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
756 KB |
Output is correct |
2 |
Correct |
9 ms |
628 KB |
Output is correct |
3 |
Correct |
66 ms |
756 KB |
Output is correct |
4 |
Correct |
62 ms |
756 KB |
Output is correct |
5 |
Correct |
2 ms |
708 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 |
49 ms |
752 KB |
Output is correct |
11 |
Correct |
61 ms |
716 KB |
Output is correct |
12 |
Correct |
63 ms |
752 KB |
Output is correct |
13 |
Correct |
34 ms |
720 KB |
Output is correct |
14 |
Correct |
52 ms |
772 KB |
Output is correct |
15 |
Correct |
48 ms |
716 KB |
Output is correct |
16 |
Correct |
48 ms |
772 KB |
Output is correct |
17 |
Correct |
263 ms |
4728 KB |
Output is correct |
18 |
Correct |
287 ms |
4744 KB |
Output is correct |
19 |
Correct |
267 ms |
4528 KB |
Output is correct |
20 |
Correct |
62 ms |
848 KB |
Output is correct |
21 |
Correct |
48 ms |
772 KB |
Output is correct |
22 |
Correct |
10 ms |
716 KB |
Output is correct |
23 |
Correct |
14 ms |
580 KB |
Output is correct |
24 |
Correct |
1 ms |
588 KB |
Output is correct |
25 |
Correct |
146 ms |
4680 KB |
Output is correct |
26 |
Correct |
146 ms |
4792 KB |
Output is correct |
27 |
Correct |
221 ms |
4672 KB |
Output is correct |
28 |
Correct |
218 ms |
4804 KB |
Output is correct |
29 |
Correct |
207 ms |
4912 KB |
Output is correct |
30 |
Correct |
202 ms |
5084 KB |
Output is correct |
31 |
Correct |
49 ms |
716 KB |
Output is correct |
32 |
Correct |
27 ms |
692 KB |
Output is correct |
33 |
Correct |
254 ms |
3652 KB |
Output is correct |
34 |
Correct |
15 ms |
588 KB |
Output is correct |
35 |
Correct |
296 ms |
4532 KB |
Output is correct |
36 |
Correct |
1 ms |
588 KB |
Output is correct |
37 |
Correct |
1 ms |
588 KB |
Output is correct |
38 |
Correct |
174 ms |
4532 KB |
Output is correct |
39 |
Correct |
185 ms |
4612 KB |
Output is correct |
40 |
Correct |
250 ms |
4688 KB |
Output is correct |
41 |
Correct |
238 ms |
4504 KB |
Output is correct |
42 |
Correct |
255 ms |
4796 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 |
1 ms |
588 KB |
Output is correct |
49 |
Correct |
252 ms |
4748 KB |
Output is correct |
50 |
Correct |
238 ms |
4492 KB |
Output is correct |
51 |
Correct |
249 ms |
4680 KB |
Output is correct |
52 |
Correct |
240 ms |
4468 KB |
Output is correct |
53 |
Correct |
241 ms |
4548 KB |
Output is correct |
54 |
Correct |
277 ms |
4772 KB |
Output is correct |
55 |
Correct |
255 ms |
4804 KB |
Output is correct |
56 |
Correct |
65 ms |
764 KB |
Output is correct |
57 |
Correct |
55 ms |
716 KB |
Output is correct |
58 |
Correct |
163 ms |
3820 KB |
Output is correct |
59 |
Correct |
186 ms |
3780 KB |
Output is correct |
60 |
Correct |
240 ms |
3816 KB |
Output is correct |
61 |
Correct |
165 ms |
3756 KB |
Output is correct |
62 |
Correct |
178 ms |
3780 KB |
Output is correct |
63 |
Correct |
239 ms |
3780 KB |
Output is correct |
64 |
Correct |
206 ms |
3892 KB |
Output is correct |
65 |
Correct |
218 ms |
3912 KB |
Output is correct |
66 |
Correct |
296 ms |
3908 KB |
Output is correct |
67 |
Correct |
14 ms |
4164 KB |
Output is correct |
68 |
Correct |
143 ms |
5700 KB |
Output is correct |
69 |
Correct |
141 ms |
5704 KB |
Output is correct |
70 |
Correct |
227 ms |
5700 KB |
Output is correct |
71 |
Correct |
225 ms |
5440 KB |
Output is correct |
72 |
Correct |
218 ms |
5540 KB |
Output is correct |
73 |
Correct |
232 ms |
5572 KB |
Output is correct |
74 |
Correct |
226 ms |
5556 KB |
Output is correct |