結果

問題 No.1 道のショートカット
ユーザー Onju
提出日時 2015-02-03 04:56:41
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,334 bytes
コンパイル時間 729 ms
コンパイル使用メモリ 63,188 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-08 03:44:41
合計ジャッジ時間 1,989 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 31 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

//No.1 道のショートカット

//メモ:
//  有効グラフの閉路はない
//  無効グラフの閉路はある

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <climits>
#define N_MAX 50
#define V_MAX 1500
using namespace std;

typedef struct
{
	int S;
	int T;
	int Y;
	int M;
} Point;

int N, C, V;
int Si[N_MAX];
Point P[V_MAX];

bool compP(const Point& x, const Point& y)
{
	return x.S == y.S ? x.T < y.T : x.S < y.S;
}

int solve(int p, int c, int m)
{
	static int ans = INT_MAX;

	//時間オーバー、NOT最適解排除
	if (c > C || m >= ans)
		return INT_MAX;

	//ゴール
	if (p == N - 1)
	{
		return (ans = m);
	}

	for (int i = Si[p]; P[i].S == p + 1; ++i)
		solve(P[i].T - 1, c + P[i].Y, m + P[i].M);

	return ans == INT_MAX ? -1 : ans;
}

int main()
{
	memset(Si, 0, sizeof(int)*N_MAX);

	//N,C,V取得
	cin >> N >> C >> V;

	//S,T,Y,M取得
	for (int i = 0; i < V; ++i)
		cin >> P[i].S;
	for (int i = 0; i < V; ++i)
		cin >> P[i].T;
	for (int i = 0; i < V; ++i)
		cin >> P[i].Y;
	for (int i = 0; i < V; ++i)
		cin >> P[i].M;

	//ソート
	sort(P, P + V, compP);

	//各町の開始インデックスを調べておく
	for (int i = 1, n = 2; i < V; ++i)
		if (n == P[i].S)
		{
			Si[n - 1] = i;
			++n;
		}

	//出力
	cout << solve(0, 0, 0) << endl;

	return 0;
}
0