結果
| 問題 | No.1601 With Animals into Institute |
| コンテスト | |
| ユーザー |
👑 |
| 提出日時 | 2021-05-22 19:23:39 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 246 ms / 3,000 ms |
| コード長 | 2,046 bytes |
| コンパイル時間 | 214 ms |
| コンパイル使用メモリ | 31,104 KB |
| 実行使用メモリ | 15,360 KB |
| 最終ジャッジ日時 | 2024-07-02 02:02:13 |
| 合計ジャッジ時間 | 6,644 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
ソースコード
#include <stdio.h>
typedef struct List {
struct List *next;
int v, cost, x;
} list;
typedef struct {
long long key;
int id;
} data;
typedef struct {
data obj[800001];
int size;
} min_heap;
void push(data x, min_heap* h)
{
int i = ++(h->size), j = i >> 1;
data tmp;
h->obj[i] = x;
while (j > 0) {
if (h->obj[i].key < h->obj[j].key) {
tmp = h->obj[j];
h->obj[j] = h->obj[i];
h->obj[i] = tmp;
i = j;
j >>= 1;
} else break;
}
}
data pop(min_heap* h)
{
int i = 1, j = 2;
data output = h->obj[1], tmp;
h->obj[1] = h->obj[(h->size)--];
while (j <= h->size) {
if (j < h->size && h->obj[j^1].key < h->obj[j].key) j ^= 1;
if (h->obj[j].key < h->obj[i].key) {
tmp = h->obj[j];
h->obj[j] = h->obj[i];
h->obj[i] = tmp;
i = j;
j = i << 1;
} else break;
}
return output;
}
void solve(int N, list* adj[], int s, long long ans[])
{
int i, j, u, w, c;
const long long sup = 1LL << 60;
long long dist[2][100001];
list *p;
min_heap h;
data d;
h.size = 0;
for (u = 1; u <= N; u++) {
dist[0][u] = sup;
dist[1][u] = sup;
}
dist[0][s] = 0;
d.key = 0;
d.id = s;
push(d, &h);
while (h.size > 0) {
d = pop(&h);
i = (d.id - 1) / N;
u = d.id - N * i;
if (d.key != dist[i][u]) continue;
for (p = adj[u]; p != NULL; p = p->next) {
w = p->v;
c = p->cost;
j = i | p->x;
if (dist[j][w] > dist[i][u] + c) {
dist[j][w] = dist[i][u] + c;
d.key = dist[j][w];
d.id = w + N * j;
push(d, &h);
}
}
}
for (u = 1; u < N; u++) ans[u] = dist[1][u];
}
int main()
{
int i, N, M, u, w, c, x;
list *adj[100001] = {}, e[400001];
scanf("%d %d", &N, &M);
for (i = 0; i < M; i++) {
scanf("%d %d %d %d", &u, &w, &c, &x);
e[i*2].v = w;
e[i*2+1].v = u;
e[i*2].cost = c;
e[i*2+1].cost = c;
e[i*2].x = x;
e[i*2+1].x = x;
e[i*2].next = adj[u];
e[i*2+1].next = adj[w];
adj[u] = &(e[i*2]);
adj[w] = &(e[i*2+1]);
}
long long ans[100001];
solve(N, adj, N, ans);
for (u = 1; u < N; u++) printf("%lld\n", ans[u]);
fflush(stdout);
return 0;
}