标签:dp
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 4528 | Accepted: 1452 | 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
Hint
Source
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 510;
const int M = 110;
const int inf = 0x3f3f3f3f;
int dp[M][N], mat[M][N];
int path[M][N];
int m, n, cnt;
void print(int i, int j)/*第i层第j个房间*/
{
if (i == 1)
{
return;
}
if (path[i][j] == j)
{
print(i - 1, j);
printf("%d\n", j);
}
else if (path[i][j] == j - 1)
{
print(i, j - 1);
printf("%d\n", j - 1);
}
else
{
print(i, j + 1);
printf("%d\n", j + 1);
}
}
int main()
{
while (~scanf("%d%d", &m, &n))
{
memset (dp, inf, sizeof(dp));
for (int i = 1; i <= m; ++i)
{
for (int j = 1; j <= n; ++j)
{
scanf("%d", &mat[i][j]);
}
}
for (int i = 1; i <= n; ++i)
{
dp[1][i] = mat[1][i];
path[1][i] = i;
}
for (int i = 2; i <= m; ++i)
{
for (int j = 1; j <= n; ++j)
{
dp[i][j] = mat[i][j] + dp[i - 1][j];
path[i][j] = j;
}
for (int j = 2; j <= n; ++j)
{
if (dp[i][j] > dp[i][j - 1] + mat[i][j])
{
dp[i][j] = dp[i][j - 1] + mat[i][j];
path[i][j] = j - 1;
}
}
for (int j = n - 1; j >= 1; --j)
{
if (dp[i][j] > dp[i][j + 1] + mat[i][j])
{
dp[i][j] = dp[i][j + 1] + mat[i][j];
path[i][j] = j + 1;
}
}
}
int ans = inf, s;
cnt = 0;
for (int i = 1; i <= n; ++i)
{
if (ans > dp[m][i])
{
s = i;
ans = dp[m][i];
}
}
print(m, s);
printf("%d\n", s);
}
return 0;
}标签:dp
原文地址:http://blog.csdn.net/guard_mine/article/details/41652099