結果
| 問題 |
No.807 umg tours
|
| コンテスト | |
| ユーザー |
akakimidori
|
| 提出日時 | 2019-03-23 11:21:34 |
| 言語 | C (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 366 ms / 4,000 ms |
| コード長 | 4,412 bytes |
| コンパイル時間 | 324 ms |
| コンパイル使用メモリ | 33,140 KB |
| 実行使用メモリ | 28,032 KB |
| 最終ジャッジ日時 | 2024-11-23 19:17:59 |
| 合計ジャッジ時間 | 5,225 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>
#include<string.h>
typedef struct pairingHeapNode{
struct pairingHeapNode *next;
struct pairingHeapNode *child;
unsigned long long int val[];
} heapNode;
typedef struct pairingHeap{
heapNode *root;
int heapSize;
size_t size;
int (*cmp)(const void *,const void *);
} heap;
heapNode* newHeapNode(const void *v,const size_t size){
heapNode *res=(heapNode *)malloc(sizeof(heapNode)+size);
memcpy(res->val,v,size);
res->next=NULL;
res->child=NULL;
return res;
}
void freeAllNode(heapNode *t){
if(t==NULL) return;
freeAllNode(t->next);
freeAllNode(t->child);
free(t);
return;
}
heap* newHeap(const size_t size,int (*cmp)(const void *,const void *)){
heap *res=(heap *)malloc(sizeof(heap));
res->root=NULL;
res->heapSize=0;
res->size=size;
res->cmp=cmp;
return res;
}
void freeHeap(heap *h){
freeAllNode(h->root);
free(h);
return;
}
int getSize(const heap *h){
return h->heapSize;
}
int isEmpty(const heap *h){
return getSize(h)==0;
}
heapNode* meld(heapNode *a,heapNode *b,int (*cmp)(const void *a,const void *b)){
if(a==NULL) return b;
if(b==NULL) return a;
if(cmp(a->val,b->val)<=0){
b->next=a->child;
a->child=b;
return a;
} else {
a->next=b->child;
b->child=a;
return b;
}
}
void push(heap *h,const void *v){
heapNode *p=newHeapNode(v,h->size);
h->heapSize++;
h->root=meld(h->root,p,h->cmp);
return;
}
void pop(heap *h,void *v){
if(v!=NULL) memcpy(v,h->root->val,h->size);
heapNode *lst=h->root->child;
int (*cmp)(const void *a,const void *b)=h->cmp;
free(h->root);
h->heapSize--;
heapNode *r=NULL;
while(lst!=NULL && lst->next!=NULL){
heapNode *a=lst;
heapNode *b=lst->next;
heapNode *next=lst->next->next;
a->next=b->next=NULL;
r=meld(r,meld(a,b,cmp),cmp);
lst=next;
}
if(lst!=NULL) r=meld(r,lst,cmp);
h->root=r;
return;
}
void top(const heap *h,void *v){
memcpy(v,h->root->val,h->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