#include <iostream>
#include <cstdio>
#include <vector>
#include <bitset>
#include <algorithm>
#define task "busline"
using namespace std;
using ll = long long;
using ld = long double;
const int N = 2e2 + 5;
const int M = 5e4 + 5;
const ll Inf = 1e17;
struct Edge
{
int u, v, c, d;
} e[M];
vector<int> adj[N], nadj[N];
int n, m;
int par[N];
ll d[7][N];
bool ok[N];
bitset<M> ck[7];
ll ans(Inf);
void Read()
{
cin >> n >> m;
for (int i = 1; i <= m; ++i)
{
cin >> e[i].u >> e[i].v >> e[i].c >> e[i].d;
adj[e[i].u].push_back(i);
nadj[e[i].v].push_back(i);
}
}
void Dijkstra(int x, vector<int> adj[N], ll d[N], bitset<M> &ck, int except)
{
for (int i = 0; i <= n; ++i)
{
d[i] = Inf;
ok[i] = 0;
par[i] = 0;
}
d[x] = 0;
while (1)
{
int v(0);
for (int i = 1; i <= n; ++i)
if (!ok[i] && d[v] > d[i])
v = i;
if (v == 0)
break;
ok[v] = 1;
for (auto i : adj[v])
if (i != except && d[e[i].v ^ e[i].u ^ v] > d[v] + e[i].c)
{
d[e[i].v ^ e[i].u ^ v] = d[v] + e[i].c;
par[e[i].v ^ e[i].u ^ v] = i;
}
}
for (int i = 1; i <= n; ++i)
if (i != x && d[i] != Inf)
ck[par[i]] = 1;
}
ll Proc(int i, int x, int y, const vector<int> &s)
{
int tmp1(s[0]), tmp2(s[3]);
if (ck[s[0]][i])
{
Dijkstra(x, adj, d[5], ck[5], i);
tmp1 = 5;
}
if (ck[s[3]][i])
{
Dijkstra(y, nadj, d[6], ck[6], i);
tmp2 = 6;
}
if (d[tmp1][e[i].v] == Inf || d[tmp2][e[i].u] == Inf)
return Inf;
ll res = d[tmp1][e[i].v] + e[i].c + e[i].d + d[tmp2][e[i].u];
tmp1 = s[2];
tmp2 = s[1];
if (ck[s[2]][i])
{
Dijkstra(y, adj, d[5], ck[5], i);
tmp1 = 5;
}
if (ck[s[1]][i])
{
Dijkstra(x, nadj, d[6], ck[6], i);
tmp2 = 6;
}
if ((d[tmp1][e[i].v] == Inf || d[tmp2][e[i].u] == Inf) && d[tmp1][x] == Inf)
return Inf;
res += min(d[tmp1][e[i].v] + e[i].c + d[tmp2][e[i].u], d[tmp1][x]);
return res;
}
void Solve()
{
Dijkstra(1, adj, d[1], ck[1], 0);
Dijkstra(1, nadj, d[2], ck[2], 0);
Dijkstra(n, adj, d[3], ck[3], 0);
Dijkstra(n, nadj, d[4], ck[4], 0);
if (d[1][n] != Inf && d[3][1] != Inf)
ans = d[1][n] + d[3][1];
for (int i = 1; i <= m; ++i)
{
ans = min(ans, Proc(i, 1, n, {1, 2, 3, 4}));
ans = min(ans, Proc(i, n, 1, {3, 4, 1, 2}));
}
cout << (ans == Inf ? -1 : ans);
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen(task ".INP", "r"))
{
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
Read();
Solve();
}
Compilation message
ho_t4.cpp: In function 'int32_t main()':
ho_t4.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
124 | freopen(task ".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:125:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
125 | freopen(task ".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
163 ms |
620 KB |
Output is correct |
2 |
Correct |
4 ms |
364 KB |
Output is correct |
3 |
Correct |
175 ms |
520 KB |
Output is correct |
4 |
Correct |
177 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
492 KB |
Output is correct |
6 |
Correct |
9 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
182 ms |
620 KB |
Output is correct |
11 |
Correct |
181 ms |
492 KB |
Output is correct |
12 |
Correct |
183 ms |
620 KB |
Output is correct |
13 |
Correct |
24 ms |
492 KB |
Output is correct |
14 |
Correct |
71 ms |
492 KB |
Output is correct |
15 |
Correct |
71 ms |
516 KB |
Output is correct |
16 |
Correct |
71 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
525 ms |
3052 KB |
Output is correct |
2 |
Correct |
519 ms |
2900 KB |
Output is correct |
3 |
Correct |
525 ms |
3092 KB |
Output is correct |
4 |
Correct |
185 ms |
492 KB |
Output is correct |
5 |
Correct |
146 ms |
492 KB |
Output is correct |
6 |
Correct |
21 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
82 ms |
3180 KB |
Output is correct |
10 |
Correct |
80 ms |
3052 KB |
Output is correct |
11 |
Correct |
210 ms |
3180 KB |
Output is correct |
12 |
Correct |
239 ms |
3052 KB |
Output is correct |
13 |
Correct |
213 ms |
3020 KB |
Output is correct |
14 |
Correct |
216 ms |
3052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
548 KB |
Output is correct |
2 |
Correct |
19 ms |
364 KB |
Output is correct |
3 |
Correct |
421 ms |
2284 KB |
Output is correct |
4 |
Correct |
10 ms |
364 KB |
Output is correct |
5 |
Correct |
494 ms |
2796 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
112 ms |
2796 KB |
Output is correct |
9 |
Correct |
118 ms |
2924 KB |
Output is correct |
10 |
Correct |
206 ms |
2944 KB |
Output is correct |
11 |
Correct |
205 ms |
2924 KB |
Output is correct |
12 |
Correct |
215 ms |
2796 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
204 ms |
2924 KB |
Output is correct |
20 |
Correct |
208 ms |
2924 KB |
Output is correct |
21 |
Correct |
204 ms |
2796 KB |
Output is correct |
22 |
Correct |
202 ms |
2796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
163 ms |
620 KB |
Output is correct |
2 |
Correct |
4 ms |
364 KB |
Output is correct |
3 |
Correct |
175 ms |
520 KB |
Output is correct |
4 |
Correct |
177 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
492 KB |
Output is correct |
6 |
Correct |
9 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
182 ms |
620 KB |
Output is correct |
11 |
Correct |
181 ms |
492 KB |
Output is correct |
12 |
Correct |
183 ms |
620 KB |
Output is correct |
13 |
Correct |
24 ms |
492 KB |
Output is correct |
14 |
Correct |
71 ms |
492 KB |
Output is correct |
15 |
Correct |
71 ms |
516 KB |
Output is correct |
16 |
Correct |
71 ms |
492 KB |
Output is correct |
17 |
Correct |
525 ms |
3052 KB |
Output is correct |
18 |
Correct |
519 ms |
2900 KB |
Output is correct |
19 |
Correct |
525 ms |
3092 KB |
Output is correct |
20 |
Correct |
185 ms |
492 KB |
Output is correct |
21 |
Correct |
146 ms |
492 KB |
Output is correct |
22 |
Correct |
21 ms |
364 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
82 ms |
3180 KB |
Output is correct |
26 |
Correct |
80 ms |
3052 KB |
Output is correct |
27 |
Correct |
210 ms |
3180 KB |
Output is correct |
28 |
Correct |
239 ms |
3052 KB |
Output is correct |
29 |
Correct |
213 ms |
3020 KB |
Output is correct |
30 |
Correct |
216 ms |
3052 KB |
Output is correct |
31 |
Correct |
165 ms |
548 KB |
Output is correct |
32 |
Correct |
19 ms |
364 KB |
Output is correct |
33 |
Correct |
421 ms |
2284 KB |
Output is correct |
34 |
Correct |
10 ms |
364 KB |
Output is correct |
35 |
Correct |
494 ms |
2796 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
112 ms |
2796 KB |
Output is correct |
39 |
Correct |
118 ms |
2924 KB |
Output is correct |
40 |
Correct |
206 ms |
2944 KB |
Output is correct |
41 |
Correct |
205 ms |
2924 KB |
Output is correct |
42 |
Correct |
215 ms |
2796 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
1 ms |
492 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
204 ms |
2924 KB |
Output is correct |
50 |
Correct |
208 ms |
2924 KB |
Output is correct |
51 |
Correct |
204 ms |
2796 KB |
Output is correct |
52 |
Correct |
202 ms |
2796 KB |
Output is correct |
53 |
Correct |
561 ms |
2920 KB |
Output is correct |
54 |
Correct |
546 ms |
2796 KB |
Output is correct |
55 |
Correct |
535 ms |
2796 KB |
Output is correct |
56 |
Correct |
181 ms |
492 KB |
Output is correct |
57 |
Correct |
176 ms |
492 KB |
Output is correct |
58 |
Correct |
458 ms |
2432 KB |
Output is correct |
59 |
Correct |
458 ms |
2396 KB |
Output is correct |
60 |
Correct |
460 ms |
2520 KB |
Output is correct |
61 |
Correct |
467 ms |
2412 KB |
Output is correct |
62 |
Correct |
461 ms |
2284 KB |
Output is correct |
63 |
Correct |
461 ms |
2412 KB |
Output is correct |
64 |
Correct |
756 ms |
2540 KB |
Output is correct |
65 |
Correct |
742 ms |
2540 KB |
Output is correct |
66 |
Correct |
671 ms |
2412 KB |
Output is correct |
67 |
Correct |
19 ms |
2408 KB |
Output is correct |
68 |
Correct |
85 ms |
3052 KB |
Output is correct |
69 |
Correct |
81 ms |
3052 KB |
Output is correct |
70 |
Correct |
221 ms |
3092 KB |
Output is correct |
71 |
Correct |
216 ms |
3052 KB |
Output is correct |
72 |
Correct |
236 ms |
3180 KB |
Output is correct |
73 |
Correct |
225 ms |
3052 KB |
Output is correct |
74 |
Correct |
217 ms |
3052 KB |
Output is correct |