結果

問題 No.1 道のショートカット
ユーザー 綾地寧々
提出日時 2015-05-15 08:36:24
言語 C90
(gcc 12.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,827 bytes
コンパイル時間 311 ms
コンパイル使用メモリ 21,120 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 16:14:00
合計ジャッジ時間 1,450 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘main’:
main.c:19:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   19 |         scanf("%d", &g_towns);
      |         ^~~~~~~~~~~~~~~~~~~~~
main.c:20:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 |         scanf("%d", &g_money);
      |         ^~~~~~~~~~~~~~~~~~~~~
main.c:21:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   21 |         scanf("%d", &g_roads);
      |         ^~~~~~~~~~~~~~~~~~~~~
main.c:23:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   23 |                 scanf("%d", &g_departure[i]);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:26:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   26 |                 scanf("%d", &g_arrival[i]);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~
main.c:29:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   29 |                 scanf("%d", &g_cost[i]);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~
main.c:32:17: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   32 |                 scanf("%d", &g_time[i]);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <stdio.h>

#define min(x,y) ((x)<(y) ? (x) : (y))

int main(void);
int move(int start, int money, int time, int maxtime);

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

int 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("%d", &g_departure[i]);
	}
	for ( i=0; i<g_roads; i++ ) {
		scanf("%d", &g_arrival[i]);
	}
	for ( i=0; i<g_roads; i++ ) {
		scanf("%d", &g_cost[i]);
	}
	for ( i=0; i<g_roads; i++ ) {
		scanf("%d", &g_time[i]);
	}
	ret = move(1,g_money,0,-1);
	printf("%d\n", ret);
	return 0;
}

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