(记忆化搜索)Jury Compromise (poj 1015)
http://acm.fzu.edu.cn/problem.php?pid=1005
Description
The fastfood chain McBurger owns several restaurants along a highway. Recently, they have decided to build several depots along the highway, each one located at a restaurant and supplying several of the restaurants with the needed ingredients. Naturally, these depots should be placed so that the average distance between a restaurant and its assigned depot is minimized. You are to write a program that computes the optimal positions and assignments of the depots.
To make this more precise, the management of McBurger has issued the following specification: You will be given the positions of n restaurants along the highway as n integers d1 < d2 < ... < dn (these are the distances measured from the company's headquarter, which happens to be at the same highway). Furthermore, a number k (k <= n) will be given, the number of depots to be built.
The k depots will be built at the locations of k different restaurants. Each restaurant will be assigned to the closest depot, from which it will then receive its supplies. To minimize shipping costs, the total distance sum, defined as
must be as small as possible.
Write a program that computes the positions of the k depots, such that the total distance sum is minimized.
Input
The input file will end with a case starting with n = k = 0. This case should not be processed.
Output
Output a blank line after each test case.
Sample Input
Sample Output
n个旅馆和k个补给站的问题
假设有3个旅馆坐标分别是 1, 4, 5, 和2个补给站,那么路程代价就是1了,一个补给站放在坐标为1的旅馆那,令一个放在4位置处。
也可以一个补给站放在坐标为 1 的旅馆那,令一个放在 5 位置处。
//dp[i][k]表示前i个店添加k个供应点所达到的最小值
//状态转移方程为:dp[i][k] = min(dp[j][k-1], sum[j+1][i]),
//其中k-1 <= j <= i-1, sum[i][j]表示从第i个饭店到第j个饭店添加一个供应点所达到的最小值,取i,j中间值即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std; #define N 220
#define MOD 1000000007
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0x3f3f3f3f int dp[N][N], sum[N][N], a[N]; int main()
{
int n, m, iCase=; while(scanf("%d%d", &n, &m), n||m)
{
int i, j, k; for(i=; i<=n; i++)
scanf("%d", &a[i]); for(i=; i<=n; i++)
{
sum[i][i] = ;
for(j=i+; j<=n; j++)
{
sum[i][j] = sum[i][j-] + a[j] - a[(i+j)/];
}
} for(i=; i<=n; i++)
for(j=; j<=m; j++)
dp[i][j] = INF; dp[][] = ;
for(i=; i<=n; i++)
{
for(k=; k<=m; k++)
{
for(j=k-; j<i; j++)
{
dp[i][k] = min(dp[i][k], dp[j][k-]+sum[j+][i]);
}
}
} printf("Chain %d\n", iCase++);
printf("Total distance sum = %d\n\n", dp[n][m]);
}
return ;
}
记忆化搜索:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
using namespace std; #define N 220
#define met(a, b) memset(a, b, sizeof(a))
#define INF 0xffffff
const long long Max = ;
typedef long long LL; int a[N], sum[N][N], dp[N][N];
int n, m; int DFS(int x, int y)
{
int j; if(x< || y<) return INF; if(dp[x][y]!=INF) return dp[x][y]; if(y>=x) ///这点我还是想不到
{
dp[x][y] = ;
return ;
} for(j=; j<=x; j++) ///在 [1,x] 中选择一个点作为补给站
dp[x][y] = min(dp[x][y], DFS(j-, y-) + sum[j][x]); return dp[x][y];
} int main()
{
int iCase=; while(scanf("%d%d", &n, &m), n||m)
{
int i, j; met(sum, );
met(a, ); for(i=; i<=n; i++)
scanf("%d", &a[i]); for(i=; i<=n; i++)
for(j=i+; j<=n; j++)
sum[i][j] = sum[i][j-] + a[j]-a[(i+j)/]; for(i=; i<=n; i++)
for(j=; j<=m; j++)
dp[i][j] = INF; dp[n][m] = DFS(n, m); printf("Chain %d\n", iCase++);
printf("Total distance sum = %d\n\n", dp[n][m]);
}
return ;
}
(记忆化搜索)Jury Compromise (poj 1015)的更多相关文章
- Jury Compromise POJ - 1015 dp (标答有误)背包思想
题意:从 n个人里面找到m个人 每个人有两个值 d p 满足在abs(sum(d)-sum(p)) 最小的前提下sum(d)+sum(p)最大 思路:dp[i][j] i个人中 和 ...
- POJ 1088 滑雪(记忆化搜索)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 92384 Accepted: 34948 Description ...
- poj 3249 Test for Job (DAG最长路 记忆化搜索解决)
Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8990 Accepted: 2004 Desc ...
- (区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)
http://poj.org/problem?id=3186 Description FJ has purchased N (1 <= N <= 2000) yummy treats ...
- poj 3249(bfs+dp或者记忆化搜索)
题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...
- POJ 1191 棋盘分割 【DFS记忆化搜索经典】
题目传送门:http://poj.org/problem?id=1191 棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
- POJ 2704 Pascal's Travels 【DFS记忆化搜索】
题目传送门:http://poj.org/problem?id=2704 Pascal's Travels Time Limit: 1000MS Memory Limit: 65536K Tota ...
- POJ 1579 Function Run Fun 【记忆化搜索入门】
题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Tota ...
- POJ 1088 滑雪 DFS 记忆化搜索
http://poj.org/problem?id=1088 校运会放假继续来水一发^ ^ 不过又要各种复习,功课拉下了许多 QAQ. 还有呀,就是昨天被一个学姐教育了一番,太感谢了,嘻嘻^ ^ 好了 ...
随机推荐
- Linux netstat
一.简介 二.语法 三.实例 1)查看TCP连接数 netstat -n | awk '/^tcp/ {++S[$NF]} END {for (a in S) print a, S[a]}'
- python消息队列Queue
实例1:消息队列Queue,不要将文件命名为"queue.py",否则会报异常"ImportError: cannot import name 'Queue'" ...
- 图片延时加载原理 和 使用jquery实现的一个图片延迟加载插件(含图片延迟加载原理)
图片加载技术分为:图片预加载和图片延时加载. javascript图片预加载和延时加载的区别主要体现在图片传输到客户端的时机上,都是为了提升用户体验的,延时加载又叫懒加载.两种技术的本质:两者的行为是 ...
- 转~Jenkins pipeline:pipeline 使用之语法详解
一.引言 Jenkins 2.0的到来,pipline进入了视野,jenkins2.0的核心特性. 也是最适合持续交付的feature. 简单的来说,就是把Jenkins1.0版本中,Project中 ...
- usr/include/c++/6.4.1/bits/stl_relops.:67: Parse error at "std"
问题描述: 1.编译某qt工程的32位架构二进制包时,出现了上面错误,具体错误信息如下 qmake-qt5 -o ProductLicense/Makefile ProductLicense/Prod ...
- windows下Git的使用教程(github)
这表文章主要是用命令操作: 使用可视化软件操作:https://www.cnblogs.com/mswyf/p/9261859.html 一.下载安装Git Bash 下载安装:https://www ...
- idea单元测试左侧装订线中的颜色指示器设置
又是idea,idea确实很智能,由于我下载的idea设置可能初始化了,所以我找不到单元测试率覆盖的具体代码情况,到底哪些代码覆盖,哪些代码未覆盖:
- Max Chunks To Make Sorted II LT768
This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...
- zookeeper相关
1.zookeeper应用:集群节点间的数据同步(资源管理),分布式锁(主要是利用客户端在一个会话中在zookeeper中创建一个znode节点,然后再去执行自己的业务代码,比如去更新数据库,其他客户 ...
- Python3实战系列之二(获取印度售后数据项目)
问题:续接上一篇.说干咱就干呀,勤勤恳恳写程序呀! 目标:安装python和pycharm.要编写并运行python程序就需要电脑有开发工具和运行环境,所以此篇就是安装编辑和运行python程序的软件 ...