結果
| 問題 |
No.807 umg tours
|
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2019-03-22 22:11:46 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 347 ms / 4,000 ms |
| コード長 | 4,374 bytes |
| コンパイル時間 | 1,278 ms |
| コンパイル使用メモリ | 31,488 KB |
| 実行使用メモリ | 21,760 KB |
| 最終ジャッジ日時 | 2024-11-23 19:01:53 |
| 合計ジャッジ時間 | 5,691 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>
#include<string.h>
typedef struct binaryHeap{
void *h;
size_t heapSize;
size_t maxSize;
size_t size;
int (*cmp)(const void *,const void *);
} heap;
heap* newHeap(const size_t size,int (*cmpF)(const void *,const void *)){
const size_t maxSize=1;
heap *res=(heap *)malloc(sizeof(heap));
res->h=malloc(size*(maxSize+1));
res->heapSize=0;
res->maxSize=maxSize;
res->size=size;
res->cmp=cmpF;
return res;
}
int isEmpty(const heap *h){
return h->heapSize==0;
}
void freeHeap(heap *h){
free(h->h);
free(h);
return;
}
void initHeap(heap *h){
h->heapSize=0;
return;
}
static inline void heapFunc_swap(void * restrict a,void * restrict b,void * restrict tmp,size_t size){
memcpy(tmp,a,size);
memcpy(a,b,size);
memcpy(b,tmp,size);
return;
}
void push(heap *q,const void *p){
if(q->heapSize==q->maxSize){
q->maxSize=2*q->maxSize+1;
q->h=realloc(q->h,q->size*(q->maxSize+1));
}
q->heapSize+=1;
char *h=(char *)(q->h);
int k=q->heapSize;
const size_t size=q->size;
int (*cmp)(const void *,const void *)=q->cmp;
memcpy(h+k*size,p,size);
while(k>1){
size_t parent=k/2;
if(cmp(h+parent*size,h+k*size)<=0) return;
heapFunc_swap(h+parent*size,h+k*size,h,size);
k=parent;
}
return;
}
void pop(heap *q,void *res){
char *h=(char *)(q->h);
const size_t size=q->size;
if(res!=NULL) memcpy(res,h+size,size);
memcpy(h+size,h+size*q->heapSize,size);
q->heapSize-=1;
int (*cmp)(const void *,const void *)=q->cmp;
const size_t n=q->heapSize;
size_t k=1;
while(2*k+1<=n){
size_t next=cmp(h+2*k*size,h+(2*k+1)*size)<=0?2*k:2*k+1;
if(cmp(h+k*size,h+next*size)<=0) return;
heapFunc_swap(h+k*size,h+next*size,h,size);
k=next;
}
if(2*k<=n && cmp(h+k*size,h+2*k*size)>0) heapFunc_swap(h+k*size,h+2*k*size,h,size);
return;
}
void top(const heap *q,void *res){
memcpy(res,(char *)q->h+q->size,q->size);
return;
}
typedef struct directed_edge {
int32_t vertex;
int32_t cost;
int32_t next;
} graph_edge;
typedef struct directedGraph {
graph_edge *edge;
int32_t *start;
int32_t pointer;
int32_t vertex_num;
int32_t max_size;
} graph;
graph* newGraph (const int vertex_num) {
graph *g = (graph *) calloc (1, sizeof (graph));
g->edge = (graph_edge *) calloc (1, sizeof (graph_edge));
g->start = (int32_t *) calloc (vertex_num, sizeof (int32_t));
g->pointer = 0;
g->vertex_num = vertex_num;
g->max_size = 1;
for (int32_t i = 0; i < vertex_num; ++i) {
g->start[i] = -1;
}
return g;
}
void freeGraph (graph *g) {
free (g->edge);
free (g->start);
free (g);
return;
}
void addEdge (graph *g, int32_t from, int32_t to, int32_t cost) {
if (g->pointer == g->max_size) {
g->max_size *= 2;
g->edge = (graph_edge *) realloc (g->edge, sizeof (graph_edge) * g->max_size);
}
g->edge[g->pointer] = (graph_edge) {to, cost, g->start[from]};
g->start[from] = g->pointer++;
}
typedef int32_t i32;
typedef int64_t i64;
typedef struct node {
i32 v;
i64 d;
} node;
int cmpNode (const void *a, const void *b) {
i64 d = ((node *)a)->d - ((node *)b)->d;
return d == 0 ? 0 : d < 0 ? -1 : 1;
}
void run (void) {
i32 n, m;
scanf("%" SCNi32 "%" SCNi32, &n, &m);
graph *g = newGraph (2 * n);
while (m--) {
i32 a, b, c;
scanf("%" SCNi32 "%" SCNi32 "%" SCNi32, &a, &b, &c);
a--;
b--;
addEdge (g, a, b, c);
addEdge (g, b, a, c);
addEdge (g, a + n, b + n, c);
addEdge (g, b + n, a + n, c);
addEdge (g, a, b + n, 0);
addEdge (g, b, a + n, 0);
}
i64 *dp = (i64 *) calloc (2 * n, sizeof (i64));
uint8_t *used = (uint8_t *) calloc (2 * n, sizeof (uint8_t));
for (i32 i = 1; i < n; ++i) {
dp[i] = (i64) 1000000000 * n;
dp[i + n] = dp[i];
}
heap *h = newHeap (sizeof (node), cmpNode);
push (h, &((node){0, 0}));
while (!isEmpty (h)) {
node t;
pop (h, &t);
const i32 v = t.v;
if (used[v]) continue;
used[v] = 1;
for (i32 p = g->start[v]; p != -1; p = g->edge[p].next) {
i32 u = g->edge[p].vertex;
i64 d = t.d + g->edge[p].cost;
if (d >= dp[u]) continue;
dp[u] = d;
push (h, &((node){u, d}));
}
}
for (i32 i = 0; i < n; ++i) {
printf("%" PRIi64 "\n", dp[i] + dp[i + n]);
}
}
int main (void) {
run ();
return 0;
}
akakimidori