SRM339 D2H OnTime

TopCoder Statistics - Problem Statement

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.

駅$0$から駅$n-1$に行きたい.空白区切りで$a\ b\ departure\ time\ cost$が与えられる.駅$a$と駅$b$を結ぶバスは,時刻$departure$に出発し,時間$time$かかり,コストは$cost$である.時刻$T$以内に駅$n-1$に着けない場合は$-1$,着ける場合は最小コストを答える.


(駅番号, 時刻)を状態にしてdijkstra.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,k,n) for(int i=k;i<(int)(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define INF 1<<30
#define mp make_pair

#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> PI;
typedef pair<P, P> PP;

vector<string> split(const string &str, char delim) {
  vector<string> res;
  size_t current = 0, found;
  while((found = str.find_first_of(delim, current)) != string::npos) {
      res.push_back(string(str, current, found - current));
      current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

vector<PP> G[55];
int d[55][100005];

void dijkstra() {
  rep(i, 55) rep(j, 100005) d[i][j] = INF;
  d[0][0] = 0;

  priority_queue<PI, vector<PI>, greater<PI> > que;
  que.push(mp(mp(0, 0), 0));

  while(que.size()) {
      PI p = que.top(); que.pop();
      int c = p.fi.fi;
      int t = p.fi.se;
      int cur = p.se;

      if(d[cur][t] < cur) continue;

      rep(i, G[cur].size()) {
          int departure = G[cur][i].fi.fi;
          int time = G[cur][i].fi.se;
          int cost = G[cur][i].se.fi;
          int next = G[cur][i].se.se;

          if(departure != 0 && time != 0 && departure <= t) continue;
          if(d[next][departure + time] > c + cost) {
              d[next][departure + time] = c + cost;
              que.push(mp(mp(c + cost, departure + time), next));
          }
      }
  }
}

class OnTime {
  public:
  int minCost(int N, int T, vector <string> buses) {
      rep(i, 55) G[i].clear();

      int M = buses.size();
      rep(i, M) {
          stringstream ss;
          ss << buses[i];
          int a, b, departure, time, cost;
          ss >> a >> b >> departure >> time >> cost;

          G[a].push_back(mp(mp(departure, time), mp(cost, b)));
      }

      dijkstra();

      int ans = INF;
      rep(i, T + 1) {
          ans = min(ans, d[N-1][i]);
      }

      if(ans == INF) return -1;
      return ans;
  }
};
Feb 10th, 2017