結果

問題 No.1 道のショートカット
ユーザー ty70
提出日時 2015-06-14 22:48:34
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 417 ms / 5,000 ms
コード長 2,460 bytes
コンパイル時間 827 ms
コンパイル使用メモリ 97,064 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 16:15:34
合計ジャッジ時間 4,061 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1<<16
/*
	No.1 道のショートカット

	dp解

	dp[i][j]: i 番目の町にいて、手持ちのお金が j のときの最小の時間
*/

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P,P> PP;

const int MAX_N = 52;
const int MAX_C = 303;

int dp[MAX_N][MAX_C];

int main()
{
	memset (dp, 0, sizeof (dp ) );

	rep (i, MAX_N ) rep (j, MAX_C ) dp[i][j] = INF;
	
	ios_base::sync_with_stdio(0);
	int N, C, V; cin >> N >> C >> V;

	vector<int> s (V, 0 );
	rep (i, V ) cin >> s[i], s[i]--;
	vector<int> t (V, 0 );
	rep (i, V ) cin >> t[i], t[i]--;
	vector<int> y (V, 0 );
	rep (i, V ) cin >> y[i];
	vector<int> m (V, 0 );
	rep (i, V ) cin >> m[i];

	multimap<P,P> table;	// (from, to ) = (cost, time );
	rep (i, V )
		table.insert (make_pair (P (s[i], t[i] ), P (y[i], m[i] ) ) );

	dp[0][C] = 0;
	for (int i = 0; i < N; i++ ){
		for (int j = i+1; j < N; j++ ){
			multimap<P,P>::iterator it = table.find(P(i, j ) );
			for (; it != table.end(); it++ ){
				P dir = (*it).first;
				int from = dir.first;
				int to   = dir.second;
				P curr = (*it).second;
				int ny = curr.first;
				int nm = curr.second;
				for (int cy = C; cy >= 0; cy-- ){
					if (dp[from][cy] != INF && cy - ny >= 0 ){
	//					cerr << "from: " << from << " to: " << to << " ny: " << ny << " nm: " << nm << endl;
						dp[to][cy-ny] = min (dp[to][cy-ny], dp[from][cy] + nm );
	//					cerr << "new_dp: " << dp[to][cy-ny] << endl;
					} // end if
				} // end for
			} // end for
		} // end for
	} // end for 

	int res = INF;
	for (int c = 0; c <= C; c++ ){
		res = min (res, dp[N-1][c] );
	} // end for
	
	cout << (res == INF ? -1 : res ) << endl;
 
	return 0;
}
0