DP(递归打印路径) UVA 662 Fast Food
题意:n个饭店在一条直线上,给了它们的坐标,现在要建造m个停车场,饭店没有停车场的要到最近的停车场,问所有饭店到停车场的最短距离
分析:易得区间(i, j)的最短距离和一定是建在(i + j) / 2的饭店,预处理出(i, j)的距离和sum[i][j],mark[i][j] 表示区间的最优停车场的位置,mid[i][j]表示(i + j) / 2。状态转移方程:dp[i][j] = max (dp[k-1][j-1] + sum[k][i]);
收获:学习递归打印路径
代码:
/************************************************
* Author :Running_Time
* Created Time :2015-8-29 16:42:33
* File Name :UVA_662.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 2e2 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int dp[N][33];
int sum[N][N], d[N], mark[N][N], mid[N][N];
int n, m;
int idx; void print(int i, int j) {
if (i < 1 || j < 1) return ;
print (mark[i][j]-1, j-1);
printf ("Depot %d at restaurant %d serves restaurant", ++idx, mid[mark[i][j]][i]);
if (mark[i][j] == i) {
printf (" %d\n", i); return ;
}
else printf ("s %d to %d\n", mark[i][j], i);
} int main(void) {
int cas = 0;
while (scanf ("%d%d", &n, &m) == 2) {
if (!n && !m) break;
for (int i=1; i<=n; ++i) scanf ("%d", &d[i]);
memset (sum, 0, sizeof (sum));
for (int i=1; i<=n; ++i) {
mid[i][i]= i;
for (int j=i+1; j<=n; ++j) {
int mm = (i + j) >> 1;
mid[i][j] = mm;
for (int k=i; k<=j; ++k) {
sum[i][j] += abs (d[mm] - d[k]);
}
}
}
memset (dp, INF, sizeof (dp));
memset (dp[0], 0, sizeof (dp[0]));
for (int i=1; i<=n; ++i) {
for (int j=1; j<=m; ++j) {
for (int k=1; k<=i; ++k) {
int tmp = dp[k-1][j-1] + sum[k][i];
if (dp[i][j] >= tmp) {
dp[i][j] = tmp;
mark[i][j] = k;
}
}
}
}
printf ("Chain %d\n", ++cas);
idx = 0; print (n, m);
printf ("Total distance sum = %d\n\n", dp[n][m]);
} return 0;
}
DP(递归打印路径) UVA 662 Fast Food的更多相关文章
- poj 1141 区间dp+递归打印路径
Brackets Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30383 Accepted: 871 ...
- UVA 1626 区间dp、打印路径
uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...
- UVA 531 - Compromise(dp + LCS打印路径)
Compromise In a few months the European Currency Union will become a reality. However, to join th ...
- FatMouse's Speed ~(基础DP)打印路径的上升子序列
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...
- UVA 10054 The Necklace(欧拉回路,打印路径)
题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- zoj 3088 Easter Holidays(最长路+最短路+打印路径)
Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...
- L2-001. 紧急救援 (Dijkstra算法打印路径)
作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...
- UVa 103 - Stacking Boxes (LIS,打印路径)
链接:UVa 103 题意:给n维图形,它们的边长是{d1,d2,d3...dn}, 对于两个n维图形,求满足当中一个的全部边长 依照随意顺序都一一相应小于还有一个的边长,这种最长序列的个数,而且打 ...
- Uva 10131 Is Bigger Smarter? (LIS,打印路径)
option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意: ...
随机推荐
- 在SUSE12中使用 Machinery 进行高级系统管理
简单介绍 在 SUSE Linux Enterprise 12 中.SUSE 如今推出了面向系统管理员的 Machinery.作为其高级系统管理模块的一部分.Machinery 是适用于 Linux ...
- Friefox清除旧的网页缓存
Ctrl + F5 适用于调试网页编码时,不断以旧设置显示页面
- Null value was assigned to a property of primitive type setter of原因及解决
出现Null value was assigned to a property of primitive type setter of错误是由于类型不匹配,将字段的属性由hibernate的int类型 ...
- Cocostudio 1.4 实现的DemoShop
开发环境是CocoStudio 1.4 + Cocos2dx 2.2 把项目文件放到Cocos2dx下的projects文件夹下就可以执行了 压缩包里面包括了 源码 和资源文件 执行效果: 初始化界 ...
- Project Euler:Problem 61 Cyclical figurate numbers
Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...
- 自动填充输入框 Asp .Net Mvc
1 效果 当在一个文本框中输入时,可以自动查找相关选项,然后加载出来以供参考 2 前台代码 <link href="~/Content/themes/base/jquery-u ...
- 【SICP练习】149 练习4.5
练习4-5 原文 Exercise 4.5. Scheme allows an additional syntax for cond clauses, ( => ). If evaluates ...
- maven 项目 spring mvc + jdbc 配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 在 Ubuntu 开启 GO 程序编译之旅
本文将使用 putty 连接到一台阿里云 Ubuntu 16.04 服务器,在其上安装 go 语言的编译环境,旨在呈现从安装到"你好,世界!"涉及的方方面面,希望完成这个过程无须觅 ...
- C++实现KMP模式匹配算法
#include<iostream> #include<string> #include<vector> using namespace std; void Nex ...