//Autor: Bartłomiej Czarkowski
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
struct edge {
int c, d;
};
const int N = 210;
const int M = 50100;
int n, m;
vector<edge> kr[N][N];
long long odp;
int odw[N];
long long v[2][N][N];
long long odl[2][2][N][N]; // czy obrócone krawędzie | z 1 / N | z wyłączeniem kogo | do kogo
void wylicz(long long odl[N], long long v[N][N], int src, int bez) {
for (int i = 1; i <= n; ++i) {
odl[i] = 1e18;
odw[i] = 0;
}
if (src == bez) {
return;
}
odw[bez] = 1;
odl[src] = 0;
for (int i = 1; i <= n; ++i) {
int x = 0;
long long od = 1e18;
for (int j = 1; j <= n; ++j) {
if (odw[j]) {
continue;
}
if (od > odl[j]) {
x = j;
od = odl[j];
}
}
if (!x) {
return;
}
odw[x] = 1;
for (int j = 1; j <= n; ++j) {
if (odw[j]) {
continue;
}
odl[j] = min(odl[j], odl[x] + v[x][j]);
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
v[0][i][j] = v[1][i][j] = 1e18;
}
}
for (int i = 1; i <= m; ++i) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
kr[a][b].push_back({c, d});
v[0][a][b] = min(v[0][a][b], (long long)c);
v[1][b][a] = min(v[1][b][a], (long long)c);
}
for (int i = 1; i <= n + 1; ++i) {
wylicz(odl[0][0][i], v[0], 1, i);
wylicz(odl[1][0][i], v[1], 1, i);
wylicz(odl[0][1][i], v[0], n, i);
wylicz(odl[1][1][i], v[1], n, i);
}
odp = odl[0][0][n + 1][n] + odl[0][1][n + 1][1]; // bez obracania krawędzi
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
// obracamy krawędź z i do j
vector<long long> cost;
cost.push_back(1e18);
for (auto k : kr[i][j]) {
cost.push_back(min(cost.back(), (long long)k.c));
}
long long suf = 1e18;
reverse(kr[i][j].begin(), kr[i][j].end());
for (auto k : kr[i][j]) {
cost.pop_back();
long long c1n1 = odl[0][0][j][i] + min(cost.back(), suf) + odl[1][1][i][j];
long long c1n2 = 1e18;
long long c1n3 = odl[0][0][i][j] + k.c + odl[1][1][j][i];
for (int g = 1; g <= n; ++g) {
c1n2 = min(c1n2, odl[0][0][j][g] + odl[1][1][i][g]);
}
long long cn11 = odl[0][1][j][i] + min(cost.back(), suf) + odl[1][0][i][j];
long long cn12 = 1e18;
long long cn13 = odl[0][1][i][j] + k.c + odl[1][0][j][i];
for (int g = 1; g <= n; ++g) {
cn12 = min(cn12, odl[0][1][i][g] + odl[1][0][j][g]);
}
odp = min(odp, c1n1 + cn11);
odp = min(odp, c1n1 + cn12);
odp = min(odp, c1n1 + cn13 + k.d);
odp = min(odp, c1n2 + cn11);
odp = min(odp, c1n2 + cn12);
odp = min(odp, c1n2 + cn13 + k.d);
odp = min(odp, c1n3 + cn11 + k.d);
odp = min(odp, c1n3 + cn12 + k.d);
odp = min(odp, c1n3 + cn13 + k.d);
suf = min(suf, (long long)k.c);
}
}
}
if (odp > 1e14) {
printf("-1\n");
return 0;
}
printf("%lld\n", odp);
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:61:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
ho_t4.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%d%d%d%d", &a, &b, &c, &d);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
3364 KB |
Output is correct |
2 |
Correct |
13 ms |
3288 KB |
Output is correct |
3 |
Correct |
145 ms |
3300 KB |
Output is correct |
4 |
Correct |
154 ms |
3248 KB |
Output is correct |
5 |
Correct |
2 ms |
1620 KB |
Output is correct |
6 |
Correct |
31 ms |
3320 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
143 ms |
3348 KB |
Output is correct |
11 |
Correct |
138 ms |
3276 KB |
Output is correct |
12 |
Correct |
138 ms |
3324 KB |
Output is correct |
13 |
Correct |
65 ms |
3312 KB |
Output is correct |
14 |
Correct |
106 ms |
3280 KB |
Output is correct |
15 |
Correct |
109 ms |
3284 KB |
Output is correct |
16 |
Correct |
108 ms |
3372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
4016 KB |
Output is correct |
2 |
Correct |
185 ms |
4104 KB |
Output is correct |
3 |
Correct |
191 ms |
4140 KB |
Output is correct |
4 |
Correct |
146 ms |
3328 KB |
Output is correct |
5 |
Correct |
135 ms |
3372 KB |
Output is correct |
6 |
Correct |
47 ms |
3312 KB |
Output is correct |
7 |
Correct |
7 ms |
3284 KB |
Output is correct |
8 |
Correct |
1 ms |
1344 KB |
Output is correct |
9 |
Correct |
112 ms |
5164 KB |
Output is correct |
10 |
Correct |
112 ms |
5068 KB |
Output is correct |
11 |
Correct |
160 ms |
5156 KB |
Output is correct |
12 |
Correct |
153 ms |
5044 KB |
Output is correct |
13 |
Correct |
148 ms |
5136 KB |
Output is correct |
14 |
Correct |
152 ms |
5100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
3316 KB |
Output is correct |
2 |
Correct |
48 ms |
3272 KB |
Output is correct |
3 |
Correct |
98 ms |
4148 KB |
Output is correct |
4 |
Correct |
33 ms |
3280 KB |
Output is correct |
5 |
Correct |
109 ms |
4308 KB |
Output is correct |
6 |
Correct |
1 ms |
1364 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
92 ms |
5080 KB |
Output is correct |
9 |
Correct |
89 ms |
5116 KB |
Output is correct |
10 |
Correct |
108 ms |
5112 KB |
Output is correct |
11 |
Correct |
105 ms |
5196 KB |
Output is correct |
12 |
Correct |
100 ms |
5108 KB |
Output is correct |
13 |
Correct |
1 ms |
1364 KB |
Output is correct |
14 |
Correct |
1 ms |
1364 KB |
Output is correct |
15 |
Correct |
1 ms |
1364 KB |
Output is correct |
16 |
Correct |
1 ms |
1352 KB |
Output is correct |
17 |
Correct |
1 ms |
1348 KB |
Output is correct |
18 |
Correct |
1 ms |
1364 KB |
Output is correct |
19 |
Correct |
106 ms |
5112 KB |
Output is correct |
20 |
Correct |
107 ms |
5308 KB |
Output is correct |
21 |
Correct |
106 ms |
5104 KB |
Output is correct |
22 |
Correct |
106 ms |
5096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
3364 KB |
Output is correct |
2 |
Correct |
13 ms |
3288 KB |
Output is correct |
3 |
Correct |
145 ms |
3300 KB |
Output is correct |
4 |
Correct |
154 ms |
3248 KB |
Output is correct |
5 |
Correct |
2 ms |
1620 KB |
Output is correct |
6 |
Correct |
31 ms |
3320 KB |
Output is correct |
7 |
Correct |
1 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
1364 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
143 ms |
3348 KB |
Output is correct |
11 |
Correct |
138 ms |
3276 KB |
Output is correct |
12 |
Correct |
138 ms |
3324 KB |
Output is correct |
13 |
Correct |
65 ms |
3312 KB |
Output is correct |
14 |
Correct |
106 ms |
3280 KB |
Output is correct |
15 |
Correct |
109 ms |
3284 KB |
Output is correct |
16 |
Correct |
108 ms |
3372 KB |
Output is correct |
17 |
Correct |
186 ms |
4016 KB |
Output is correct |
18 |
Correct |
185 ms |
4104 KB |
Output is correct |
19 |
Correct |
191 ms |
4140 KB |
Output is correct |
20 |
Correct |
146 ms |
3328 KB |
Output is correct |
21 |
Correct |
135 ms |
3372 KB |
Output is correct |
22 |
Correct |
47 ms |
3312 KB |
Output is correct |
23 |
Correct |
7 ms |
3284 KB |
Output is correct |
24 |
Correct |
1 ms |
1344 KB |
Output is correct |
25 |
Correct |
112 ms |
5164 KB |
Output is correct |
26 |
Correct |
112 ms |
5068 KB |
Output is correct |
27 |
Correct |
160 ms |
5156 KB |
Output is correct |
28 |
Correct |
153 ms |
5044 KB |
Output is correct |
29 |
Correct |
148 ms |
5136 KB |
Output is correct |
30 |
Correct |
152 ms |
5100 KB |
Output is correct |
31 |
Correct |
84 ms |
3316 KB |
Output is correct |
32 |
Correct |
48 ms |
3272 KB |
Output is correct |
33 |
Correct |
98 ms |
4148 KB |
Output is correct |
34 |
Correct |
33 ms |
3280 KB |
Output is correct |
35 |
Correct |
109 ms |
4308 KB |
Output is correct |
36 |
Correct |
1 ms |
1364 KB |
Output is correct |
37 |
Correct |
1 ms |
1364 KB |
Output is correct |
38 |
Correct |
92 ms |
5080 KB |
Output is correct |
39 |
Correct |
89 ms |
5116 KB |
Output is correct |
40 |
Correct |
108 ms |
5112 KB |
Output is correct |
41 |
Correct |
105 ms |
5196 KB |
Output is correct |
42 |
Correct |
100 ms |
5108 KB |
Output is correct |
43 |
Correct |
1 ms |
1364 KB |
Output is correct |
44 |
Correct |
1 ms |
1364 KB |
Output is correct |
45 |
Correct |
1 ms |
1364 KB |
Output is correct |
46 |
Correct |
1 ms |
1352 KB |
Output is correct |
47 |
Correct |
1 ms |
1348 KB |
Output is correct |
48 |
Correct |
1 ms |
1364 KB |
Output is correct |
49 |
Correct |
106 ms |
5112 KB |
Output is correct |
50 |
Correct |
107 ms |
5308 KB |
Output is correct |
51 |
Correct |
106 ms |
5104 KB |
Output is correct |
52 |
Correct |
106 ms |
5096 KB |
Output is correct |
53 |
Correct |
182 ms |
5324 KB |
Output is correct |
54 |
Correct |
197 ms |
5388 KB |
Output is correct |
55 |
Correct |
184 ms |
5312 KB |
Output is correct |
56 |
Correct |
142 ms |
3332 KB |
Output is correct |
57 |
Correct |
147 ms |
3316 KB |
Output is correct |
58 |
Correct |
172 ms |
4940 KB |
Output is correct |
59 |
Correct |
175 ms |
4976 KB |
Output is correct |
60 |
Correct |
170 ms |
4976 KB |
Output is correct |
61 |
Correct |
175 ms |
4876 KB |
Output is correct |
62 |
Correct |
177 ms |
4956 KB |
Output is correct |
63 |
Correct |
168 ms |
5020 KB |
Output is correct |
64 |
Correct |
176 ms |
4752 KB |
Output is correct |
65 |
Correct |
159 ms |
4796 KB |
Output is correct |
66 |
Correct |
142 ms |
4944 KB |
Output is correct |
67 |
Correct |
15 ms |
3140 KB |
Output is correct |
68 |
Correct |
114 ms |
5388 KB |
Output is correct |
69 |
Correct |
114 ms |
5316 KB |
Output is correct |
70 |
Correct |
149 ms |
5236 KB |
Output is correct |
71 |
Correct |
152 ms |
5336 KB |
Output is correct |
72 |
Correct |
152 ms |
5260 KB |
Output is correct |
73 |
Correct |
150 ms |
5272 KB |
Output is correct |
74 |
Correct |
148 ms |
5284 KB |
Output is correct |