ural 1119 Metro dp水,uraldp

來源:互聯網
上載者:User

ural 1119 Metro dp水,uraldp
點擊開啟連結
1119. MetroTime limit: 0.5 second
Memory limit: 64 MB
Many of SKB Kontur programmers like to get to work by Metro because the main office is situated quite close the station Uralmash. So, since a sedentary life requires active exercises off-duty, many of the staff — Nikifor among them — walk from their homes to Metro stations on foot.Nikifor lives in a part of our city where streets form a grid of residential quarters. All the quarters are squares with side 100 meters. A Metro entrance is situated at one of the crossroads. Nikifor starts his way from another crossroad which is south and west of the Metro entrance. Naturally, Nikifor, starting from his home, walks along the streets leading either to the north or to the east. On his way he may cross some quarters diagonally from their south-western corners to the north-eastern ones. Thus, some of the routes are shorter than others. Nikifor wonders, how long is the shortest route.You are to write a program that will calculate the length of the shortest route from the south-western corner of the grid to the north-eastern one.InputThere are two integers in the first line: N and M (0 < N,M ≤ 1000) — west-east and south-north sizes of the grid. Nikifor starts his way from a crossroad which is situated south-west of the quarter with coordinates (1, 1). A Metro station is situated north-east of the quarter with coordinates (N, M). The second input line contains a number K (0 ≤ K ≤ 100) which is a number of quarters that can be crossed diagonally. Then K lines with pairs of numbers separated with a space follow — these are the coordinates of those quarters.OutputYour program is to output a length of the shortest route from Nikifor's home to the Metro station in meters, rounded to the integer amount of meters.Sample

input output
3 231 13 21 2
383
Problem Author: Leonid Volkov
Problem Source: USU Open Collegiate Programming Contest October'2001 Junior Session

從左下角的點走到右上方的點最少可以走多少步,其中有的點可以走對角線。第一行和第一列的點肯定無法從對角線得來,所以可以初始化。對於其餘的點,有的可以從其左面或者下面得來,有的還可以從對角線得來,只要求最小即可。狀態轉移方程:如果可以從左面或者下面得來的點:dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1);如果可以從左面或者下面或者對角線得來的點:dp[i][j]=min(dp[i-1][j]+1,min(dp[i][j-1]+1,dp[i-1][j-1]+P));
開了3層for迴圈,原本以為會逾時,1000*1000*100。結果跑了0.1568s,也是醉了。
//0.1568 110 KB#include<stdio.h>#include<algorithm>#include<math.h>#define P sqrt(2)using namespace std;double dp[1007][1007];struct N{    int x,y;}p[107];int cmp(N a,N b){    if(a.x==b.x)return a.y<b.y;    return a.x<b.x;}int main(){int n,m;while(scanf("%d%d",&n,&m)!=EOF){    n++;m++;        int k;        scanf("%d",&k);        for(int i=0;i<k;i++)            scanf("%d%d",&p[i].x,&p[i].y);        sort(p,p+k,cmp);        dp[1][1]=0;        for(int i=2;i<=n;i++)//初始化第一行            dp[i][1]=dp[i-1][1]+1;        for(int i=2;i<=m;i++)//初始化第一列            dp[1][i]=dp[1][i-1]+1;        for(int i=2;i<=n;i++)            for(int j=2;j<=m;j++)            {                int flag=0;                for(int a=0;a<k;a++)                    if(p[a].x==(i-1)&&p[a].y==(j-1))//從三個方向得來                        {dp[i][j]=min(dp[i-1][j]+1,min(dp[i][j-1]+1,dp[i-1][j-1]+P));flag=1;break;}                    else if(p[a].x>(i-1))break;                if(!flag)dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1);//從兩個方向得來            }        printf("%.lf\n",dp[n][m]*100);}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.