結果

問題 No.1 道のショートカット
ユーザー simansiman
提出日時 2016-03-27 20:54:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 2,279 bytes
コンパイル時間 531 ms
コンパイル使用メモリ 78,304 KB
実行使用メモリ 6,664 KB
最終ジャッジ日時 2023-09-27 21:53:28
合計ジャッジ時間 2,025 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 4 ms
6,564 KB
testcase_09 AC 4 ms
5,780 KB
testcase_10 AC 4 ms
4,472 KB
testcase_11 AC 5 ms
4,404 KB
testcase_12 AC 9 ms
6,664 KB
testcase_13 AC 8 ms
6,636 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,944 KB
testcase_16 AC 3 ms
5,356 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,572 KB
testcase_19 AC 3 ms
5,216 KB
testcase_20 AC 2 ms
4,652 KB
testcase_21 AC 2 ms
4,900 KB
testcase_22 AC 2 ms
4,388 KB
testcase_23 AC 8 ms
4,376 KB
testcase_24 AC 7 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 8 ms
4,952 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 4 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 5 ms
5,548 KB
testcase_33 AC 3 ms
4,904 KB
testcase_34 AC 7 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,384 KB
testcase_39 AC 3 ms
5,332 KB
testcase_40 AC 3 ms
4,380 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 3 ms
5,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <limits.h>
#include <time.h>
#include <string>
#include <string.h>
#include <sstream>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>

using namespace std;

typedef long long ll;

const int MAX_N = 51;

int dp[MAX_N][MAX_N];

vector<int> pathCost[MAX_N][MAX_N];
vector<int> pathTime[MAX_N][MAX_N];

int main(){
  int n;
  cin >> n;
  int c;
  cin >> c;
  int v;
  cin >> v;
  int s[v];
  int t[v];
  int y[v];
  int m[v];

  int dp[n+1][n+1][c+1];

  for(int i = 0; i < 4; i++){
    for(int j = 0; j < v; j++){
      if(i == 0){
        cin >> s[j];
      }else if(i == 1){
        cin >> t[j];
      }else if(i == 2){
        cin >> y[j];
      }else{
        cin >> m[j];
      }
    }
  }

  memset(dp, -1, sizeof(dp));

  dp[0][1][0] = 0;

  for(int i = 0; i < v; i++){
    int from = s[i];
    int to = t[i];
    int cost = y[i];
    int time = m[i];

    pathCost[from][to].push_back(cost);
    pathTime[from][to].push_back(time);
  }

  int minTime = INT_MAX;

  for(int step = 1; step <= n; step++){
    bool update = false;

    for(int from = 1; from <= n; from++){
      for(int cost = 0; cost <= c; cost++){
        if(dp[step-1][from][cost] >= 0){
          for(int to = 1; to <= n; to++){
            if(pathCost[from][to].size() > 0){
              int size = pathCost[from][to].size();
              for(int k = 0; k < size; k++){

                int nextCost = cost + pathCost[from][to][k];
                int nextTime = dp[step-1][from][cost] + pathTime[from][to][k];

                if(nextCost <= c){
                  update = true;

                  if(to == n && minTime > nextTime){
                    minTime = nextTime;
                  }

                  if(dp[step][to][nextCost] == -1){
                    dp[step][to][nextCost] = nextTime;
                  }else{
                    dp[step][to][nextCost] = min(dp[step][to][nextCost], nextTime);
                  }
                }
              }
            }
          }
        }
      }
    }

    if(!update){
      break;
    }
  }

  if(minTime == INT_MAX){
    cout << -1 << endl;
  }else{
    cout << minTime << endl;
  }

  return 0;
}
0