이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000
#define M 200000
#define K 5
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
int ij[M], ww[M];
int *eh[N], eo[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;
}
long long *dp_;
int pq[N], iq[1 + N], cnt;
int lt(int i, int j) { return dp_[i] < dp_[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;
}
int main() {
static int gg[N];
static long long dp[1 << K][N];
int n, k, m, g, h, i, j, b;
long long ans;
scanf("%d%d%d", &n, &k, &m);
memset(gg, -1, n * sizeof *gg);
for (g = 0; g < k; g++) {
scanf("%d", &i), i--;
gg[i] = g;
}
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, &ww[h]), i--, j--;
ij[h] = i ^ j;
append(i, h), append(j, h);
}
for (b = 0; b < 1 << k; b++)
memset(dp[b], 0x3f, n * sizeof *dp[b]);
for (i = 0; i < n; i++)
dp[gg[i] == -1 ? 0 : 1 << gg[i]][i] = 0;
for (b = 0; b < 1 << k; b++) {
int c, b_;
dp_ = dp[b];
c = b, b_ = 0;
do
for (i = 0; i < n; i++)
dp_[i] = min(dp_[i], dp[b_][i] + dp[b ^ b_][i]);
while (b_ = b_ - c & c);
cnt = 0;
for (i = 0; i < n; i++)
pq_add_last(i);
for (h = cnt / 2; h > 0; h--)
pq_dn(iq[h]);
while (cnt) {
int o;
long long x;
i = pq_remove_first();
for (o = 0; o < eo[i]; o++) {
h = eh[i][o], j = i ^ ij[h], x = dp_[i] + ww[h];
if (dp_[j] > x)
dp_[j] = x, pq_up(j);
}
}
}
b = (1 << k) - 1;
ans = INF;
for (i = 0; i < n; i++)
ans = min(ans, dp[b][i]);
printf("%lld\n", ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
cities.c: In function 'append':
cities.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
19 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
cities.c: In function 'main':
cities.c:93:18: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
93 | while (b_ = b_ - c & c);
| ~~~^~~
cities.c:93:10: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
93 | while (b_ = b_ - c & c);
| ^~
cities.c:68:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d%d%d", &n, &k, &m);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
cities.c:71:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf("%d", &i), i--;
| ^~~~~~~~~~~~~~~
cities.c:77:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d%d%d", &i, &j, &ww[h]), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |