题目传送门

题意: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的更多相关文章

  1. poj 1141 区间dp+递归打印路径

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30383   Accepted: 871 ...

  2. UVA 1626 区间dp、打印路径

    uva 紫书例题,这个区间dp最容易错的应该是(S)这种匹配情况,如果不是题目中给了提示我就忽略了,只想着左右分割忘记了这种特殊的例子. dp[i][j]=MIN{dp[i+1][j-1] | if( ...

  3. UVA 531 - Compromise(dp + LCS打印路径)

      Compromise  In a few months the European Currency Union will become a reality. However, to join th ...

  4. FatMouse's Speed ~(基础DP)打印路径的上升子序列

    FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take ...

  5. UVA 10054 The Necklace(欧拉回路,打印路径)

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. zoj 3088 Easter Holidays(最长路+最短路+打印路径)

    Scandinavians often make vacation during the Easter holidays in the largest ski resort Are. Are prov ...

  7. L2-001. 紧急救援 (Dijkstra算法打印路径)

    作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标在地图上.当其他城市有紧急求 ...

  8. UVa 103 - Stacking Boxes (LIS,打印路径)

    链接:UVa 103 题意:给n维图形,它们的边长是{d1,d2,d3...dn},  对于两个n维图形,求满足当中一个的全部边长 依照随意顺序都一一相应小于还有一个的边长,这种最长序列的个数,而且打 ...

  9. Uva 10131 Is Bigger Smarter? (LIS,打印路径)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=1072">链接:UVa 10131 题意: ...

随机推荐

  1. 【转】apache storm 内置的定时机制

    原文:http://www.cnblogs.com/kqdongnanf/p/4778672.html ------------------------------------------------ ...

  2. ubuntu磁盘分区和挂载

  3. 每天一个JavaScript实例-获取元素当前高度

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. VS code - code Snippet

    For anyone working on the UI and using VS Code, you can create a user Snippet and keyboard shortcut ...

  5. 深刻理解Java中形參与实參,引用与对象的关系

    声明:本博客为原创博客,未经同意.不得转载! 原文链接为http://blog.csdn.net/bettarwang/article/details/30989755 我们都知道.在Java中,除了 ...

  6. 点滴记录——Ubuntu 14.04中Solr与Tomcat整合安装

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/38965981 1. 安装jdk,tomcat     sudo apt-get instal ...

  7. 使用JS对select标签进行联动选择

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  8. 【Java 安全技术探索之路系列:J2SE安全架构】之二:安全管理器

    作者:郭嘉 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells github:https://github.com/AllenWell 一 ...

  9. 将项目上传到GitHub

    第一步: 1.进入Github首页,点击New repository新建一个项目 2.填写相应信息后点击create即可 Repository name: 仓库名称 Description(可选): ...

  10. noteexpress使用指南

    软件功能:在写论文时直接调用参考数据并输出正规的格式. (以下简称NE) A.下载安装 下载地址:Note-express - Bibliography Software  选择相应的学校进行下载,相 ...