# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
484581 | rainboy | Alias (COCI21_alias) | C11 | 13 ms | 8268 KiB |
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 1000
#define M 1000
#define L 20
#define INF 0x3f3f3f3f3f3f3f3fLL
char ss[N][L + 1];
int *ej[N], *ew[N], eo[N], n;
int idx(char *s) {
int i;
for (i = 0; i < n; i++)
if (strcmp(ss[i], s) == 0)
return i;
strcpy(ss[n], s);
ej[n] = (int *) malloc(2 * sizeof *ej[n]);
ew[n] = (int *) malloc(2 * sizeof *ew[n]);
return n++;
}
void append(int i, int j, int w) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0) {
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ew[i] = (int *) realloc(ew[i], o * 2 * sizeof *ew[i]);
}
ej[i][o] = j, ew[i][o] = w;
}
long long *dd_; int pq[N], iq[1 + N], cnt;
int lt(int i, int j) { return dd_[i] < dd_[j]; }
int p2(int p) {
return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}
void pq_up(int i) {
int p, q, j;
for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_dn(int i) {
int p, q, j;
for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
iq[pq[j] = p] = j;
iq[pq[i] = p] = i;
}
void pq_add_last(int i) {
iq[pq[i] = ++cnt] = i;
}
int pq_remove_first() {
int i = iq[1], j = iq[cnt--];
if (j != i)
pq[j] = 1, pq_dn(j);
pq[i] = 0;
return i;
}
void dijkstra(long long *dd, int i) {
memset(dd, 0x3f, n * sizeof *dd), dd_ = dd;
dd[i] = 0, pq_add_last(i);
while (cnt) {
int o;
i = pq_remove_first();
for (o = eo[i]; o--; ) {
int j = ej[i][o];
long long d = dd[i] + ew[i][o];
if (dd[j] > d) {
if (dd[j] == INF)
pq_add_last(j);
dd[j] = d, pq_up(j);
}
}
}
}
int main() {
static long long dd[N][N];
int m, q, h, i, j;
scanf("%d%d", &n, &m);
n = 0;
for (h = 0; h < m; h++) {
static char s[L + 1], t[L + 1];
int w;
scanf("%s%s%d", s, t, &w), i = idx(s), j = idx(t);
append(i, j, w);
}
scanf("%d", &q);
for (i = 0; i < n; i++)
dijkstra(dd[i], i);
while (q--) {
static char s[L + 1], t[L + 1];
scanf("%s%s", s, t), i = idx(s), j = idx(t);
if (dd[i][j] == INF)
printf("Roger\n");
else
printf("%lld\n", dd[i][j]);
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |