AOJ0611 Silk Road

Silk Road | Aizu Online Judge

Introduction to Programming Introduction to Algorithms and Data Structures Library of Data Structures Library of Graph Algorithms Library of Computational Geometry Library of Dynamic Programming Library of Number Theory

とする.到達している時の最小値から移動するのが,都市日目に到達する時の最小となる.これを繰り返す.

Code

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
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

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

struct RMQ {
  int n;
  vector<ll> dat;

  RMQ(int n_) {
      n = 1;
      while(n < n_) n *= 2;

      dat.resize(n*4);
      rep(i,n*4) dat[i] = INF;
  }

  void update(int k,ll a) {
      int i = k+n-1;
      dat[i] = a;

      while(i > 0) {
          i = (i-1) / 2;
          dat[i] = min(dat[i*2+1],dat[i*2+2]);
      }
  }

  //[a,b)
  //query(a,b,0,0,n)
  ll _query(int a,int b,int k,int l,int r) {
      if(r <= a || b <= l) return INF;

      if(a <= l && r <= b) return dat[k];
      else {
          ll vl = _query(a,b,k*2+1,l,(l+r)/2);
          ll vr = _query(a,b,k*2+2,(l+r)/2,r);
          return min(vl,vr);
      }
  }

  //[a,b)
  ll query(int a,int b) {
      return _query(a,b,0,0,n);
  }
};

ll dp[1005][1005], d[1005][1005];

int main() {
  int n, m;
  cin >> n >> m;

  vector<ll> D(n), C(m);
  rep(i, n) cin >> D[i];
  rep(i, m) cin >> C[i];

  rep(i, n) {
      rep(j, m) dp[i][j] = INF;
  }

  rep(i, n) {
      rep(j, m) d[i][j] = D[i] * C[j];
  }

  rep(j, m) dp[0][j] = D[0] * C[j];

  REP(i, 1, n) {
      RMQ rmq(m);
      rep(j, m) rmq.update(j, dp[i-1][j]);

      rep(j, m) {
          dp[i][j] = rmq.query(0, j) + d[i][j];
      }
  }

  ll ans = INF;
  rep(j, m) {
      ans = min(ans, dp[n-1][j]);
  }

  cout << ans << endl;

  return 0;
}
Mar 18th, 2016