-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNational Railway.cpp
85 lines (82 loc) · 2.36 KB
/
National Railway.cpp
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
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define int long long int
#define double double_t
#define INF 1e18
using namespace __gnu_pbds;
using namespace std;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int GCD(int a, int b) {
return b == 0 ? a : GCD(b, a % b);
}
int power(int x, int y, int MOD = INF) {
if (y == 0) {
return 1;
}
if (y % 2 == 0) {
return power((x * x) % MOD, y / 2) % MOD;
} else {
return (x * power((x * x) % MOD, (y - 1) / 2) % MOD) % MOD;
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
while (t--) {
int h,w,c;
cin >> h>>w>>c;
int **ar = new int*[h];
for (int i = 0; i < h; ++i) {
ar[i] = new int[w];
}
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
cin>>ar[i][j];
}
}
int dp[h][w];
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
dp[i][j] = INT_MAX;
}
}
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (i==0 && j==0){
dp[i][j] = INT_MAX;
} else if (i==0){
dp[i][j] = min(ar[i][j],dp[i][j-1] + c);
} else if (j==0){
dp[i][j] = min(ar[i][j],dp[i-1][j] + c);
} else{
dp[i][j] = min(ar[i][j],min(dp[i-1][j] + c,dp[i][j-1] + c));
}
}
}
int x[h][w];
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
x[i][j] = INT_MAX;
}
}
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
if (i==0 && j==0){
x[i][j] = c + ar[i][j];
} else if (i==0){
x[i][j] = dp[i][j-1] + c + ar[i][j];
} else if (j==0){
x[i][j] = dp[i-1][j] + c + ar[i][j];
} else{
x[i][j] = min(dp[i-1][j],dp[i][j-1]) + c + ar[i][j];
}
}
}
cout<<x[h-1][w-1];
cout << "\n";
}
}