poj - 1160 - Post Office(dp)

题意:一条直路上有V个村庄(1 <= V <= 300),现在建P个邮局(1 <= P <= 30),求每个村庄到其最近邮局的最短距离和的最小值。

题目链接:http://poj.org/problem?id=1160

——>>状态:dp[i][j]表示前i个村庄建j个邮局的最短距离和。

状态转移方程:dp[i][j] = min(dp[i][j], dp[k][j - 1] + nDis[k + 1][i]);(前k个村庄由前j - 1个邮局管,后面的村庄归最后一个邮局管)

状态:nDis[i][j]表示第i个村庄到第j个村庄建1个邮局的最短距离和。

状态转移方程:nDis[i][j] = nDis[i][j-1] + x[j] - x[(i + j) / 2];

总时间复杂度:O(V * V * P)

#include <cstdio>
#include <algorithm>
#include <cstring>

using std::min;

const int MAXV = 300 + 1;
const int MAXP = 30 + 1;

int x[MAXV];
int dp[MAXV][MAXP];
int nDis[MAXV][MAXV];

void Read(int V)
{
    for (int i = 1; i <= V; ++i)
    {
        scanf("%d", x + i);
    }
}

void Init(int V)
{
    memset(nDis, 0, sizeof(nDis));
    for (int i = 1; i <= V; i++)
    {
        for (int j = i; j <= V; j++)
        {
            nDis[i][j] = nDis[i][j-1] + x[j] - x[(i + j) / 2];
        }
    }
}

void Dp(int V, int P)
{
    memset(dp, 0x3f, sizeof(dp));
    for (int i = 0; i <= P; ++i)
    {
        dp[i][i] = 0;
    }
    for (int i = 1; i <= V; ++i)
    {
        // dp[i][i] = 0; 这里初始化会越界,小心!
        for (int j = 1; j <= P && j <= i; ++j)
        {
            for (int k = j - 1; k < i; ++k)
            {
                dp[i][j] = min(dp[i][j], dp[k][j - 1] + nDis[k + 1][i]);
            }
        }
    }
}

void Output(int V, int P)
{
    printf("%d\n", dp[V][P]);
}

int main()
{
    int V, P;

    while (scanf("%d%d", &V, &P) == 2)
    {
        Read(V);
        Init(V);
        Dp(V, P);
        Output(V, P);
    }

    return 0;
}
时间: 2024-10-02 18:00:10

poj - 1160 - Post Office(dp)的相关文章

POJ 1160 Post Office (动态规划)

Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15412   Accepted: 8351 Description There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each villa

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

题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i][j]表示到第i个村庄为止建立j个邮局的最小距离和,dis[i][j]表示i~j之间建一个邮局的最小距离和,我们很容易得出状态转移方程:dp[i][j]=min{dp[k][j]+dis[k+1][i]}(k<i). 主要是dis[i][j]的预处理很巧妙,从别人的博客上看的“将邮局建在i~j中间即

POJ 2948 Martian Mining(DP)

题目链接 题意 : n×m的矩阵,每个格子中有两种矿石,第一种矿石的的收集站在最北,第二种矿石的收集站在最西,需要在格子上安装南向北的或东向西的传送带,但是每个格子中只能装一种传送带,求最多能采多少矿. 思路 :记忆化搜索.也可以用递推. //2948 #include <stdio.h> #include <string.h> #include <iostream> using namespace std ; int yeye[510][510] ,blog[510]

【POJ 3071】 Football(DP)

[POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted: 2222 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all tea

【POJ 3034】 Whac-a-Mole(DP)

[POJ 3034] Whac-a-Mole(DP) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3621   Accepted: 1070 Description While visiting a traveling fun fair you suddenly have an urge to break the high score in the Whac-a-Mole game. The goal of the W

POJ 3280 Cheapest Palindrome(DP)

题目链接 题意 :给你一个字符串,让你删除或添加某些字母让这个字符串变成回文串,删除或添加某个字母要付出相应的代价,问你变成回文所需要的最小的代价是多少. 思路 :DP[i][j]代表的是 i 到 j 这一段位置变成回文所需的最小的代价. 1 //3280 2 #include <stdio.h> 3 #include <string.h> 4 #include <iostream> 5 6 using namespace std ; 7 8 char sh[2100]

poj 1160 Post Office(邮局问题)

Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17110   Accepted: 9226 Description There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each villa

poj - 1093 - Formatting Text(dp)

题意:输入一段短文(所有字符总数不超过10000),要求格式化成两端对齐(每行长度为n,1 <= n <= 80)的方式输出并使得总坏值最小(一个空隙的坏值是这个空隙的空格总数减1后的平方),若有多种方案输出空格数字典序最小方案. 题目链接:http://poj.org/problem?id=1093 -->>状态:dp[i]表示从第i个单词开始到最后一个单词的最小总坏值(第i个单词是这一行的第1个单词) 状态转移方程:dp[i] = min(dp[i], dp[j + 1] +

POJ 1260:Pearls(DP)

http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8474   Accepted: 4236 Description In Pearlania everybody is fond of pearls. One company, called The Royal Pearl, produces a lot of jewelry with pearls