結果
| 問題 |
No.1 道のショートカット
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-05-14 22:55:24 |
| 言語 | C90 (gcc 12.3.0) |
| 結果 |
AC
|
| 実行時間 | 1 ms / 5,000 ms |
| コード長 | 2,177 bytes |
| コンパイル時間 | 817 ms |
| コンパイル使用メモリ | 23,296 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-07-08 03:57:55 |
| 合計ジャッジ時間 | 1,720 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 40 |
コンパイルメッセージ
main.c: In function ‘main’:
main.c:21:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
21 | scanf("%d", &g_towns);
| ^~~~~~~~~~~~~~~~~~~~~
main.c:22:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
22 | scanf("%d", &g_money);
| ^~~~~~~~~~~~~~~~~~~~~
main.c:23:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
23 | scanf("%d", &g_roads);
| ^~~~~~~~~~~~~~~~~~~~~
main.c:25:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
25 | scanf(" %[0123456789]", temp);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:29:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
29 | scanf(" %[0123456789]", temp);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:33:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
33 | scanf(" %[0123456789]", temp);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:37:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
37 | scanf(" %[0123456789]", temp);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
#include <stdio.h>
#include <math.h>
#define min(x,y) ((x)<(y) ? (x) : (y))
void main(void);
int move(int start, int money, int time, int maxtime);
int strtoint(char str[5]);
int g_towns = 0;
int g_money = 0;
int g_roads = 0;
int g_departure[1500] = {0};
int g_arrival[1500] = {0};
int g_cost[1500] = {0};
int g_time[1500] = {0};
void main(void) {
char temp[5];
int i, ret;
scanf("%d", &g_towns);
scanf("%d", &g_money);
scanf("%d", &g_roads);
for ( i=0; i<g_roads; i++ ) {
scanf(" %[0123456789]", temp);
g_departure[i] = strtoint(temp);
}
for ( i=0; i<g_roads; i++ ) {
scanf(" %[0123456789]", temp);
g_arrival[i] = strtoint(temp);
}
for ( i=0; i<g_roads; i++ ) {
scanf(" %[0123456789]", temp);
g_cost[i] = strtoint(temp);
}
for ( i=0; i<g_roads; i++ ) {
scanf(" %[0123456789]", temp);
g_time[i] = strtoint(temp);
}
ret = move(1,g_money,0,-1);
printf("%d\n", ret);
return;
}
int move(int start, int money, int time, int maxtime) {
int mintime = -1;
int newtime = 0;
int index = 0;
/* ----- 再帰呼出し終了条件の判定 ---------- */
/* コストが最大値を超えてしまった場合 */
if ( money < 0 ) {
return -1;
}
/* 最後の街に到着した場合 */
else if ( start == g_towns ) {
return time;
}
/* 他のルートより時間がかかったことを検出した場合 */
else if ( (maxtime != -1) && (maxtime <= time) ) {
return -1;
}
/* ----- 再帰呼出しをおこなう ------------ */
mintime = maxtime;
for ( index=0; index<g_roads; index++ ) {
/* 行き先が見つかった */
if ( g_departure[index] == start ) {
newtime = move(g_arrival[index],money-g_cost[index],time+g_time[index],mintime);
if ( newtime == -1 ) {
/* 今回のルートでは最後まで辿りつけなかったので何もしない。 */
}
else if ( mintime == -1 ) {
mintime = newtime;
}
else {
mintime = min(mintime, newtime);
}
}
}
/* ----- 探した最短時間での結果を返却する */
return mintime;
}
int strtoint(char str[5]) {
int result = 0;
int i = 0;
while ( str[i] != '\0' ) {
result *= 10;
result += (int)(str[i]-'0');
i++;
}
return result;
}