-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoupon.cpp
74 lines (65 loc) · 1.55 KB
/
coupon.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
#include<iostream>
#define MAX 10009
using namespace std;
int price[MAX][MAX];
int dis[MAX][MAX];
int net[MAX][MAX];
void calNet(int n,int m) {
for(int i=0;i<n;i++)
for(int j=1;j<m;j++) {
net[i][j] = price[i][j] - dis[i][j-1] ;
if(net[i][j]<0)
net[i][j] = 0;
// cout << net[i][j] << " ";
}
}
long long calPrice(int n,int m) {
int cost;
int last,min=1000000;
for(int j=0;j<m;j++) {
if(min > price[0][j] + net[1][j]) {
min = price[1][j] + net[1][j];
last = j;
}
cost = min;
}
for(int j=1;j<n;j++) {
min = 1000000;
for(int i=0;i<m;i++) {
if(last == i) {
if(min > net[i][j] + net[i+1][j]) {
min = net[i][j] + net[i+1][j];
last = j;
}
}
else {
if(min > price[i][j] + net[i][j+1]) {
min = price[i][j] + net[i][j+1];
last = i;
}
}
}
cost += min;
}
return cost;
}
main() {
int t;
cin >> t;
while(t--) {
int n,m;
cin >> n >> m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin >> price[i][j];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin >> dis[i][j];
calNet(n,m);
/* for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cout << net[i][j] << " ";
*/
cout << calPrice(n,m) << endl;
}
}