Answer:
DP Recurrence:
P0 = 0
Pi = min{Pk + (200 - |mi - mk|)2}
0<=k
i.e.,
P[0] = 0
P[i] = min (k=0 to i-1) { P[k] + (200 – abs(mi – mk))2}
Where, Pi or P[i] is the minimum Penalty to reach ith destination.
DP Table:
0 | 1 | 2 | 3 | … | n |
0 | | | | | Pn |
DP Algorithm (for filling the table):
long minimize_penalty() {
int stops[MAX] = {0}, display[MAX] = {0};
P[0] = 0;
for(i = 1; i <= n_posts; i++) {
P[i] = INFINITY;
for(k = 0; k < style="mso-spacerun:yes"> {
t1 = (200 - abs(M[i] - M[k]));
t1 *= t1;
if((P[k] + t1) < style="mso-spacerun:yes"> {
P[i] = P[k] + t1;
stops[i] = k;
}
}
}
for(i = 1; i <= n_posts; i++) {
printf("\t %d ", P[i]);
}
printf("\n\n Penalty: %d\n Stops: ", P[n_posts]);
i = -1;
k = n_posts;
do {
display[++i] = k;
k = stops[k];
}while(k != 0);
for(k = i; k >= 0; k--)
printf(" %d", display[k]);
printf(" \n");
}
No comments:
Post a Comment