POJ 1160    Post Office

我不知道优化,我只知道最暴力的方法,O(V^3),居然100ms不到的过了

设DP[i][j][k]表示考虑前i个小镇,放了j个邮局,最后一个邮局的所在城镇编号为k的k以前的所有的城镇距离最近的邮局的最小距离和(TM自己给自己搞了个这么绕的状态。。)

那么有DP[i][j][i] = MIN{DP[i-1][j-1][k] + dis[k][i]}其中k<i

   DP[i][j][k] = DP[i-1][j][k]

其中,dis[k][i]表示[k, i]这个区间里的所有城镇距离 邮局k 和 邮局i 的最小值的和,即:

dis[i][j] = sum{ MIN(x[k]-x[i], x[j]-x[k]) } i<=k<=j

考虑边界条件,对于DP[i][j][k],由于如果前i个城镇一个都不放,那么k取任意值都是不合法的(因为我是这么表示状态的2333333..),所以对于任意的i,先算一边j=1的情况,即DP[i][1][k] = dis[0][k],默认的x[0] = -INF

最后的答案就是:  MIN{DP[V][P][k] + dis[k][V]}    P <=k<=V

其他的按照上面的思路敲就是了,然后我就愉快的提交了,TM居然告诉我内存超了300 × 300 × 30 啊,怎么会超!!!TM内存只给了10000,没办法就只好将第一维大小压缩到2(因为当前的i只与i-1有关。。)

79ms,O(V^3),我愉快的笑了

 #include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 100000000
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
template<class T> T CMP_MIN ( T a, T b ) { return a < b; }
template<class T> T CMP_MAX ( T a, T b ) { return a > b; }
template<class T> T MAX ( T a, T b ) { return a > b ? a : b; }
template<class T> T MIN ( T a, T b ) { return a < b ? a : b; }
template<class T> T GCD ( T a, T b ) { return b ? GCD ( b, a % b ) : a; }
template<class T> T LCM ( T a, T b ) { return a / GCD ( a, b ) * b; }
template<class T> T SWAP( T& a, T& b ) { T t = a; a = b; b = t; } //typedef __int64 LL;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-; int V, P;
int x[], DP[][][];
int dis[][], pre[]; void initDis()
{
mem0(dis);mem0(pre);
for(int i=;i<=V;i++)
for(int j=i;j<=V;j++)
for(int k=i;k<=j;k++)
dis[i][j] += MIN(x[k]-x[i], x[j]-x[k]);
for(int i=;i<=V;i++)for(int j=i;j>=;j--)
pre[i] += x[i] - x[j];
} int main()
{
//FOPENIN ( "in.txt" );
//FOPENOUT("out.txt");
while(~scanf("%d %d", &V, &P))
{
for(int i=;i<=V;i++)
scanf("%d", &x[i]);
initDis();
for(int i=;i<;i++)
for(int j=;j<=MIN(i,P);j++)
for(int k=;k<=i;k++){
DP[i][j][k] = INF;
}
int now = ;
for(int i=;i<=V;i++)
{
now = !now;
int s = ;
for(int j=;j<=i;j++) DP[now][][j] = pre[j];
for(int j=;j<=MIN(i, P); j++)
{
DP[now][j][i] = INF;
for(int k=j-;k<i;k++)if(DP[!now][j-][k] != INF)
{
DP[now][j][i] = MIN(DP[now][j][i], DP[now][j-][k] + dis[k][i]);
}
for(int k=j;k<i;k++) DP[now][j][k] = DP[!now][j][k];
}
}
int ans = INF;
for(int k=P;k<=V;k++)if(DP[now][P][k] != INF)
{
int s = ;
for(int j = k + ; j <= V; j ++ ) s += x[j] - x[k];
ans = MIN(ans, DP[now][P][k] + s);
}
printf("%d\n", ans);
}
return ;
}

POJ 1160Post Office的更多相关文章

  1. POJ 1160 Post Office(区间DP)

    Description There is a straight highway with villages alongside the highway. The highway is represen ...

  2. POJ——T 1160 Post Office

    http://poj.org/problem?id=1160 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20218   ...

  3. 【POJ】1160 Post Office

    http://poj.org/problem?id=1160 题意:直线上有n个城市,其中有p个城市有邮局,问如何建p个邮局使得每个城市到最近的邮局和最小.(n<=300, p<=30&a ...

  4. 【POJ】【1160】Post Office

    DP/四边形不等式 邮局,经典的四边形不等式例题! 关于四边形不等式的学习请看 赵爽论文<动态规划加速原理之四边形不等式> 题目总结&题解:http://blog.csdn.net ...

  5. POJ 1160 Post Office (动态规划)

    Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15412   Accepted: 8351 Desc ...

  6. poj 1160 Post Office (间隔DP)

    Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15966   Accepted: 8671 Desc ...

  7. [IOI 2000]POJ 1160 Post Office

    Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22278 Accepted: 12034 Descrip ...

  8. POJ 1160 Post Office(DP+经典预处理)

    题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i] ...

  9. POJ 1160 Post Office (四边形不等式优化DP)

    题意: 给出m个村庄及其距离,给出n个邮局,要求怎么建n个邮局使代价最小. 析:一般的状态方程很容易写出,dp[i][j] = min{dp[i-1][k] + w[k+1][j]},表示前 j 个村 ...

随机推荐

  1. ecshop 用户名和邮箱都能登陆

    1.打开/includes/modules/integrates/integrate.php文件,并找到下面代码 if ($this->check_user($username, $passwo ...

  2. JVM——类加载机制

    虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制. 在Java语言中,类型的加载.连接和初始化过 ...

  3. Hadoop2.6.0完全分布式安装

    本文地址:http://www.cnblogs.com/myresearch/p/hadoop-full-distributed-operation.html,转载请注明源地址. 我这边是使用了两台主 ...

  4. 502 Bad Gateway nginx 解决

    打开 /usr/local/php/etc/php-fpm.conf 调大以下两个参数(根据服务器实际情况,过大也不行) <value name=”max_children”>5120&l ...

  5. 在Ubuntu中用root帐号登录

    一.其实我个人认为这没有多大必要,因为当你需要 root 的权限时,使用 sudo 便可以了.如果你实在需要在 Ubuntu 中启用 root 帐号的话,那么不妨执行下面的操作: 1.重新设置 roo ...

  6. hdu 3478(判断奇环)

    题意:给你一个无向图,问你有没有可能存在一个奇环连接所有的节点. 分析:好久没写博客了,这个好习惯还是要继续保持的!这道题通过转化之后就是问你有没有存在一个奇环连接所有的节点,这里用到的方法是染色法, ...

  7. UIImageView 点击放大缩小

    static CGRect oldframe; -(void)showImage:(UIImageView *)avatarImageView{ UIImage *image=avatarImageV ...

  8. Http中Cookie和Session介绍

    先介绍下B/S系统的工作的完整过程.首先客户端的浏览器发出请求,服务端的webserver接受到请求后,调用相关请求的页面进行处理,处理完后将结果发送给客户端的浏览器进行显示.只能是浏览器向webse ...

  9. <测试用例设计>用户及权限管理功能常规测试方法

    1)  赋予一个人员相应的权限后,在界面上看此人员是否具有此权限,并以此人员身份登陆,验证权限设置是否正确(能否超出所给予的权限): 2)  删除或修改已经登陆系统并正在进行操作的人员的权限,程序能否 ...

  10. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 unselect_from_list_by_label(self, locator, *labels)

    def unselect_from_list_by_label(self, locator, *labels): """Unselects `*labels` from ...