URAL 1119
来源:互联网 发布:黄磊人设崩塌知乎 编辑:程序博客网 时间:2024/04/29 08:23
题目大意:求存在K条对角线路线的单位方格图上一条从(0,0)到(M,N)的最短路径的长度(四舍五入取整)。
Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
数据规模:0<M,N<=1000,0<=K<=100。
理论基础:定理1:三角形两边之和大于第三边。
定理2:等腰直角三角形斜边等于直角边的sqrt(2)倍。
题目分析:突然感觉上面说出那两条定理有点弱智(擦汗)。。。
言归正传:这是一道较为简单的动态规划题目。我们用dp[i][j]表示从(i,j)点到(m,n)点的最短路径长度。用sht[i][j]表示到此处是否具有对角线路线。
首先:初始化。我们易得:dp[i][n]到(m,n)的最短路径长度必然为m-i,dp[m][j]到(m,n)的最短路径长度必然为n-j。其余状态均初始化为0,INF或者不初始化均可。
然后:我们推得状态转移方程为:
dp[i][j]=sht[i+1][j+1]?sqrt((double)2)+dp[i+1][j+1]:1+min(dp[i+1][j],dp[i][j+1])。
最后:(int)floor(dp[0][0]*100+0.5)即为所要求的答案。
代码如下:
#include<iostream>#include<cstring>#include<string>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>#include<ctime>#include<vector>using namespace std;typedef double db;#define DBG 0#define maa (1<<31)#define mii ((1<<31)-1)#define ast(b) if(DBG && !(b)) { printf("%d!!|\n", __LINE__); while(1) getchar(); } //调试#define dout DBG && cout << __LINE__ << ">>| "#define pr(x) #x"=" << (x) << " | "#define mk(x) DBG && cout << __LINE__ << "**| "#x << endl#define pra(arr, a, b) if(DBG) {\ dout<<#arr"[] |" <<endl; \ for(int i=a,i_b=b;i<=i_b;i++) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"\n"); \ if((b-a+1)%8) puts("");\}template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; }template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; }typedef long long LL;typedef long unsigned int LU;typedef long long unsigned int LLU;#define N 1000double dp[N+1][N+1];int m,n,ns;bool sht[N+1][N+1];void init(int n,int m){ memset(dp,0,sizeof(dp)); int bor=max(n,m); for(int i=0;i<=bor;i++) { if(i<=n)dp[n-i][m]=i; if(i<=m)dp[n][m-i]=i; } pra(dp[n],1,m)}void solve(int n,int m){ for(int i=n-1;i>=0;i--) { for(int j=m-1;j>=0;j--) { dp[i][j]=sht[i+1][j+1]?sqrt((double)2)+dp[i+1][j+1]:\ 1+min(dp[i+1][j],dp[i][j+1]); } pra(dp[i],1,m) pra(sht[i],1,m) } printf("%d\n",(int)(dp[0][0]*100+0.5));}int main(){ memset(sht,0,sizeof(sht)); scanf("%d%d",&n,&m); scanf("%d",&ns); for(int i=0;i<ns;i++) { int a,b; scanf("%d%d",&a,&b); sht[a][b]=true; } init(n,m); solve(n,m);return 0;}其中,pra(dp[i],1,m)之类的语句均为调试语句在DBG==0时不会执行。
by:Jsun_moon http://blog.csdn.net/jsun_moon
- URAL 1119
- ural 1119【dp+精度】
- URAL 1119 Metro
- ural 1119 Metro
- URAL 1119Metro
- Ural 1119 Metro
- URAL
- 【ural】
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- URAL
- linux下删除乱码文件名的方法
- C#对文本文件的读写
- Zend Studio 9 汉化
- C++学习基本语言-字面值常量
- gw
- URAL 1119
- 如何设置 sql server 2008 开启远程连接
- android ListView详解
- hdu_1428 漫步校园
- tomcat线程池的实现
- 利用ictclas进行新闻关键词提取
- LED必要了解的基础知识
- 二分图最大匹配--poj2062
- How do I add an integer value with javascript (jquery) to a value that's returning a string?