-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminpath_cost.cpp
75 lines (72 loc) · 1.38 KB
/
minpath_cost.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
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int n,m;
/*void mincostpath(cost[n][m])
{
int mincost[n][m],i,j;
mincost[0][0]=cost[0][0];
for(i=1;i<m;i++)
{
mincost[0][i]=mincost[0][i-1]+cost[0][i];
}
for(i=1;i<n;i++)
{
mincost[i][0]=mincost[i-1][0]+cost[0][i];
}
for(i=1;i<n;i++)
{
for(j=1;j<m;j++)
{
mincost[i][j]=min(mincost[i-1][j],mincost[i][j-1])+cost[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<mincost[i][j]<<" ";
}
cout<<"\n";
}
}*/
int main()
{
int i,j;
cin>>n>>m;
int cost[n][m];
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cin>>cost[i][j];
}
}
// mincostpath(cost);
int mincost[n][m];
mincost[0][0]=cost[0][0];
for(i=1;i<m;i++)
{
mincost[0][i]=mincost[0][i-1]+cost[0][i];
}
for(i=1;i<n;i++)
{
mincost[i][0]=mincost[i-1][0]+cost[0][i];
}
for(i=1;i<n;i++)
{
for(j=1;j<m;j++)
{
mincost[i][j]=min(mincost[i-1][j],mincost[i][j-1])+cost[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout<<mincost[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}