uva116 (DP)

Source: Internet
Author: User

The title means to start with the first column and go to the last column, which is the minimum amount of weight.

There are three ways to go, the first line on top is the last line (see figure).


The optimal substructure is d[i] [j] =min (d[k][J]-g[I [j]) where K is the column J column, three transformations of the number of rows.

Walk from the point in column J of line I to the last row minimum value, equal to the value of the dot K row j + 1 column go to the last column of the minimum value plus (I,J) this point.

Because it requires the same length as the dictionary order, we can only go from the post DP.

AC Code:

#include <stdio.h> #include <string.h> #include <algorithm> #define LL long long using namespace std;
const int M = 15;
const int N = 105;
CONST LL inf = 0x3f3f3f3f3f3f3f3f;
ll G[m][n];
ll D[m][n];
int r,c;
	ll min (ll A, ll B, ll c) {ll temp = (A < b? a:b);
	ll temp2 = (Temp < c temp:c);
return TEMP2;
	} ll DP (int l, int t) {if (d[l][t]! = INF) return d[l][t];
	int Up,down;
	int ahead = L;
	if (L = = 0) up = r-1;
	else up = l-1;
	if (L = = r-1) down = 0;
	else down = l + 1;
	ll m = min (dp (UP, T + 1), DP (down, T + 1), DP (ahead, T + 1));
	D[l][t] = m + g[l][t];
return d[l][t];
		} void print (int l, int t) {if (t = = c-1) {printf ("%d", L + 1);
	Return
	} printf ("%d", L + 1);
	int Up,down;
	int ahead = L;
	if (L = = 0) up = r-1;
	else up = l-1;
	if (L = = r-1) down = 0;
	else down = l + 1;
	int temp[3] = {up, ahead, down};
	Sort (Temp, temp + 3); for (int i = 0; i < 3; i++) {if (D[l][t]-g[l][t] = = d[temp[i]][t + 1]) {
			Print (Temp[i], T + 1);
		Break }}} int main () {while (~scanf ("%d%d", &r,&c)) {for (int i = 0; i < M; i++) {for (int j = 0; J &L T
		N; j + +) d[i][j] = inf;
		} memset (g, 0, sizeof (g));
			for (int i = 0; i < R; i++) {for (int j = 0; J < C; j + +) {scanf ("%lld", &g[i][j]);
		}} for (int i = 0; i < R; i++) {d[i][c-1] = g[i][c-1];
		} ll m = inf;
		int res;
			for (int i = 0; i < R; i++) {d[i][0] = DP (i, 0);
				if (D[i][0] < m) {m = d[i][0];
			res = i;
		}} print (res, 0);
		printf ("\ n");
	printf ("%lld\n", m); }
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.