結果

問題 No.807 umg tours
ユーザー akakimidoriakakimidori
提出日時 2019-03-22 22:11:46
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 361 ms / 4,000 ms
コード長 4,374 bytes
コンパイル時間 1,215 ms
コンパイル使用メモリ 31,236 KB
実行使用メモリ 22,152 KB
最終ジャッジ日時 2023-08-15 11:59:35
合計ジャッジ時間 6,254 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 0 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 216 ms
15,928 KB
testcase_12 AC 200 ms
14,436 KB
testcase_13 AC 272 ms
17,112 KB
testcase_14 AC 110 ms
8,716 KB
testcase_15 AC 75 ms
7,140 KB
testcase_16 AC 277 ms
17,672 KB
testcase_17 AC 361 ms
21,452 KB
testcase_18 AC 351 ms
21,028 KB
testcase_19 AC 334 ms
20,024 KB
testcase_20 AC 159 ms
11,140 KB
testcase_21 AC 167 ms
12,976 KB
testcase_22 AC 58 ms
6,116 KB
testcase_23 AC 43 ms
5,080 KB
testcase_24 AC 142 ms
17,940 KB
testcase_25 AC 359 ms
22,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0