POJ 1160 DP
来源:互联网 发布:火炬之光2mac版本更新 编辑:程序博客网 时间:2024/05/22 00:25
用数轴描述一条高速公路,有V个村庄,每一个村庄坐落在数轴的某个点上,需要选择P个村庄在其中建立邮局,要求每个村庄到最近邮局的距离和最小。
cost记录每两个村庄之间建一个邮局的最小代价。
转移方程: dp[j][i]=Min(dp[j][i],dp[k][i-1]+cost[k+1][j])
前j个村庄建i个邮局=前k个村庄建I-1个邮局+第k+1村庄到第j村庄建一个邮局的代价
#include "stdio.h"#include "string.h"int inf=99999999;int Abs(int a){ if (a<0) return -a;else return a;}int Min(int a,int b){ if (a<b) return a;else return b;}int main(){ int n,m,i,j,mid,k; int dp[310][31],a[31],cost[310][310]; while (scanf("%d%d",&n,&m)!=EOF) { for (i=1;i<=n;i++) scanf("%d",&a[i]); memset(cost,0,sizeof(cost)); for (i=1;i<=n;i++) for (j=i;j<=n;j++) { mid=(i+j)/2; for (k=i;k<=j;k++) cost[i][j]+=Abs(a[k]-a[mid]); } for (i=1;i<=n;i++) for (j=1;j<=m;j++) dp[i][j]=inf; for (i=1;i<=n;i++) dp[i][1]=cost[1][i]; for (i=2;i<=m;i++) for (j=i;j<=n;j++) for (k=i;k<=j;k++) dp[j][i]=Min(dp[j][i],dp[k][i-1]+cost[k+1][j]); printf("%d\n",dp[n][m]); } return 0;}
0 0
- poj 1160(dp)
- POJ 1160 dp
- poj 1160 DP
- POJ 1160 DP
- POJ 1160 DP
- poj 1160 DP
- POJ 1160 DP
- poj-1160 post office (dp)
- Post Office - POJ 1160 dp
- 【DP】 POJ 1160 Post Office
- POJ 1160 Post Office DP
- POJ 1160 Post Office (DP)
- POJ 1160 dp + 四边形优化
- poj 1160 post office------DP
- POJ 1160 DP 解题报告
- poj dp
- 【dp】POJ
- 【dp】POJ
- 前端学习笔记
- imo突然登陆非常慢卡死的问题
- 感知器算法
- 读取本地文件更改文件名 Android
- 转载:机器学习中的相似性度量
- POJ 1160 DP
- Android 对话框(Dialog)大全 建立你自己的对话框
- 《设计模式之禅》依赖倒置原则之问
- 经验分享:CSS浮动(float,clear)通俗讲解
- android之AlertDialog
- glassfish4.0配置
- box2d实力触摸不规则图片
- 第162天
- WebRTC中的AppRTCDemo.apk简单分析