#include <bits/stdc++.h>
#define ff first
#define ss second
#define int unsigned
using namespace std;
typedef long long ll;
typedef pair<ll, int> pli;
typedef pair<int, int> pii;
struct st{int a, b, c; ll d;} kraw[50000];
vector<int> g[201][2];
int C[201][201][2], n;
int odl[4][201][201]; //0. to z 1 do innych, 1. to z n do innych, 2. to z innych do 1, 3. to z innych do n
bool odw[201];
void dijkstra(int s, int y, int b, bool ODWR){
for(int i = 1; i <= n; ++i) odw[i] = 0;
odw[y] = 1;
odl[b][s][y] = 0;
for(int x = 1; x <= n; ++x){
int u=0; ll dis = 2e09;
for(int j = 1; j <= n; ++j){
if(odw[j]) continue;
if(dis > odl[b][j][y]) u = j, dis = odl[b][j][y];
}
if(!u) return;
odw[u] = 1;
for(int j = 1; j <= n; ++j){
if(odw[j]) continue;
odl[b][j][y] = min(odl[b][j][y], odl[b][u][y] + C[u][j][ODWR]);
}
}
}
void read(int &a){
a = 0; char c = getchar_unlocked();
while(c<'0'||'9'<c) c = getchar_unlocked();
while('0'<=c&&c<='9') a = (a<<3)+(a<<1)+c-48, c = getchar_unlocked();
}
void readll(ll &a){
a = 0; char c = getchar_unlocked();
while(c<'0'||'9'<c) c = getchar_unlocked();
while('0'<=c&&c<='9') a = (a<<3)+(a<<1)+c-48, c = getchar_unlocked();
}
signed main(){
int m, a, b, c; ll d;
read(n); read(m);
for(int i = 0; i <= n; ++i)
for(int j = 0; j <= n; ++j) odl[0][i][j] = odl[1][i][j] = odl[2][i][j] = odl[3][i][j] = C[i][j][0] = C[i][j][1] = 2e09;
for(int i = 0; i < m; ++i){
read(a); read(b); read(c); readll(d);
kraw[i] = {a, b, c, d};
if(C[a][b][0] == 2e09) g[a][0].emplace_back(b);
if(C[b][a][1] == 2e09) g[b][1].emplace_back(a);
C[a][b][0] = min(C[a][b][0], c);
C[b][a][1] = min(C[b][a][1], c);
}
for(int j = 0; j <= n; ++j) odl[0][1][j] = odl[1][n][j] = odl[2][1][j] = odl[3][n][j] = 0;
for(int i = 2; i <= n; ++i){
if(g[i][1].empty()) continue;
dijkstra(1, i, 0, 0);
} dijkstra(1, 0, 0, 0);
for(int i = 1; i < n; ++i) {
if(g[i][1].empty()) continue;
dijkstra(n, i, 1, 0);
} dijkstra(n, 0, 1, 0);
for(int i = 2; i <= n; ++i) dijkstra(1, i, 2, 1);
for(int i = 1; i < n; ++i) dijkstra(n, i, 3, 1);
multiset<int> s[201][4]; //0. to z 1 do x, 1. to z n do x, 2. to z x do 1, 3. to z x do n
for(int i = 0; i < m; ++i){
a = kraw[i].a, b = kraw[i].b, c = kraw[i].c;
s[b][0].emplace(odl[0][a][b] + c);
s[b][1].emplace(odl[1][a][b] + c);
s[a][2].emplace(odl[2][b][a] + c);
s[a][3].emplace(odl[3][b][a] + c);
}
ll wynik = 2e09;
for(int i = 0; i < m; ++i){
a = kraw[i].a, b = kraw[i].b, c = kraw[i].c, d = kraw[i].d;
int tmp = c + odl[0][a][b];
multiset<int>::iterator it = s[b][0].find(tmp); s[b][0].erase(it);
tmp = c + odl[1][a][b];
it = s[b][1].find(tmp); s[b][1].erase(it);
tmp = c + odl[2][a][b]; s[b][2].emplace(tmp);
tmp = c + odl[3][a][b]; s[b][3].emplace(tmp);
ll t0 = 2e09, t1 = 2e09, t2 = *s[b][2].begin(), t3 = *s[b][3].begin();
if(s[b][0].size()) t0 = *s[b][0].begin();
if(s[b][1].size()) t1 = *s[b][1].begin();
if(b == 1) t0 = t2 = 0;
if(b == n) t1 = t3 = 0;
wynik = min(wynik, (ll) t0 + t3 + t1 + t2 + d);
wynik = min(wynik, (ll) t0 + t3 + odl[1][1][b] + d);
wynik = min(wynik, (ll) odl[0][n][b] + t1 + t2 + d);
tmp = c + odl[2][a][b];
it = s[b][2].find(tmp); s[b][2].erase(it);
tmp = c + odl[3][a][b];
it = s[b][3].find(tmp); s[b][3].erase(it);
tmp = c + odl[0][a][b]; s[b][0].emplace(tmp);
tmp = c + odl[1][a][b]; s[b][1].emplace(tmp);
}
wynik = min(wynik, (ll) odl[0][n][0] + odl[1][1][0]);
if(wynik != 2e09) printf("%lld\n", wynik);
else printf("-1");
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
1468 KB |
Output is correct |
2 |
Correct |
13 ms |
1308 KB |
Output is correct |
3 |
Correct |
148 ms |
1496 KB |
Output is correct |
4 |
Correct |
145 ms |
1468 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
33 ms |
1236 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
148 ms |
1476 KB |
Output is correct |
11 |
Correct |
149 ms |
1472 KB |
Output is correct |
12 |
Correct |
153 ms |
1464 KB |
Output is correct |
13 |
Correct |
71 ms |
1468 KB |
Output is correct |
14 |
Correct |
107 ms |
1476 KB |
Output is correct |
15 |
Correct |
109 ms |
1480 KB |
Output is correct |
16 |
Correct |
109 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
264 ms |
13028 KB |
Output is correct |
2 |
Correct |
245 ms |
13004 KB |
Output is correct |
3 |
Correct |
246 ms |
13212 KB |
Output is correct |
4 |
Correct |
155 ms |
1768 KB |
Output is correct |
5 |
Correct |
146 ms |
1456 KB |
Output is correct |
6 |
Correct |
50 ms |
1436 KB |
Output is correct |
7 |
Correct |
4 ms |
1364 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
165 ms |
13272 KB |
Output is correct |
10 |
Correct |
150 ms |
13128 KB |
Output is correct |
11 |
Correct |
201 ms |
13112 KB |
Output is correct |
12 |
Correct |
216 ms |
13180 KB |
Output is correct |
13 |
Correct |
203 ms |
13312 KB |
Output is correct |
14 |
Correct |
212 ms |
13156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
1472 KB |
Output is correct |
2 |
Correct |
51 ms |
1292 KB |
Output is correct |
3 |
Correct |
155 ms |
10412 KB |
Output is correct |
4 |
Correct |
33 ms |
1304 KB |
Output is correct |
5 |
Correct |
182 ms |
13136 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
226 ms |
13068 KB |
Output is correct |
9 |
Correct |
211 ms |
13092 KB |
Output is correct |
10 |
Correct |
193 ms |
12968 KB |
Output is correct |
11 |
Correct |
173 ms |
12940 KB |
Output is correct |
12 |
Correct |
179 ms |
13068 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
189 ms |
12940 KB |
Output is correct |
20 |
Correct |
207 ms |
12964 KB |
Output is correct |
21 |
Correct |
175 ms |
12940 KB |
Output is correct |
22 |
Correct |
189 ms |
12948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
1468 KB |
Output is correct |
2 |
Correct |
13 ms |
1308 KB |
Output is correct |
3 |
Correct |
148 ms |
1496 KB |
Output is correct |
4 |
Correct |
145 ms |
1468 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
33 ms |
1236 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
148 ms |
1476 KB |
Output is correct |
11 |
Correct |
149 ms |
1472 KB |
Output is correct |
12 |
Correct |
153 ms |
1464 KB |
Output is correct |
13 |
Correct |
71 ms |
1468 KB |
Output is correct |
14 |
Correct |
107 ms |
1476 KB |
Output is correct |
15 |
Correct |
109 ms |
1480 KB |
Output is correct |
16 |
Correct |
109 ms |
1484 KB |
Output is correct |
17 |
Correct |
264 ms |
13028 KB |
Output is correct |
18 |
Correct |
245 ms |
13004 KB |
Output is correct |
19 |
Correct |
246 ms |
13212 KB |
Output is correct |
20 |
Correct |
155 ms |
1768 KB |
Output is correct |
21 |
Correct |
146 ms |
1456 KB |
Output is correct |
22 |
Correct |
50 ms |
1436 KB |
Output is correct |
23 |
Correct |
4 ms |
1364 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
165 ms |
13272 KB |
Output is correct |
26 |
Correct |
150 ms |
13128 KB |
Output is correct |
27 |
Correct |
201 ms |
13112 KB |
Output is correct |
28 |
Correct |
216 ms |
13180 KB |
Output is correct |
29 |
Correct |
203 ms |
13312 KB |
Output is correct |
30 |
Correct |
212 ms |
13156 KB |
Output is correct |
31 |
Correct |
96 ms |
1472 KB |
Output is correct |
32 |
Correct |
51 ms |
1292 KB |
Output is correct |
33 |
Correct |
155 ms |
10412 KB |
Output is correct |
34 |
Correct |
33 ms |
1304 KB |
Output is correct |
35 |
Correct |
182 ms |
13136 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
226 ms |
13068 KB |
Output is correct |
39 |
Correct |
211 ms |
13092 KB |
Output is correct |
40 |
Correct |
193 ms |
12968 KB |
Output is correct |
41 |
Correct |
173 ms |
12940 KB |
Output is correct |
42 |
Correct |
179 ms |
13068 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
189 ms |
12940 KB |
Output is correct |
50 |
Correct |
207 ms |
12964 KB |
Output is correct |
51 |
Correct |
175 ms |
12940 KB |
Output is correct |
52 |
Correct |
189 ms |
12948 KB |
Output is correct |
53 |
Correct |
320 ms |
13276 KB |
Output is correct |
54 |
Correct |
338 ms |
13224 KB |
Output is correct |
55 |
Correct |
318 ms |
13212 KB |
Output is correct |
56 |
Correct |
151 ms |
1472 KB |
Output is correct |
57 |
Correct |
156 ms |
1516 KB |
Output is correct |
58 |
Correct |
250 ms |
10944 KB |
Output is correct |
59 |
Correct |
256 ms |
10808 KB |
Output is correct |
60 |
Correct |
289 ms |
10796 KB |
Output is correct |
61 |
Correct |
263 ms |
10828 KB |
Output is correct |
62 |
Correct |
272 ms |
10832 KB |
Output is correct |
63 |
Correct |
252 ms |
10760 KB |
Output is correct |
64 |
Correct |
252 ms |
10784 KB |
Output is correct |
65 |
Correct |
249 ms |
10760 KB |
Output is correct |
66 |
Correct |
203 ms |
10904 KB |
Output is correct |
67 |
Correct |
71 ms |
9664 KB |
Output is correct |
68 |
Correct |
188 ms |
13308 KB |
Output is correct |
69 |
Correct |
190 ms |
13228 KB |
Output is correct |
70 |
Correct |
246 ms |
13312 KB |
Output is correct |
71 |
Correct |
252 ms |
13204 KB |
Output is correct |
72 |
Correct |
244 ms |
13332 KB |
Output is correct |
73 |
Correct |
246 ms |
13216 KB |
Output is correct |
74 |
Correct |
255 ms |
13212 KB |
Output is correct |