結果

問題 No.134 走れ!サブロー君
ユーザー asugen0402asugen0402
提出日時 2019-01-29 16:43:38
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 57 ms / 5,000 ms
コード長 3,900 bytes
コンパイル時間 199 ms
コンパイル使用メモリ 33,792 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-18 18:07:32
合計ジャッジ時間 979 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 3 ms
6,940 KB
testcase_06 AC 10 ms
6,944 KB
testcase_07 AC 22 ms
6,944 KB
testcase_08 AC 57 ms
6,944 KB
testcase_09 AC 29 ms
6,940 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部定数
#define D_POINT_MAX		15										// 最大地点数
#define D_PTN_MAX		10000									// 最大配達済パターン数

// 内部構造体 - 地点情報
typedef struct Point {
	int miX, miY;												// 位置
	double mdWeight;											// 重さ
} Point;

// 内部変数
static FILE *szpFpI;											// 入力
static Point sz1Point[D_POINT_MAX];								// 地点 [0]:酒屋 [>0]:配達先
static int siDCnt;												// 配達先の数
static double sd2Time[D_PTN_MAX][D_POINT_MAX];					// 最短時間[配達済][現在地]

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
#endif

// 合計時間 - 取得
double
fGetSum(
	int piNow					// <I> 現在地
	, int piTo					// <I> 行き先
	, int piDel					// <I> 配達済
	, double pdWeight			// <I> 重さ
	, double pdTime				// <I> 時間
)
{
	// 距離
	int liLen = abs(sz1Point[piNow].miX - sz1Point[piTo].miX);
	liLen += abs(sz1Point[piNow].miY - sz1Point[piTo].miY);

	// 合計時間
	double ldSum = pdTime + (double)liLen * (pdWeight + 100.0) / 120.0 + sz1Point[piTo].mdWeight;

	// 最短時間
	if (sd2Time[piDel][piTo] <= ldSum) {
		return -1.0;
	}
	sd2Time[piDel][piTo] = ldSum;

	return ldSum;
}

// 最短時間 - セット
int
fSetMin(
	int piDCnt					// <I> 配達数
	, int piDel					// <I> 配達済
	, int piNow					// <I> 現在地
	, double pdWeight			// <I> 重さ
	, double pdTime				// <I> 時間
)
{
	int i;

	// 配達終了
	if (piDCnt == siDCnt) {
		fGetSum(piNow, 0, 0, pdWeight, pdTime);		// 酒屋へ戻る
		return 0;
	}

	// 配達
	for (i = 1; i <= siDCnt; i++) {
		int liBit = 1 << (i - 1);

		// 配達済
		if ((piDel & liBit) > 0) {
			continue;
		}
		int liDel = piDel | liBit;

		// 合計時間
		double ldSum = fGetSum(piNow, i, liDel, pdWeight, pdTime);
		if (ldSum < 0.0) {
			continue;
		}

		// 下位へ
		fSetMin(piDCnt + 1, liDel, i, pdWeight - sz1Point[i].mdWeight, ldSum);
	}

	return 0;
}

// 実行メイン
int
fMain(
	int piTNo					// <I> テスト番号 1~
)
{
	int i, j;
	char lc1Buf[1024], lc1Out[1024];

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", piTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", piTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// 最短時間 - 初期化
	for (i = 0; i < D_PTN_MAX; i++) {
		for (j = 0; j < D_POINT_MAX; j++) {
			sd2Time[i][j] = DBL_MAX;
		}
	}

	// 酒屋の位置 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d%d", &sz1Point[0].miX, &sz1Point[0].miY);

	// 配達先の数 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d", &siDCnt);

	// 配達先 - 取得
	double ldSum = 0.0;
	for (i = 1; i <= siDCnt; i++) {
		fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
		sscanf(lc1Buf, "%d%d%lf", &sz1Point[i].miX, &sz1Point[i].miY, &sz1Point[i].mdWeight);

		// 重さ合計 - セット
		ldSum += sz1Point[i].mdWeight;
	}

	// 最短時間 - セット
	fSetMin(0, 0, 0, ldSum, 0.0);

	// 結果 - セット
	sprintf(lc1Out, "%.8lf\n", sd2Time[0][0]);

	// 結果 - 表示
#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, lc1Out)) {
		siRes = -1;
	}
#else
	printf("%s", lc1Out);
#endif

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", piTNo);
	}
	else {
		printf("NG %d\n", piTNo);
	}
#endif

	return 0;
}

int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		fMain(i);
	}
#else
	fMain(0);
#endif

	return 0;
}

0