Timus 1119. Metro 动态规划
来源:互联网 发布:windows 性能监视器 编辑:程序博客网 时间:2024/05/22 14:16
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.
Input
There 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.
Output
Your 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
3 231 13 21 2
383
很好的一道动态规划法题目。
注意:
1 行列别搞错了,要很细心一点一点对起来
2 要以边线思考,不要以方块来计算, N*M个方块就成了(N+1)*(M+1)条交叉线了,最下面和最左边的线就方便初始化了
3 注意C++的四舍五入的方法
动态规划的状态转移方程:
if (A[y][x]) B[x] = t + 1.414213562;else B[x] = min(B[x-1], B[x]) + 1 ;A[y][x]表示是否有对角线,有对角线必定是走对角线的。
void Metro1119_Vec(){int N, M, K, x, y;cin>>N>>M;//小心什么是行什么是列vector<vector<bool> > A(M+1, vector<bool>(N+1));cin>>K;for (int i = 0; i < K; i++){cin>>x>>y;//列和行别搞错了A[y][x] = 1;}vector<double> B(N+1);for (int i = 0; i <= N; i++){B[i] = (double)i;}for (y = 1; y <= M; y++){double t = B[0];B[0] += 1.0;for (x = 1; x <= N; x++){double a = B[x];if (A[y][x]) B[x] = t + 1.414213562;else B[x] = min(B[x-1], B[x]) + 1 ;t = a;}}cout<<(int)(B[N] * 100.0 + 0.5);//修改要全面}
逛了下网上,发现本题还可以利用LIS的思想去做的。
还可以使用一下bitset的容器进一步省内存。
有空继续优化一下。
2 0
- Timus 1119. Metro 动态规划
- Timus 1119. Metro
- Timus 1119. Metro
- Timus Online Judge:1119. Metro
- 1119. Metro(动态规划,滚动数组)
- URAL 1119 Metro (动态规划)
- timus 1002. Phone Numbers(KMP&动态规划)
- URAL 1119 Metro (DP动态规划)
- UVA 1025 - A Spy in the Metro(动态规划)
- UVa 1025 - A Spy in the Metro(动态规划)
- 动态规划_DAG例题_A Spy in the Metro(UVA1025)
- 动态规划_DAG例题_A Spy in the Metro(UVA1025)
- UVA 1025 A Spy in the Metro [动态规划] [DAGdp]
- UVa 1025 -A Spy in the Metro(动态规划)
- UVA 1025 A Spy in the Metro (动态规划)
- 【算法设计与数据结构】动态规划入门——URAL 1119 Metro
- uva 1025 A Spy in the Metro DAG上的动态规划(固定终点)
- 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍
- JVM什么时候会触发Full GC
- 关于java 内存泄露的有用的文章
- php生成二维码带logo
- Android Volley完全解析(二),使用Volley加载网络图片
- 统计分析SQL Server Profiler 跟踪的SQL
- Timus 1119. Metro 动态规划
- iOS文件系统的管理
- ASP.NET 应用程序遭遇Server Application Unavailable问题的解决办法
- 应用层协议实现系列(二)——HTTP服务器之http协议解析
- 何时用类?何时用接口?
- AT&T将推机舱WiFi服务 同时实时传回飞机数据
- ubuntu12.04安装opencv2.4.1
- Android 程序框架设计
- Core Animation之多种动画效果