#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 200
#define M 50000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int ii[M], jj[M], ww[M], cc[M];
int *eh[N], eo[N], *fh[N], fo[N];
void append(int **eh, int *eo, int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
char visited[N]; int ff[N];
void dijkstra(int **eh, int *eo, int *dd, int n, int s, int h_) {
int i, i_, o;
memset(visited, 0, n * sizeof *visited);
memset(dd, 0x3f, n * sizeof *dd), memset(ff, -1, n * sizeof *ff), dd[s] = 0;
while (1) {
i_ = -1;
for (i = 0; i < n; i++)
if (!visited[i] && dd[i] != INF && (i_ == -1 || dd[i_] > dd[i]))
i_ = i;
if (i_ == -1)
break;
i = i_;
visited[i] = 1;
for (o = eo[i]; o--; ) {
int h = eh[i][o], j = i ^ ii[h] ^ jj[h], d = dd[i] + ww[h];
if (h == h_)
continue;
if (dd[j] > d)
dd[j] = d, ff[j] = h;
}
}
}
int main() {
static int ddso[N], ddto[N], ddsi[N], ddti[N], ddso_[N], ddto_[N], ddsi_[N], ddti_[N];
static char usedso[M], usedto[M], usedsi[M], usedti[M];
int n, m, h, i, j, dst, dts, ans;
scanf("%d%d", &n, &m);
for (i = 0; i < n; i++) {
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
fh[i] = (int *) malloc(2 * sizeof *fh[i]);
}
for (h = 0; h < m; h++) {
scanf("%d%d%d%d", &ii[h], &jj[h], &ww[h], &cc[h]), ii[h]--, jj[h]--;
append(eh, eo, ii[h], h), append(fh, fo, jj[h], h);
}
dijkstra(eh, eo, ddso, n, 0, -1);
for (i = 0; i < n; i++)
if (ff[i] != -1)
usedso[ff[i]] = 1;
dijkstra(eh, eo, ddto, n, n - 1, -1);
for (i = 0; i < n; i++)
if (ff[i] != -1)
usedto[ff[i]] = 1;
dijkstra(fh, fo, ddsi, n, 0, -1);
for (i = 0; i < n; i++)
if (ff[i] != -1)
usedsi[ff[i]] = 1;
dijkstra(fh, fo, ddti, n, n - 1, -1);
for (i = 0; i < n; i++)
if (ff[i] != -1)
usedti[ff[i]] = 1;
ans = INF;
if (ddso[n - 1] != INF && ddto[0] != INF)
ans = min(ans, ddso[n - 1] + ddto[0]);
for (h = 0; h < m; h++) {
if (usedso[h])
dijkstra(eh, eo, ddso_, n, 0, h);
else
memcpy(ddso_, ddso, n * sizeof *ddso);
if (usedto[h])
dijkstra(eh, eo, ddto_, n, n - 1, h);
else
memcpy(ddto_, ddto, n * sizeof *ddto);
if (usedsi[h])
dijkstra(fh, fo, ddsi_, n, 0, h);
else
memcpy(ddsi_, ddsi, n * sizeof *ddsi);
if (usedti[h])
dijkstra(fh, fo, ddti_, n, n - 1, h);
else
memcpy(ddti_, ddti, n * sizeof *ddti);
i = ii[h], j = jj[h];
dst = ddso_[n - 1];
if (ddso_[j] != INF && ddti_[i] != INF)
dst = min(dst, ddso_[j] + ww[h] + ddti_[i]);
dts = ddto_[0];
if (ddto_[j] != INF && ddsi_[i] != INF)
dts = min(dts, ddto_[j] + ww[h] + ddsi_[i]);
if (dst != INF && dts != INF)
ans = min(ans, dst + dts + cc[h]);
}
if (ans == INF)
ans = -1;
printf("%d\n", ans);
return 0;
}
Compilation message
ho_t4.c: In function 'append':
ho_t4.c:17:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
17 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
ho_t4.c: In function 'main':
ho_t4.c:54:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
ho_t4.c:60:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d%d%d%d", &ii[h], &jj[h], &ww[h], &cc[h]), ii[h]--, jj[h]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
70 ms |
368 KB |
Output is correct |
4 |
Correct |
82 ms |
368 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
73 ms |
340 KB |
Output is correct |
11 |
Correct |
72 ms |
340 KB |
Output is correct |
12 |
Correct |
73 ms |
340 KB |
Output is correct |
13 |
Correct |
15 ms |
340 KB |
Output is correct |
14 |
Correct |
42 ms |
380 KB |
Output is correct |
15 |
Correct |
43 ms |
376 KB |
Output is correct |
16 |
Correct |
42 ms |
372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
2896 KB |
Output is correct |
2 |
Correct |
178 ms |
2816 KB |
Output is correct |
3 |
Correct |
219 ms |
3260 KB |
Output is correct |
4 |
Correct |
76 ms |
428 KB |
Output is correct |
5 |
Correct |
57 ms |
364 KB |
Output is correct |
6 |
Correct |
12 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
53 ms |
3108 KB |
Output is correct |
10 |
Correct |
55 ms |
3028 KB |
Output is correct |
11 |
Correct |
115 ms |
3112 KB |
Output is correct |
12 |
Correct |
115 ms |
3112 KB |
Output is correct |
13 |
Correct |
136 ms |
3028 KB |
Output is correct |
14 |
Correct |
139 ms |
3204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
340 KB |
Output is correct |
2 |
Correct |
10 ms |
340 KB |
Output is correct |
3 |
Correct |
151 ms |
2268 KB |
Output is correct |
4 |
Correct |
5 ms |
340 KB |
Output is correct |
5 |
Correct |
177 ms |
2860 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
51 ms |
2848 KB |
Output is correct |
9 |
Correct |
54 ms |
2792 KB |
Output is correct |
10 |
Correct |
144 ms |
2856 KB |
Output is correct |
11 |
Correct |
114 ms |
2860 KB |
Output is correct |
12 |
Correct |
127 ms |
2888 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
284 KB |
Output is correct |
16 |
Correct |
1 ms |
288 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
284 KB |
Output is correct |
19 |
Correct |
139 ms |
3020 KB |
Output is correct |
20 |
Correct |
132 ms |
2800 KB |
Output is correct |
21 |
Correct |
128 ms |
2864 KB |
Output is correct |
22 |
Correct |
115 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
70 ms |
368 KB |
Output is correct |
4 |
Correct |
82 ms |
368 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
288 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
73 ms |
340 KB |
Output is correct |
11 |
Correct |
72 ms |
340 KB |
Output is correct |
12 |
Correct |
73 ms |
340 KB |
Output is correct |
13 |
Correct |
15 ms |
340 KB |
Output is correct |
14 |
Correct |
42 ms |
380 KB |
Output is correct |
15 |
Correct |
43 ms |
376 KB |
Output is correct |
16 |
Correct |
42 ms |
372 KB |
Output is correct |
17 |
Correct |
213 ms |
2896 KB |
Output is correct |
18 |
Correct |
178 ms |
2816 KB |
Output is correct |
19 |
Correct |
219 ms |
3260 KB |
Output is correct |
20 |
Correct |
76 ms |
428 KB |
Output is correct |
21 |
Correct |
57 ms |
364 KB |
Output is correct |
22 |
Correct |
12 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
53 ms |
3108 KB |
Output is correct |
26 |
Correct |
55 ms |
3028 KB |
Output is correct |
27 |
Correct |
115 ms |
3112 KB |
Output is correct |
28 |
Correct |
115 ms |
3112 KB |
Output is correct |
29 |
Correct |
136 ms |
3028 KB |
Output is correct |
30 |
Correct |
139 ms |
3204 KB |
Output is correct |
31 |
Correct |
60 ms |
340 KB |
Output is correct |
32 |
Correct |
10 ms |
340 KB |
Output is correct |
33 |
Correct |
151 ms |
2268 KB |
Output is correct |
34 |
Correct |
5 ms |
340 KB |
Output is correct |
35 |
Correct |
177 ms |
2860 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
51 ms |
2848 KB |
Output is correct |
39 |
Correct |
54 ms |
2792 KB |
Output is correct |
40 |
Correct |
144 ms |
2856 KB |
Output is correct |
41 |
Correct |
114 ms |
2860 KB |
Output is correct |
42 |
Correct |
127 ms |
2888 KB |
Output is correct |
43 |
Correct |
0 ms |
212 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
284 KB |
Output is correct |
46 |
Correct |
1 ms |
288 KB |
Output is correct |
47 |
Correct |
1 ms |
212 KB |
Output is correct |
48 |
Correct |
0 ms |
284 KB |
Output is correct |
49 |
Correct |
139 ms |
3020 KB |
Output is correct |
50 |
Correct |
132 ms |
2800 KB |
Output is correct |
51 |
Correct |
128 ms |
2864 KB |
Output is correct |
52 |
Correct |
115 ms |
2772 KB |
Output is correct |
53 |
Correct |
212 ms |
2916 KB |
Output is correct |
54 |
Correct |
211 ms |
2884 KB |
Output is correct |
55 |
Correct |
196 ms |
2892 KB |
Output is correct |
56 |
Correct |
74 ms |
468 KB |
Output is correct |
57 |
Correct |
72 ms |
388 KB |
Output is correct |
58 |
Correct |
163 ms |
2392 KB |
Output is correct |
59 |
Correct |
185 ms |
2388 KB |
Output is correct |
60 |
Correct |
165 ms |
2388 KB |
Output is correct |
61 |
Correct |
166 ms |
2380 KB |
Output is correct |
62 |
Correct |
162 ms |
2304 KB |
Output is correct |
63 |
Correct |
160 ms |
2376 KB |
Output is correct |
64 |
Correct |
291 ms |
2464 KB |
Output is correct |
65 |
Correct |
292 ms |
2468 KB |
Output is correct |
66 |
Correct |
244 ms |
2452 KB |
Output is correct |
67 |
Correct |
16 ms |
2152 KB |
Output is correct |
68 |
Correct |
65 ms |
3120 KB |
Output is correct |
69 |
Correct |
58 ms |
3020 KB |
Output is correct |
70 |
Correct |
133 ms |
3088 KB |
Output is correct |
71 |
Correct |
124 ms |
2988 KB |
Output is correct |
72 |
Correct |
139 ms |
3092 KB |
Output is correct |
73 |
Correct |
123 ms |
3120 KB |
Output is correct |
74 |
Correct |
123 ms |
3128 KB |
Output is correct |