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 2000
#define M 2000
#define L 100000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
int ij[M * 2], cc[M], m;
int *eh[N], eo[N], n;
void append(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;
}
void solve(long long *dd, int u) {
static char visited[M * 2];
int h, h_, i, o;
memset(dd, 0x3f, m * 2 * sizeof *dd), dd[u] = cc[u >> 1];
memset(visited, 0, m * 2 * sizeof *visited);
while (1) {
h_ = -1;
for (h = 0; h < m * 2; h++)
if (!visited[h] && (h_ == -1 || dd[h_] > dd[h]))
h_ = h;
if (h_ == -1)
break;
visited[h_] = 1;
h = h_, i = ij[h ^ 1];
for (o = eo[i]; o--; ) {
h_ = eh[i][o];
if (!visited[h_] && (h ^ h_) != 1)
dd[h_] = min(dd[h_], dd[h] + cc[h_ >> 1]);
}
}
}
int main() {
static long long dd[M * 2][M * 2], dp[M * 2], dq[M * 2];
static int uu1[M * 2][N], uu2[M * 2][N], ii[L];
int q, l, g, h, i, j, t, t1, t2, u, v;
long long ans;
scanf("%d%d%d%d", &n, &m, &q, &l);
for (i = 0; i < n; i++)
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
for (h = 0; h < m; h++) {
scanf("%d%d%d", &i, &j, &cc[h]), i--, j--;
ij[h << 1 | 0] = i, ij[h << 1 | 1] = j;
append(i, h << 1 | 0), append(j, h << 1 | 1);
}
for (u = 0; u < m * 2; u++)
solve(dd[u], u);
for (g = 0; g < l; g++)
scanf("%d", &ii[g]), ii[g]--;
while (q--) {
scanf("%d%d", &g, &i), g--, i--;
ii[g] = i;
}
for (v = 0; v < m * 2; v++) {
memset(uu1[v], -1, n * sizeof *uu1[v]), memset(uu2[v], -1, n * sizeof *uu2[v]);
for (u = 0; u < m * 2; u++)
if (dd[u][v] != INF) {
i = ij[u];
if (uu1[v][i] == -1 || dd[uu1[v][i]][v] > dd[u][v])
uu2[v][i] = uu1[v][i], uu1[v][i] = u;
else if (uu2[v][i] == -1 || dd[uu2[v][i]][v] > dd[u][v])
uu2[v][i] = u;
}
}
for (g = 0; g + 1 < l; g++) {
i = ii[g], j = ii[g + 1];
memset(dq, 0x3f, m * 2 * sizeof *dq);
t1 = t2 = -1;
for (t = 0; t < m * 2; t++)
if (dp[t] != INF) {
if (t1 == -1 || dp[t1] > dp[t])
t2 = t1, t1 = t;
else if (t2 == -1 || dp[t2] > dp[t])
t2 = t;
}
for (v = 0; v < m * 2; v++)
if (ij[v ^ 1] == j && (u = uu1[v][i]) != -1) {
if (g == 0)
dq[v] = min(dq[v], dd[u][v]);
else {
if (t1 != -1 && (t1 ^ u) != 1)
dq[v] = min(dq[v], dp[t1] + dd[u][v]);
else if (t2 != -1)
dq[v] = min(dq[v], dp[t2] + dd[u][v]);
if ((u = uu2[v][i]) != -1) {
if (t1 != -1 && (t1 ^ u) != 1)
dq[v] = min(dq[v], dp[t1] + dd[u][v]);
else if (t2 != -1)
dq[v] = min(dq[v], dp[t2] + dd[u][v]);
}
}
}
memcpy(dp, dq, m * 2 * sizeof *dq);
}
ans = INF;
for (v = 0; v < m * 2; v++)
ans = min(ans, dp[v]);
printf("%lld\n", ans == INF ? -1 : ans);
return 0;
}
Compilation message (stderr)
wild_boar.c: In function 'append':
wild_boar.c:18:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
18 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
wild_boar.c: In function 'main':
wild_boar.c:52:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | scanf("%d%d%d%d", &n, &m, &q, &l);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.c:56:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d%d%d", &i, &j, &cc[h]), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.c:63:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d", &ii[g]), ii[g]--;
| ^~~~~~~~~~~~~~~~~~~
wild_boar.c:65:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | scanf("%d%d", &g, &i), g--, i--;
| ^~~~~~~~~~~~~~~~~~~~~
# | 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... |