This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |