Submission #55749811
Source Code Expand
Copy
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <inttypes.h>#define MAX_N 212345struct edge_s {int to, cost;};int ec[MAX_N * 2];struct edge_s* es[MAX_N * 2];void ae(int from, int to, int cost) {es[from] = realloc(es[from], sizeof(*es[from]) * (ec[from] + 1));if (es[from] == NULL) exit(2);es[from][ec[from]].to = to;es[from][ec[from]].cost = cost;ec[from]++;}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#define MAX_N 212345
struct edge_s {
int to, cost;
};
int ec[MAX_N * 2];
struct edge_s* es[MAX_N * 2];
void ae(int from, int to, int cost) {
es[from] = realloc(es[from], sizeof(*es[from]) * (ec[from] + 1));
if (es[from] == NULL) exit(2);
es[from][ec[from]].to = to;
es[from][ec[from]].cost = cost;
ec[from]++;
}
int Q_cap = 0, Q_size = 0;
struct q_s {
int node;
int64_t cost;
} *Q;
void qadjust(int pos) {
for (;;) {
int minpos = pos;
int i;
for (i = 1; i <= 2; i++) {
int c = pos * 2 + i;
if (c < Q_size && Q[minpos].cost > Q[c].cost) minpos = c;
}
if (minpos == pos) {
if (pos == 0) return;
pos = (pos - 1) / 2;
} else {
struct q_s temp = Q[minpos];
Q[minpos] = Q[pos];
Q[pos] = temp;
pos = minpos;
}
}
}
void qadd(int node, int64_t cost) {
if (Q_size >= Q_cap) {
Q_cap = Q_size + 1;
Q = realloc(Q, sizeof(*Q) * Q_cap);
if (Q == NULL) exit(2);
}
Q[Q_size].node = node;
Q[Q_size].cost = cost;
Q_size++;
qadjust(Q_size - 1);
}
struct q_s qget(void) {
struct q_s ret;
if (Q_size <= 0) exit(99);
ret = Q[0];
Q_size--;
if (Q_size > 0) {
Q[0] = Q[Q_size];
qadjust(0);
}
return ret;
}
int64_t mincost[MAX_N * 2];
int main(void) {
int N, M;
int i;
if (scanf("%d%d", &N, &M) != 2) return 1;
Q_cap = N * 2 + M * 2;
Q = malloc(sizeof(*Q) * Q_cap);
if (Q == NULL) return 2;
for (i = 1; i <= N; i++) {
int A;
if (scanf("%d", &A) != 1) return 1;
ae(i, i + MAX_N, A);
}
for (i = 0; i < M; i++) {
int U, V, B;
if (scanf("%d%d%dd", &U, &V, &B) != 3) return 1;
ae(U + MAX_N, V, B);
ae(V + MAX_N, U, B);
}
memset(mincost, -1 ,sizeof(mincost));
mincost[1] = 0;
qadd(1, 0);
while (Q_size > 0) {
struct q_s cur = qget();
if (cur.cost == mincost[cur.node]) {
for (i = 0; i < ec[cur.node]; i++) {
int next_node = es[cur.node][i].to;
int64_t next_cost = cur.cost + es[cur.node][i].cost;
if (mincost[next_node] < 0 || mincost[next_node] > next_cost) {
mincost[next_node] = next_cost;
qadd(next_node, next_cost);
}
}
}
}
printf("%" PRId64, mincost[2 + MAX_N]);
for (i = 3; i <= N; i++) {
printf(" %" PRId64, mincost[i + MAX_N]);
}
putchar('\n');
return 0;
}
/*
各頂点を、入口と出口に分ける
入口→出口に、その頂点の重みをコストとする辺を張る
*/
Submission Info
| Submission Time | |
|---|---|
| Task | D - Shortest Path 3 |
| User | mikecat |
| Language | C (gcc 12.2.0) |
| Score | 425 |
| Code Size | 2519 Byte |
| Status | AC |
| Exec Time | 350 ms |
| Memory | 27816 KB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 425 / 425 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt |
| All | 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random-small_01.txt, 01_random-small_02.txt, 01_random-small_03.txt, 01_random-small_04.txt, 01_random-small_05.txt, 01_random-small_06.txt, 01_random-small_07.txt, 01_random-small_08.txt, 01_random-small_09.txt, 01_random-small_10.txt, 01_random-small_11.txt, 01_random-small_12.txt, 01_random-small_13.txt, 01_random-small_14.txt, 01_random-small_15.txt, 02_random-large_01.txt, 02_random-large_02.txt, 02_random-large_03.txt, 02_random-large_04.txt, 02_random-large_05.txt, 02_random-large_06.txt, 02_random-large_07.txt, 02_random-large_08.txt, 02_random-large_09.txt, 02_random-large_10.txt, 02_random-large_11.txt, 02_random-large_12.txt, 02_random-large_13.txt, 02_random-large_14.txt, 02_random-large_15.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt, 03_handmade_04.txt, 03_handmade_05.txt, 03_handmade_06.txt, 03_handmade_07.txt, 03_handmade_08.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_01.txt | AC | 2 ms | 4836 KB |
| 00_sample_02.txt | AC | 2 ms | 4920 KB |
| 00_sample_03.txt | AC | 2 ms | 4928 KB |
| 01_random-small_01.txt | AC | 3 ms | 5020 KB |
| 01_random-small_02.txt | AC | 3 ms | 4964 KB |
| 01_random-small_03.txt | AC | 3 ms | 5072 KB |
| 01_random-small_04.txt | AC | 3 ms | 5104 KB |
| 01_random-small_05.txt | AC | 62 ms | 8932 KB |
| 01_random-small_06.txt | AC | 14 ms | 5872 KB |
| 01_random-small_07.txt | AC | 9 ms | 5676 KB |
| 01_random-small_08.txt | AC | 44 ms | 7808 KB |
| 01_random-small_09.txt | AC | 2 ms | 5000 KB |
| 01_random-small_10.txt | AC | 20 ms | 6468 KB |
| 01_random-small_11.txt | AC | 6 ms | 5256 KB |
| 01_random-small_12.txt | AC | 13 ms | 5804 KB |
| 01_random-small_13.txt | AC | 64 ms | 8852 KB |
| 01_random-small_14.txt | AC | 3 ms | 5104 KB |
| 01_random-small_15.txt | AC | 18 ms | 6348 KB |
| 02_random-large_01.txt | AC | 167 ms | 14896 KB |
| 02_random-large_02.txt | AC | 325 ms | 24052 KB |
| 02_random-large_03.txt | AC | 10 ms | 5692 KB |
| 02_random-large_04.txt | AC | 324 ms | 24084 KB |
| 02_random-large_05.txt | AC | 135 ms | 13168 KB |
| 02_random-large_06.txt | AC | 316 ms | 24028 KB |
| 02_random-large_07.txt | AC | 228 ms | 17916 KB |
| 02_random-large_08.txt | AC | 323 ms | 24060 KB |
| 02_random-large_09.txt | AC | 99 ms | 11232 KB |
| 02_random-large_10.txt | AC | 325 ms | 24092 KB |
| 02_random-large_11.txt | AC | 308 ms | 23088 KB |
| 02_random-large_12.txt | AC | 322 ms | 24072 KB |
| 02_random-large_13.txt | AC | 280 ms | 21120 KB |
| 02_random-large_14.txt | AC | 332 ms | 24040 KB |
| 02_random-large_15.txt | AC | 88 ms | 10136 KB |
| 03_handmade_01.txt | AC | 225 ms | 23972 KB |
| 03_handmade_02.txt | AC | 201 ms | 23852 KB |
| 03_handmade_03.txt | AC | 350 ms | 27816 KB |
| 03_handmade_04.txt | AC | 73 ms | 10188 KB |
| 03_handmade_05.txt | AC | 213 ms | 16788 KB |
| 03_handmade_06.txt | AC | 204 ms | 16320 KB |
| 03_handmade_07.txt | AC | 2 ms | 4948 KB |
| 03_handmade_08.txt | AC | 2 ms | 4936 KB |