快餐店之间插入仓库,路最短,DP,POJ(1485)
2016-05-29 21:03
387 查看
题目链接:http://poj.org/problem?id=1485
暂时我还没想出思路求路径。哈哈哈,先写一下中间步骤吧。
暂时我还没想出思路求路径。哈哈哈,先写一下中间步骤吧。
#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; int d[210]; ///沿高速公路n各快餐店的位置 int cost[210][210]; ///cost[i][j]表示任意两个快餐店之间建一个仓库,所有饭店到该仓库的距离之和 int dis[210][210]; ///dis[i][j]表示前i个仓库与前j个快餐店之间的最小距离 int n,k; int main() { while(scanf("%d%d",&n,&k),n!=0||k!=0) { for(int i=1;i<=n;i++) scanf("%d",&d[i]); memset(cost,0,sizeof(cost)); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { int tmp = (i+j)/2; for(int m=i;m<=j;m++) cost[i][j]+=abs(d[m]-d[tmp]); } } memset(dis,1,sizeof(dis)); for(int i=1;i<=n;i++) dis[1][i] = cost[1][i]; for(int i=2;i<=k;i++) ///仓库 { for(int j=i;j<=n;j++) ///快餐店 { for(int m=i-1;m<j;m++) { int tmp = dis[i-1][m] + cost[m+1][j]; if(tmp<dis[i][j]) dis[i][j] = tmp; } } } printf("%d\n",dis[k] ); } return 0; }
相关文章推荐
- ZZULIOJ 1432: 背包again <类似进制问题>
- url 重写,设置伪静态页面
- 23种设计模式01--简单工厂
- 多重背包问题
- Tinyhttp服务器编译运行
- eerror: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCo
- C51学习笔记
- C++走向远洋——57(项目二2、动物这样叫、抽象类)
- win10安装mysql解压缩安装数据库成功之后无法登陆
- 分治算法主定理
- 强悍的命令行 —— 强悍的 cd
- P105 5.9输入4个整数,要求按由小到大的顺序输出
- Qt/C++ 图片分割~
- python unittest 之mock学习笔记(续二)
- Twisted源码分析4--Deferred
- 数据库使用
- mysql维护记录
- scala学习之scala与java的一些区别(一)
- Volley 的使用和执行流程分析
- linux chrome标签页乱码