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 of rooms in the order they should be visited to approve the document in the cheapest way. If there are more than 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 |
Hint
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 Author: Evgeny Frolov
Problem Source: Ural Collegiate Programming Contest '99
Problem Source: Ural Collegiate Programming Contest '99
Tags:
)
Difficulty: 335
这个题就是一个矩阵,从第一行转移到最后一行,到达某个点的时候某个点的 fee得加上,算作花费,问从第一行走到最后一行最少花费时的方案,
输出的时每行房间所在的号,比如说从map[1][3]--map[2][3]--map[2][2]--map[2][1]--map[3][1] 把列号输出来……
每个点只能从同列号的上一行转化,或者同行的相邻列!
状态转移很好写,问题是如何记录路径,我开了一个数组ans[][]表示该点是从哪一个点转移过来的,0表示本身,-1表示上一行,1表示左边,2表示右边!
这个小题思路一下子就想出来了,但是除了两个小Bug,我先把每行从上一行挑选一个最小值更新这一行同列的值,然后从这个值向两边转移,却不知道局部最优未必是全局最优!
比如:
1 1 1 1 0
1 1 1 1 1222
后来发现了就改了,但是还是WA在第一组测试数据了,郁闷死了! 这几天网速够给力,就是AC不给力啊! 检查了半天,思考了半天,各种数据与标称对拍,终于发现一个数组名写错了…………………………………… 这个小错误又是几个小时出去了!!!!!!!要是比赛,肯定又惨了!
娘的,最不该出错的地方出错了,我有点狗蛋当年的心情了!
不说,贴代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
long long dp[110][510];
long long map[110][510];
int ans[110][510];
int n,m;
int f(int x)
{///返回x行最小值所处的列
int ans0=1;
for(int j=1;j<=m;++j)
{
if(dp[x][ans0]>dp[x][j])ans0=j;///是dp不是map数组!!!!!!!!!!!
}
return ans0;
}
int main()
{
memset(map,0,sizeof(map));
memset(dp,0x3f3f,sizeof(dp));
memset(ans,0,sizeof(ans));
cin>>n>>m;
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
scanf("%d",&map[i][j]);
dp[1][j]=map[1][j];
}
}
for(int i=2;i<=n;++i)
{
int ind=f(i-1);
long long tmp=dp[i][ind];
dp[i][ind]=min(dp[i-1][ind]+map[i][ind],dp[i][ind]);
if(tmp!=dp[i][ind])ans[i][ind]=-1;
for(int j=1;j<=m;++j)
{ tmp=dp[i][j];
dp[i][j]=min(dp[i-1][j]+map[i][j],dp[i][j]);
if(tmp!=dp[i][j])ans[i][j]=-1;///从下边来的
}
for(int j=2;j<=m;++j)
{
tmp=dp[i][j];
dp[i][j]=min(dp[i][j-1]+map[i][j],dp[i][j]);
if(tmp!=dp[i][j])ans[i][j]=1;///从左边来的
}
for(int j=m-1;j>=1;--j)
{ tmp=dp[i][j];
dp[i][j]=min(dp[i][j+1]+map[i][j],dp[i][j]);
if(tmp!=dp[i][j])ans[i][j]=2;///从右边来的
}
}
// for(int i=1;i<=n;++i)
// {
// for(int j=1;j<=m;++j)
// cout<<dp[i][j]<<" ";
// cout<<endl;
// }
int in=f(n);
// cout<<dp[n][in]<<"========"<<in<<endl;
stack<int> sta;
int t,x=n;
t=ans[x][in];
while(1)
{ sta.push(in);
if(t==0){break;}
else if(t==-1)
{
x--;
}else if(t==1)
{
in--;
}else if(t==2)
{
in++;
}t=ans[x][in];
}
while(sta.size()>1)
{
cout<<sta.top()<<" ";
sta.pop();
}if(sta.size()==1)cout<<sta.top()<<endl,sta.pop() ;
return 0;
}
timus 1029. Ministry URAL 解题报告