結果

問題 No.1 道のショートカット
ユーザー nenuon
提出日時 2017-08-01 01:04:37
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 64 ms / 5,000 ms
コード長 1,431 bytes
コンパイル時間 964 ms
コンパイル使用メモリ 102,104 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 16:32:31
合計ジャッジ時間 2,738 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;
const int inf = 1e9;
int N,C,V;
int S[2000],T[2000],Y[2000],M[2000];
struct edge {
  int to, cost, times;
  edge(int a, int b, int c) {
    to = a, cost = b, times = c;
  }
};
vector<vector<edge> > G;
int dp[60][301];

int main()
{
  cin >> N >> C >> V;
  G.resize(N+1);
  FOR(i,0,V) cin >> S[i];
  FOR(i,0,V) cin >> T[i];
  FOR(i,0,V) cin >> Y[i];
  FOR(i,0,V) cin >> M[i];
  G[0].push_back(edge(1,0,0));
  FOR(i,0,V) {
    G[S[i]].push_back(edge(T[i],Y[i],M[i]));
  }
  FOR(i,0,60) FOR(j,0,301) dp[i][j] = inf;
  dp[0][0] = 0;
  // 幅優先で
  queue<int> Q;
  Q.push(0);
  while(!Q.empty()) {
    int v = Q.front();Q.pop();
    for(edge &e : G[v]) {
      bool next = false;
      FOR(i,0,301) {
        if(dp[v][i] == inf) continue;
        if(i + e.cost > 300) continue;
        if(dp[e.to][i + e.cost] > dp[v][i] + e.times) {
          next = true;
          dp[e.to][i + e.cost] = dp[v][i] + e.times;
        }
      }
      if(next) Q.push(e.to);
    }
  }
  int mn = inf;
  FOR(i,1,C+1) {
    mn = min(mn, dp[N][i]);
  }
  cout << (mn == inf ? -1 : mn) << endl;
  return 0;
}
0