#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <functional>
#define F first
#define S second
using namespace std;
typedef pair<int, int> ii;
typedef pair<long long, long long> ll;
typedef pair<long long, ii> yay;
typedef pair<ii, ll> edge;
const long long INF = 1000000000000000000;
int main() {
int N, M;
scanf("%d %d", &N, &M);
edge edges[M];
bool inr[M], inb[M], inrr[M], inrb[M];
fill_n(inr, M, 0);
fill_n(inb, M, 0);
fill_n(inrr, M, 0);
fill_n(inrb, M, 0);
vector<int> adjlist[N], revlist[N];
for (int i = 0; i < M; ++i) {
scanf("%d %d %lld %lld", &edges[i].F.F, &edges[i].F.S, &edges[i].S.F, &edges[i].S.S);
--edges[i].F.F, --edges[i].F.S;
adjlist[edges[i].F.F].push_back(i);
revlist[edges[i].F.S].push_back(i);
}
long long rsp[N], bsp[N], rrsp[N], rbsp[N];
fill_n(rsp, N, INF);
priority_queue< yay, vector<yay>, greater<yay> > dijkstra;
dijkstra.push(yay(0, ii(0, -1)));
rsp[0] = 0;
while (!dijkstra.empty()) {
yay a = dijkstra.top();
dijkstra.pop();
if (a.F > rsp[a.S.F]) continue;
if (a.S.S != -1) inr[a.S.S] = true;
for (int j: adjlist[a.S.F]) {
if (edges[j].S.F + a.F < rsp[edges[j].F.S]) {
rsp[edges[j].F.S] = edges[j].S.F + a.first;
dijkstra.push(yay(rsp[edges[j].F.S], ii(edges[j].F.S, j)));
}
}
}
fill_n(bsp, N, INF);
dijkstra.push(yay(0, ii(N - 1, -1)));
bsp[N - 1] = 0;
while (!dijkstra.empty()) {
yay a = dijkstra.top();
dijkstra.pop();
if (a.F > bsp[a.S.F]) continue;
if (a.S.S != -1) inb[a.S.S] = true;
for (int j: adjlist[a.S.F]) {
if (edges[j].S.F + a.F < bsp[edges[j].F.S]) {
bsp[edges[j].F.S] = edges[j].S.F + a.first;
dijkstra.push(yay(bsp[edges[j].F.S], ii(edges[j].F.S, j)));
}
}
}
fill_n(rrsp, N, INF);
dijkstra.push(yay(0, ii(0, -1)));
rrsp[0] = 0;
while (!dijkstra.empty()) {
yay a = dijkstra.top();
dijkstra.pop();
if (a.F > rrsp[a.S.F]) continue;
if (a.S.S != -1) inrr[a.S.S] = true;
for (int j: revlist[a.S.F]) {
if (edges[j].S.F + a.F < rrsp[edges[j].F.F]) {
rrsp[edges[j].F.F] = edges[j].S.F + a.first;
dijkstra.push(yay(rrsp[edges[j].F.F], ii(edges[j].F.F, j)));
}
}
}
fill_n(rbsp, N, INF);
dijkstra.push(yay(0, ii(N - 1, -1)));
rbsp[N - 1] = 0;
while (!dijkstra.empty()) {
yay a = dijkstra.top();
dijkstra.pop();
if (a.F > rbsp[a.S.F]) continue;
if (a.S.S != -1) inrb[a.S.S] = true;
for (int j: revlist[a.S.F]) {
if (edges[j].S.F + a.F < rbsp[edges[j].F.F]) {
rbsp[edges[j].F.F] = edges[j].S.F + a.first;
dijkstra.push(yay(rbsp[edges[j].F.F], ii(edges[j].F.F, j)));
}
}
}
long long ans = rsp[N - 1] + bsp[0];
for (int i = 0; i < M; ++i) {
long long a, b, c = edges[i].S.F, d = edges[i].S.F;
if (inr[i]) {
long long rsp[N];
fill_n(rsp, N, INF);
dijkstra.push(yay(0, ii(0, -1)));
rsp[0] = 0;
while (!dijkstra.empty()) {
yay a = dijkstra.top();
dijkstra.pop();
if (a.F > rsp[a.S.F]) continue;
for (int j: adjlist[a.S.F]) {
if (j == i) continue;
if (edges[j].S.F + a.F < rsp[edges[j].F.S]) {
rsp[edges[j].F.S] = edges[j].S.F + a.first;
dijkstra.push(yay(rsp[edges[j].F.S], ii(edges[j].F.S, j)));
}
}
if (a.S.F == edges[i].F.S) {
if (edges[i].S.F + a.F < rsp[edges[i].F.F]) {
rsp[edges[i].F.F] = edges[i].S.F + a.first;
dijkstra.push(yay(rsp[edges[i].F.F], ii(edges[i].F.F, i)));
}
}
}
a = rsp[N - 1], c += rsp[edges[i].F.S];
} else a = rsp[N - 1], c += rsp[edges[i].F.S];
if (inb[i]) {
long long bsp[N];
fill_n(bsp, N, INF);
dijkstra.push(yay(0, ii(N - 1, -1)));
bsp[N - 1] = 0;
while (!dijkstra.empty()) {
yay a = dijkstra.top();
dijkstra.pop();
if (a.F > bsp[a.S.F]) continue;
for (int j: adjlist[a.S.F]) {
if (j == i) continue;
if (edges[j].S.F + a.F < bsp[edges[j].F.S]) {
bsp[edges[j].F.S] = edges[j].S.F + a.first;
dijkstra.push(yay(bsp[edges[j].F.S], ii(edges[j].F.S, j)));
}
}
if (a.S.F == edges[i].F.S) {
if (edges[i].S.F + a.F < bsp[edges[i].F.F]) {
bsp[edges[i].F.F] = edges[i].S.F + a.first;
dijkstra.push(yay(bsp[edges[i].F.F], ii(edges[i].F.F, i)));
}
}
}
b = bsp[0], d += bsp[edges[i].F.S];
} else b = bsp[0], d += bsp[edges[i].F.S];
d += rrsp[edges[i].F.F];
c += rbsp[edges[i].F.F];
ans = min(ans, min(a, c) + min(b, d) + edges[i].S.S);
}
printf("%lld", ans < INF ? ans : -1);
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:16:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~~
ho_t4.cpp:25:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %lld %lld", &edges[i].F.F, &edges[i].F.S, &edges[i].S.F, &edges[i].S.S);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
17 ms |
384 KB |
Output is correct |
4 |
Correct |
18 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
23 ms |
384 KB |
Output is correct |
11 |
Correct |
23 ms |
384 KB |
Output is correct |
12 |
Correct |
22 ms |
384 KB |
Output is correct |
13 |
Correct |
7 ms |
384 KB |
Output is correct |
14 |
Correct |
12 ms |
384 KB |
Output is correct |
15 |
Correct |
12 ms |
384 KB |
Output is correct |
16 |
Correct |
12 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
2424 KB |
Output is correct |
2 |
Correct |
125 ms |
2424 KB |
Output is correct |
3 |
Correct |
133 ms |
2432 KB |
Output is correct |
4 |
Correct |
16 ms |
384 KB |
Output is correct |
5 |
Correct |
10 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
51 ms |
2296 KB |
Output is correct |
10 |
Correct |
48 ms |
2304 KB |
Output is correct |
11 |
Correct |
99 ms |
2304 KB |
Output is correct |
12 |
Correct |
90 ms |
2432 KB |
Output is correct |
13 |
Correct |
89 ms |
2296 KB |
Output is correct |
14 |
Correct |
86 ms |
2432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
78 ms |
1912 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
96 ms |
2424 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
40 ms |
2304 KB |
Output is correct |
9 |
Correct |
44 ms |
2296 KB |
Output is correct |
10 |
Correct |
70 ms |
2304 KB |
Output is correct |
11 |
Correct |
69 ms |
2304 KB |
Output is correct |
12 |
Correct |
72 ms |
2304 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
4 ms |
256 KB |
Output is correct |
15 |
Correct |
4 ms |
256 KB |
Output is correct |
16 |
Correct |
4 ms |
256 KB |
Output is correct |
17 |
Correct |
4 ms |
256 KB |
Output is correct |
18 |
Correct |
4 ms |
256 KB |
Output is correct |
19 |
Correct |
74 ms |
2432 KB |
Output is correct |
20 |
Correct |
72 ms |
2304 KB |
Output is correct |
21 |
Correct |
71 ms |
2304 KB |
Output is correct |
22 |
Correct |
76 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
17 ms |
384 KB |
Output is correct |
4 |
Correct |
18 ms |
384 KB |
Output is correct |
5 |
Correct |
6 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
23 ms |
384 KB |
Output is correct |
11 |
Correct |
23 ms |
384 KB |
Output is correct |
12 |
Correct |
22 ms |
384 KB |
Output is correct |
13 |
Correct |
7 ms |
384 KB |
Output is correct |
14 |
Correct |
12 ms |
384 KB |
Output is correct |
15 |
Correct |
12 ms |
384 KB |
Output is correct |
16 |
Correct |
12 ms |
384 KB |
Output is correct |
17 |
Correct |
134 ms |
2424 KB |
Output is correct |
18 |
Correct |
125 ms |
2424 KB |
Output is correct |
19 |
Correct |
133 ms |
2432 KB |
Output is correct |
20 |
Correct |
16 ms |
384 KB |
Output is correct |
21 |
Correct |
10 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
4 ms |
256 KB |
Output is correct |
25 |
Correct |
51 ms |
2296 KB |
Output is correct |
26 |
Correct |
48 ms |
2304 KB |
Output is correct |
27 |
Correct |
99 ms |
2304 KB |
Output is correct |
28 |
Correct |
90 ms |
2432 KB |
Output is correct |
29 |
Correct |
89 ms |
2296 KB |
Output is correct |
30 |
Correct |
86 ms |
2432 KB |
Output is correct |
31 |
Correct |
13 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
78 ms |
1912 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
96 ms |
2424 KB |
Output is correct |
36 |
Correct |
4 ms |
256 KB |
Output is correct |
37 |
Correct |
5 ms |
256 KB |
Output is correct |
38 |
Correct |
40 ms |
2304 KB |
Output is correct |
39 |
Correct |
44 ms |
2296 KB |
Output is correct |
40 |
Correct |
70 ms |
2304 KB |
Output is correct |
41 |
Correct |
69 ms |
2304 KB |
Output is correct |
42 |
Correct |
72 ms |
2304 KB |
Output is correct |
43 |
Correct |
4 ms |
256 KB |
Output is correct |
44 |
Correct |
4 ms |
256 KB |
Output is correct |
45 |
Correct |
4 ms |
256 KB |
Output is correct |
46 |
Correct |
4 ms |
256 KB |
Output is correct |
47 |
Correct |
4 ms |
256 KB |
Output is correct |
48 |
Correct |
4 ms |
256 KB |
Output is correct |
49 |
Correct |
74 ms |
2432 KB |
Output is correct |
50 |
Correct |
72 ms |
2304 KB |
Output is correct |
51 |
Correct |
71 ms |
2304 KB |
Output is correct |
52 |
Correct |
76 ms |
2296 KB |
Output is correct |
53 |
Correct |
141 ms |
2296 KB |
Output is correct |
54 |
Correct |
140 ms |
2296 KB |
Output is correct |
55 |
Correct |
139 ms |
2424 KB |
Output is correct |
56 |
Correct |
18 ms |
384 KB |
Output is correct |
57 |
Correct |
18 ms |
384 KB |
Output is correct |
58 |
Correct |
121 ms |
1912 KB |
Output is correct |
59 |
Correct |
125 ms |
1920 KB |
Output is correct |
60 |
Correct |
130 ms |
2040 KB |
Output is correct |
61 |
Correct |
120 ms |
1912 KB |
Output is correct |
62 |
Correct |
119 ms |
1920 KB |
Output is correct |
63 |
Correct |
127 ms |
1920 KB |
Output is correct |
64 |
Correct |
790 ms |
2304 KB |
Output is correct |
65 |
Correct |
715 ms |
3192 KB |
Output is correct |
66 |
Correct |
694 ms |
3068 KB |
Output is correct |
67 |
Correct |
25 ms |
2688 KB |
Output is correct |
68 |
Correct |
55 ms |
3456 KB |
Output is correct |
69 |
Correct |
54 ms |
3456 KB |
Output is correct |
70 |
Correct |
98 ms |
3448 KB |
Output is correct |
71 |
Correct |
97 ms |
3448 KB |
Output is correct |
72 |
Correct |
102 ms |
3448 KB |
Output is correct |
73 |
Correct |
108 ms |
3576 KB |
Output is correct |
74 |
Correct |
95 ms |
3448 KB |
Output is correct |