結果

問題 No.1 道のショートカット
ユーザー siman
提出日時 2016-03-27 20:31:54
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 2,080 bytes
コンパイル時間 554 ms
コンパイル使用メモリ 75,072 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-08 04:25:31
合計ジャッジ時間 1,604 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3 WA * 1
other AC * 16 WA * 24
権限があれば一括ダウンロードができます

ソースコード

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];

int pathCost[MAX_N][MAX_N];
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(pathCost, -1, sizeof(pathCost));
  memset(pathTime, -1, sizeof(pathTime));
  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] = cost;
    pathTime[from][to] = time;
  }

  int minTime = INT_MAX;

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

              if(nextCost <= c){
                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(minTime == INT_MAX){
    cout << -1 << endl;
  }else{
    cout << minTime << endl;
  }

  return 0;
}
0