zoukankan      html  css  js  c++  java
  • POJ 1160Post Office【DP】

    Description

    There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same position. The distance between two positions is the absolute value of the difference of their integer coordinates.

    Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village and its nearest post office is minimum.

    You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office.

    Input

    Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.

    Output

    The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.

    Sample Input

    10 5
    1 2 3 6 7 9 11 22 44 50

    Sample Output

    9

    Post Office

    题意:在V个村庄上建立P个邮局,使每个村庄到达邮局的最小距离最小。

    思路:dp[i][j]表示前i个村庄,建立j个邮局,sum[i][j]表示i, j之间建立1个邮局的最小距离和,sum[i][j]=sum[i][j-1]+p[j]-p[(i+j)/2];//HRBUST 有一利用此公式的题目

    样例解析: 1     2     3 6  7  9 11  22   44     50

                                建1     建2     建3  建4   建5

    状态转移方程为:dp[i][j]=min(dp[i][j], dp[k][j-1]+sum[k+1][i]);

    代码如下:

    #include<string.h>
    #include<iostream>
    #include<stdio.h>
    using namespace std;
    int main()
    {
    	int i, j, k, v, p;
    	int sum[302][302], pp[302], dp[302][32];
    	while(scanf("%d%d", &v, &p)!=EOF)
    	{
    		memset(sum, 0, sizeof(sum));
    		memset(pp, 0, sizeof(pp));
    		memset(dp, 0, sizeof(dp));
    		for(i=1; i<=v; i++) 
    			scanf("%d", &pp[i]);
    		for(i=1; i<=v; i++)
    		{
    			for(j=i; j<=v; j++)
    				sum[i][j]=sum[i][j-1]+pp[j]-pp[(i+j)/2];
    		}
    		for(i=1; i<=v; i++)
    			dp[i][1]=sum[1][i];
    		for(j=2; j<=p; j++)		
    			for(i=1; i<=v; i++)
    			{ 
    				int minnum=0xfffffff;
    				for(k=j-1; k<i; k++)
    				{
    					minnum=min(minnum, dp[k][j-1]+sum[k+1][i]);
    				}
    				dp[i][j]=minnum;
    			}
    		printf("%d\n", dp[v][p]);
    	}
    	return 0;
    }

     

  • 相关阅读:
    A survey of best practices for RNA-seq data analysis RNA-seq数据分析指南
    DART: a fast and accurate RNA-seq mapper with a partitioning strategy DART:使用分区策略的快速准确的RNA-seq映射器
    中科院生物信息学题目整理
    生物信息学题目整理: 陈润生
    第六章 Windows应用程序对键盘与鼠标的响应 P121 6-8
    第七章 资源在Windows编程中的应用 P157 7-8
    第四章 Windows的图形设备接口及Windows绘图 P83 4-6
    Android Fragment 你应该知道的一切
    Android Fragment 真正的完全解析(下)
    Android Fragment 真正的完全解析(上)
  • 原文地址:https://www.cnblogs.com/Hilda/p/2616698.html
Copyright © 2011-2022 走看看