Description We must have seen the TV of the real war. As a matter of Zhangga, he also ran back to support the tunnel battle. It is a small town in Hebei, this small town is more complex, what kind of people have. So Zhangga can not walk the street, only in the tunnel walk. However, depending on the geological situation, the same length of the street, the tunnel may be different time to walk. Of course, there are tunnels under each street, and there are connections in the streets. One day Zhangga in a, suddenly found that there are large forces of the enemy come, so he must be as fast as possible to run to the B to inform the unit concealed, each of the tunnels marked his time to go. Please help him to calculate, how to walk the shortest time.
A +---2---+---3---+----1---+----2---+ | | | | | 2 1 2 2 3 | | | | | +---2---+---3---+----4---+---5----+ | | | | | 3 4 1 2 3 | | | | | +---2---+---2---+---1----+---4----+ | | | | | 2 2 1 3 4 | | | | |
Input has multiple test cases. For each test case, line 1th enters 2 integers n and m (1 <= n,m <=100) that represent the number of bars in the horizontal street and the number of vertical streets. The following n lines enter each segment of the tunnel (landscape) required time, each row in order from left to right for each line of M line input the length of the tunnel (portrait) required time, each column from top to bottom processing to the end of the file output one line for each test case, output his shortest time from A to B sample Input
4 52 3 1 22 3 4 52 2 1 41 3 2 32 3 21 4 22 1 12 2 33 3 4
Sample Output
13
HINT
Only down or right
The main idea: the shortest circuit is the other way for infinity, can walk to take the minimum value, the final output DP[N][M], the problem of data processing a bit difficult.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intINF =0x3f3f3f3f;intMain () {intdp[ the][ the]; intx[ the][ the],y[ the][ the]; intn,m; while(~SCANF ("%d%d",&n,&m)) { //Heng for(inti =0; I <= N; i++) for(intj =0; J <= M; j + +) X[i][j]= Y[i][j] = Dp[i][j] =inf; for(inti =1; I <= n;i++){ for(intj =1; J <= M1; j + +) {scanf ("%d",&X[i][j]); } } //Shu for(inti =1; I <= m; i++){ for(intj =1; J <= N-1; j + +) {scanf ("%d",&Y[i][j]); }} dp[1][1] =0; for(inti =1; I <= n;i++){ for(intj =1; J <= m;j++){ if(i = =1&& J = =1) Continue; DP[I][J]=0; DP[I][J]+=min (dp[i-1][J] + y[j][i-1],dp[i][j-1]+x[i][j-1]); }} printf ("%d\n", Dp[n][m]); } return 0;}
View Code
dp--Tunnel battle