您的位置:首页 > 其它

ural 1119. Metro(动态规划)

2013-09-05 22:02 337 查看

1119. Metro

Time 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.

#include<iostream>
#include<cstdio>
#include<cmath>

using namespace std;
int s[1010][1010]={0};
double dp[1010][1010]={0};

double min(double a,double b,double c=9999999999)
{
if(a>b)
return b<c?b:c;
else
return a<c?a:c;
}

int main()
{
//    freopen("1.txt","r",stdin);
int n,m;
cin>>n>>m;
int k;
cin>>k;
int i,j;
int a,b;
n++;
m++;
for(i=1;i<=n;i++)
dp[0][i]=9999999999;
for(i=1;i<=m;i++)
dp[i][0]=9999999999;
for(i=0;i<k;i++)
{
cin>>a>>b;
s[b+1][a+1]=1;
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(i==1&&j==1)continue;
if(s[i][j]==1)
{//如果改点可以由一点斜着到达
dp[i][j]=min(dp[i][j-1]+100,dp[i-1][j]+100,dp[i-1][j-1]+sqrt(2.0)*100);//比较得出dp[i][j-1]+100、dp[i-1][j]+100、dp[i-1][j-1]+sqrt(2)*100中的最小值;
}//注意sqrt()里面是精度数,例如不可以是2,单可以是2.0
else
{//改点不可以由一点斜着到达
dp[i][j]=min(dp[i][j-1]+100,dp[i-1][j]+100);//比较求出dp[i][j-1]+100、dp[i-1][j]+100中的最小值
}
}
}
printf("%.0lf\n",dp[m]
);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: