結果

問題 No.1 道のショートカット
ユーザー tkm2261
提出日時 2017-06-22 21:05:12
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,014 bytes
コンパイル時間 728 ms
コンパイル使用メモリ 98,896 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 16:30:43
合計ジャッジ時間 1,763 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>
#include <climits>
#include <bitset>
#include <cassert>
#include <random>
#include <typeinfo>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define UNIQUE(v) (v).erase(unique(ALL(v)), (v).end())
#define MP make_pair
#define MT make_tuple

using namespace std;

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

int dp[55][610];

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, c, v; // 街の数、所持金、道の数
	cin >> n >> c >> v;

	vector<int> s(v), t(v), y(v), m(v);
	rep(i, v) cin >> s[i]; // 出発地
	rep(i, v) cin >> t[i]; // 目的地
	rep(i, v) cin >> y[i]; // コスト
	rep(i, v) cin >> m[i]; // 時間

  // 二次配列にタプル
  vector<vector<tuple<int, int, int>>> G(n);

  // 道の数だけfor、push_backはベクターに要素追加
  // インデックスを0スタートにしてG[from][-] = (to, コスト, 時間)
  rep(i, v) G[s[i] - 1].push_back(MT(t[i] - 1, y[i], m[i]));

  // DP配列の初期化
  rep(i, 55) rep(j, 610) dp[i][j] = 1e9;
  rep(i, 55) rep(j, 610) dp[i][j] = 1e9;
  // 開始地点は0
	dp[0][0] = 0;

  // 街の数×所持金のループ
	rep(i, n) rep(j, c + 1){
		if (dp[i][j] == 1e9) continue; //到達してないところは飛ばす
		for (auto t : G[i]){
			int to, cost, dist;
      // std::tieで値を一気に取り出す
			tie(to, cost, dist) = t;
      //
			dp[to][j + cost] = min(dp[to][j + cost], dp[i][j] + dist);
		}
	}
  int ans = *min_element(dp[n - 1], dp[n - 1] + c + 1);
	cout << (ans != 1e9 ? ans : -1) << endl;
	return 0;
}
0