URAL 1029 Ministry

来源:互联网 发布:js date 设置日期 编辑:程序博客网 时间:2024/05/17 19:59

题目链接:URAL 1029

题面:

1029. Ministry

Time limit: 1.0 second
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 anM-floor building with floors numbered from 1 to M, 1 ≤ M ≤ 100. Each floor hasN 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 theM-th floor. An official signs a document only if at least one of the following conditions is satisfied:
  1. the official works on the 1st floor;
  2. the document is signed by the official working in the room with the same number but situated one floor below;
  3. 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 109.
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 integerM represents the number of floors in the building, and the second integerN represents the number of rooms per floor. Each of the next M lines containsN integers separated with spaces that describe fees (the k-th integer atl-th line is the fee required by the official working in the k-th room at thel-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

inputoutput
3 410 10 1 102 2 2 101 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 109.
Problem Author: Evgeny Frolov
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 longusing 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;}


0 0
原创粉丝点击