Submission #47792
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | #include <stdio.h> #include <limits.h> #define max(X, Y) ((X) > (Y) ? (X) : (Y)) #define min(X, Y) ((X) < (Y) ? (X) : (Y)) //#include "vector.h" //#include "heap.h" #ifndef EI1710_VECTOR_H #define EI1710_VECTOR_H #include <stddef.h> #include <stdlib.h> #include <string.h> #include <stdio.h> typedef unsigned char Byte; //vector実現手法 //memcopy(), memmove()でオブジェクトをコピー //要素へのアクセスはsizeof(Type) * index バイト分先の要素を見て実現 //あらかじめ大き目に領域を取っておき、キャパシティオーバーしたらrealloc()する typedef struct { Byte *storage; //配列領域 size_t data_size; //格納データ型のサイズ size_t array_size; //格納データの個数 size_t capacity; //realloc()せずに格納できるデータ数 } Vector; //vectorの初期化 int Vector_init(Vector *vec, size_t data_size, size_t vec_size); //vector末尾にデータを追加 int Vector_push_back(Vector *vec, void *data); //vectorが管理するデータにアクセス void *Vector_array(Vector *vec, size_t i); //末尾のデータを削除 int Vector_pop_back(Vector *vec); //vectorをクリア void Vector_clear(Vector *vec); //vectorに格納しているデータの個数を返す size_t Vector_size(Vector *vec); //キャパシティを返す size_t Vector_capacity(Vector *vec); //vectorの初期化 int Vector_init(Vector *vec, size_t data_size, size_t vec_size) { vec->data_size = data_size; vec->capacity = 0; vec->array_size = 0; if (vec_size > 0) { vec->storage = calloc (vec_size, data_size); if (vec->storage == NULL) { return 0; } else { vec->capacity = vec_size; vec->array_size = vec_size; } } else { vec->storage = NULL; } return 1; } //vector末尾にデータを追加 int Vector_push_back(Vector *vec, void *data) { size_t indx = vec->array_size; size_t new_capacity = max(vec->capacity + 1, vec->capacity * 2); Byte *new_storage; //配列の再確保 if (indx >= vec->capacity) { new_storage = realloc (vec->storage, new_capacity * vec->data_size); if (new_storage == NULL) { return 0; } else { vec->storage = new_storage; vec->capacity = new_capacity; } } memcpy (vec->storage + (indx * vec->data_size), data, vec->data_size); vec->array_size = vec->array_size + 1; return 1; } //vectorが管理するデータにアクセス void *Vector_array(Vector *vec, size_t i) { if (i >= vec->array_size || i < 0) { return NULL; } else { return (vec->storage + (vec->data_size * i)); } } //vector末尾の要素を削除 int Vector_pop_back(Vector *vec) { if (vec->array_size == 0) { return 0; } else { vec->array_size--; } return 1; } //vectorをクリア void Vector_clear(Vector *vec) { free (vec->storage); Vector_init(vec, vec->data_size, 0); return ; } //vectorに格納しているデータの個数を返す size_t Vector_size(Vector *vec) { return vec->array_size; } //キャパシティを返す size_t Vector_capacity(Vector *vec) { return vec->capacity; } #endif #ifndef EI1710_Queue_H #define EI1710_Queue_H #include <stdio.h> #include <stdlib.h> #include <string.h> //線形リストでQueueを実装する //リストの先頭からデータを取り出し、 //リストの末尾にデータを追加していく typedef struct que_cell{ void *data; //実データへのポインタ struct que_cell *next; //次セル } QueCell; typedef struct queue { QueCell *head; //キューの先頭(データの取り出し位置) QueCell *last; //キューの末尾(データの格納位置) size_t data_size; //格納データの大きさ size_t que_length; //格納データの個数 = キューの大きさ } Queue; //Queue構造体の初期化を行う //q:初期化するQueue構造体へのポインタ //data_size:格納するデータの大きさ //Queueを使う前に必ず呼び出すこと! // ex) int型を格納する場合) Queue_init(&que, sizeof(int)); void Queue_init( struct queue *q, const size_t data_size); //enque //q:操作対象のQueueへのポインタ //data:格納データへのポインタ //正しく格納できれば正数 エラーが起きたとき0を返す int Queue_enque( struct queue *q, void *data); //deque //q:操作対象のQueueへのポインタ //data:取り出したデータの格納場所 //キューが空(エラー)のとき0, 正常に取り出したとき正数を返す //dataが指す領域がデータを格納するのに足らない場合の動作は不定である int Queue_deque( struct queue *q, void *data); //キューに格納されているデータ数を返す size_t Queue_size( struct queue *q); //Queueをクリアする //Queueを使い終わったら必ず呼び出すこと! void Queue_clear( struct queue *q); void Queue_cell_remove(QueCell * remove ); QueCell *Queue_cell_create( void *data, size_t data_size); void Queue_cell_remove(QueCell * remove ) { free ( remove ->data); free ( remove ); return ; } QueCell *Queue_cell_create( void *data, size_t data_size) { QueCell *cell = malloc ( sizeof (QueCell)); if (cell == NULL) { return NULL; } cell->data = malloc (data_size); if (cell->data == NULL) { free (cell); return NULL; } memcpy (cell->data, data, data_size); cell->next = NULL; return cell; } void Queue_init( struct queue *q, const size_t data_size) { q->head = NULL; q->last = NULL; q->data_size = data_size; q->que_length = 0; return ; } int Queue_enque( struct queue *q, void *data) { QueCell *new_data = Queue_cell_create(data, q->data_size); if (new_data == NULL) { return 0; } //セル追加 if (q->que_length <= 0) { q->last = new_data; q->head = new_data; } else { q->last->next = new_data; q->last = new_data; } q->que_length = q->que_length + 1; return 1; } int Queue_deque( struct queue *q, void *data) { if (q->que_length > 0) { //データコピー if (data != NULL) { memcpy (data, q->head->data, q->data_size); } //セルを解放 QueCell * remove = q->head; q->head = q->head->next; Queue_cell_remove( remove ); q->que_length = q->que_length - 1; return 1; } else { return 0; } } size_t Queue_size( struct queue *q) { return q->que_length; } void Queue_clear( struct queue *q) { //空になるまでdeque() while (Queue_size(q)) { Queue_deque(q, NULL); } return ; } #endif // INSERT ABOVE HERE //typedef long long ll; typedef struct { int to; int cost; } Edge; typedef struct { int pos; int cost; } State; const int inf = (INT_MAX / 2); Vector graph[100005]; int min_cost[100005]; void Dijkstra( void ); int main() { //init for ( int i = 0; i < 100005; i++) { Vector_init(&graph[i], sizeof (Edge), 0); min_cost[i] = inf; } int n, m; int x, y; int cost; //input scanf ( "%d %d" , &n, &m); for ( int i = 0; i < m; i++) { scanf ( "%d %d %d" , &x, &y, &cost); Vector_push_back(&graph[x], &(Edge){y, cost}); Vector_push_back(&graph[y], &(Edge){x, cost}); } Dijkstra(); if (min_cost[n] == inf) { puts ( "NA" ); } else { printf ( "%d\n" , min_cost[n]); } return 0; } void Dijkstra( void ) { Queue que; Queue_init(&que, sizeof (State)); Queue_enque(&que, &(State){.cost = 0, .pos = 1}); min_cost[1] = 0; while (Queue_size(&que) > 0) { State tmp; Queue_deque(&que, &tmp); int pos = tmp.pos; int cost = tmp.cost; for ( int i = 0; i < Vector_size(&graph[pos]); i++) { int npos = ((Edge *)Vector_array(&graph[pos], i))->to; int ncost = cost + ((Edge *)Vector_array(&graph[pos], i))->cost; if (min_cost[npos] > ncost) { min_cost[npos] = ncost; Queue_enque(&que, &(State){.pos = npos, .cost = ncost}); } } } return ; } |
ステータス
項目 | データ |
---|---|
問題 | 0431 - 君も始めようダイクストラ大好き厨 |
ユーザー名 | ei1710 |
投稿日時 | 2019-03-27 17:03:44 |
言語 | C |
状態 | Accepted |
得点 | 1 |
ソースコード長 | 8090 Byte |
最大実行時間 | 146 ms |
最大メモリ使用量 | 9692 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 1 / 1 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
m_in1.txt | AC | 146 ms | 9692 KB |
1
|
r_in1.txt | AC | 41 ms | 5244 KB |
1
|
r_in2.txt | AC | 33 ms | 5356 KB |
1
|
r_in3.txt | AC | 29 ms | 5364 KB |
1
|
r_in4.txt | AC | 23 ms | 4428 KB |
1
|
r_in5.txt | AC | 32 ms | 5552 KB |
1
|
r_in6.txt | AC | 33 ms | 5200 KB |
1
|
r_in7.txt | AC | 21 ms | 4668 KB |
1
|
r_in8.txt | AC | 28 ms | 4632 KB |
1
|
r_in9.txt | AC | 41 ms | 5672 KB |
1
|
r_in10.txt | AC | 25 ms | 4304 KB |
1
|
r_in11.txt | AC | 37 ms | 5156 KB |
1
|
r_in12.txt | AC | 33 ms | 5180 KB |
1
|
r_in13.txt | AC | 33 ms | 5144 KB |
1
|
r_in14.txt | AC | 34 ms | 5392 KB |
1
|
r_in15.txt | AC | 35 ms | 5452 KB |
1
|
r_in16.txt | AC | 32 ms | 4592 KB |
1
|
r_in17.txt | AC | 34 ms | 4680 KB |
1
|
r_in18.txt | AC | 34 ms | 4716 KB |
1
|
r_in19.txt | AC | 37 ms | 4456 KB |
1
|
r_in20.txt | AC | 27 ms | 4628 KB |
1
|
r_in21.txt | AC | 19 ms | 4288 KB |
1
|
r_in22.txt | AC | 29 ms | 4384 KB |
1
|
r_in23.txt | AC | 23 ms | 4476 KB |
1
|
r_in24.txt | AC | 26 ms | 4612 KB |
1
|
r_in25.txt | AC | 29 ms | 4752 KB |
1
|
r_in26.txt | AC | 24 ms | 4888 KB |
1
|
r_in27.txt | AC | 30 ms | 4940 KB |
1
|
r_in28.txt | AC | 26 ms | 5028 KB |
1
|
r_in29.txt | AC | 33 ms | 5016 KB |
1
|
r_in30.txt | AC | 31 ms | 5084 KB |
1
|