Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4220 | Accepted: 1348 | Special Judge |
Description
a. the official works on the 1st floor;
b. the document is signed by the official working in the room with the same number but situated one floor below;
c. the document is signed by an official working in a neighbouring room (rooms are neighbouring if they are situated on the same floor and their numbers differ by one).
Input
Output
Sample Input
3 4 10 10 1 10 2 2 2 10 1 10 10 10
Sample Output
3 3 2 1 1
题目大意:有一栋m层的房子,每层n个房间,如果该房间在第一层或者该房间的上一层的房间或者左右任意一个房间被访问才能访问该房间,问从第一层到最后一层走过的房间的权值和最小的路径。
#include <stdio.h> #include <iostream> #include <string.h> using namespace std; int fee[105][505]; int dp[105][505]; int path[105][505]; int ans[50005]; int main() { int m, n; int s; while(scanf("%d%d", &m, &n) != EOF) { memset(dp, 0, sizeof(dp)); memset(path, 0, sizeof(path)); memset(ans, 0, sizeof(ans)); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { scanf("%d", &fee[i][j]); } } for (int i = 1; i <= n; i++) { dp[1][i] = fee[1][i]; path[1][i] = i; } for (int i = 2; i <= m; i++) { dp[i][1] = dp[i - 1][1] + fee[i][1]; path[i][1] = 1; for (int j = 2; j <= n; j++) { if (dp[i - 1][j] < dp[i][j - 1]) { dp[i][j] = fee[i][j] + dp[i - 1][j]; path[i][j] = j; } else { dp[i][j] = fee[i][j] + dp[i][j- 1]; path[i][j] = j - 1; } } for (int j = n - 1; j > 0; j--) { if (dp[i][j + 1] + fee[i][j] < dp[i][j]) { dp[i][j] = dp[i][j + 1] + fee[i][j]; path[i][j] = j + 1; } } } int temp = 0x7fffffff; for (int i = 1; i <= n; i++) { if (temp > dp[m][i]) { temp = dp[m][i]; s = i; } } int nCount = 0; int x = m; ans[0] = s; while (x != 1) { nCount++; ans[nCount] = path[x][ans[nCount - 1]]; if (ans[nCount] == ans[nCount - 1]) { x--; } } for (int i = nCount; i >=0; i--) { printf("%d\n", ans[i]); } } return 0; }