题目链接:URAL 1029
题面:
1029. Ministry
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Mr. F. wants to get a document be signed by a minister. A minister signs a document only if it is approved by his ministry. The ministry is an
M-floor building with floors numbered from 1 to
M, 1 ≤
M ≤ 100. Each floor has
N rooms (1 ≤
N ≤ 500) also numbered from 1 to
N. In each room there is one (and only one) official.
A document is approved by the ministry only if it is signed by at least one official from the
M-th floor. An official signs a document only if at least one of the following conditions is satisfied:
- the official works on the 1st floor;
- the document is signed by the official working in the room with the same number but situated one floor below;
- 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).
Each official collects a fee for signing a document. The fee is a positive integer not exceeding 10
9.
You should find the cheapest way to approve the document.
Input
The first line of an input contains two integers, separated by space. The first integer
M represents the number of floors in the building, and the second integer
N represents the number of rooms per floor. Each of the next
M lines contains
N integers separated with spaces that describe fees (the
k-th integer at
l-th line is the fee required by the official working in the
k-th room at the
l-th floor).
Output
You should print the numbers ofrooms in the order they should be visited toapprove the document in the cheapest way. If there are morethan one way leading to the cheapest cost you may print an any of them.
Sample
input | output |
---|---|
3 4 10 10 1 10 2 2 2 10 1 10 10 10 | 3 3 2 1 1 |
Notes
You can assume that for each official there always exists a way to get the approval of a document (from the 1st floor to this official inclusively) paying no more than 10
9.
Problem Source: Ural Collegiate Programming Contest '99
题意:
给定一矩阵,每次从矩阵的第一层任意房间开始,要求到达最后一层的任意房间所经过路径和代价最小。可以移动的方向为向上,左右。
解题:
因为在每一层是单向移动的,且如若从一个方向移过来,肯定不会移回去,因此可以用dp解决。dp[i][j]表示到达此处的最小代价,最后遍历最后一层,找出最小代价,反向输出路径即可。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#define LL long long
using namespace std;
int dp[105][505],cost[105][505],pathx[105][505],pathy[105][505];
int ans[50010];
int main()
{
int m,n,cnt=0;
scanf("%d%d",&m,&n);
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
}
}
//初始化第1层
for(int i=1;i<=n;i++)
{
dp[1][i]=cost[1][i];
pathx[1][i]=0;
pathy[1][i]=0;
}
for(int i=2;i<=m;i++)
{
//先从上面1层过来
for(int j=1;j<=n;j++)
{
dp[i][j]=dp[i-1][j]+cost[i][j];
pathx[i][j]=i-1;
pathy[i][j]=j;
}
//从左边往右扫
for(int j=2;j<=n;j++)
{
if(dp[i][j]>dp[i][j-1]+cost[i][j])
{
dp[i][j]=dp[i][j-1]+cost[i][j];
pathx[i][j]=i;
pathy[i][j]=j-1;
}
}
//从右往左更新
for(int j=n-1;j>=1;j--)
{
if(dp[i][j]>dp[i][j+1]+cost[i][j])
{
dp[i][j]=dp[i][j+1]+cost[i][j];
pathx[i][j]=i;
pathy[i][j]=j+1;
}
}
}
//找最小值
int minn=dp[m][1],px=m,py=1,tx,ty;
for(int i=1;i<=n;i++)
{
if(dp[m][i]<minn)
{
minn=dp[m][i];
py=i;
}
}
ans[cnt++]=py;
int tt=0;
//反向输出路径
while(1)
{
tx=pathx[px][py];
ty=pathy[px][py];
px=tx;
py=ty;
if(px==0)break;
ans[cnt++]=py;
}
printf("%d",ans[cnt-1]);
for(int i=cnt-2;i>=0;i--)
printf(" %d",ans[i]);
printf("\n");
return 0;
}