dp +路径输出

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <vector>
#include <sstream>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <iostream>
#define FFI freopen("in.txt", "r", stdin)
#define maxn 1010
#define INF 0x3f3f3f3f
#define inf 10000000
#define MOD 1000000007
#define ULL unsigned long long
#define LL long long
#define _setm(houge) memset(houge, INF, sizeof(houge))
#define _setf(houge) memset(houge, -1, sizeof(houge))
#define _clear(houge) memset(houge, 0, sizeof(houge))
using namespace std; int dp[35][205], n, k;
int loc[210], w[210][210]; void fun(int m,int x,int y) {
if(x == y)
printf("Depot %d at restaurant %d serves restaurant %d\n", m, x, y);
else
printf("Depot %d at restaurant %d serves restaurants %d to %d\n", m, (x+y)/2, x, y);
} void print(int x, int y, int sum) {
if(x == 1) {
fun(x, x, y);
return;
}
else {
for(int i = x; i <= y; ++ i) {
if(w[i][y] + dp[x-1][i-1] == sum) {
print(x-1, i-1, sum-w[i][y]);
fun(x, i, y);
break;
}
}
}
} int main() {
FFI;
int ca = 0;
while(scanf("%d%d", &n, &k) == 2 && n+k) {
for(int i = 1; i <= n; ++ i) {
scanf("%d", &loc[i]);
}
_clear(w);
_setm(dp);
for(int i = 1; i <= n; ++ i) {
for(int j = i; j <= n; ++ j) {
for(int d = i; d <= j; ++ d) {
w[i][j] += abs(loc[d]-loc[(i+j)/2]);
}
}
}
for(int i = 1; i <= n; ++ i) dp[1][i] = w[1][i];
for(int i = 2; i <= k; ++ i) {
for(int j = n; j >= i; -- j) {
for(int d = i-1; d <= j; ++ d) {
dp[i][j] = min(dp[i][j], dp[i-1][d]+w[d+1][j]);
}
}
}
printf("Chain %d\n", ++ ca);
print(k, n, dp[k][n]);
printf("Total distance sum = %d\n\n", dp[k][n]);
}
return 0;
}

  

uva 662的更多相关文章

  1. Problem W UVA 662 二十三 Fast Food

    Fast Food Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status P ...

  2. DP(递归打印路径) UVA 662 Fast Food

    题目传送门 题意:n个饭店在一条直线上,给了它们的坐标,现在要建造m个停车场,饭店没有停车场的要到最近的停车场,问所有饭店到停车场的最短距离 分析:易得区间(i, j)的最短距离和一定是建在(i + ...

  3. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  4. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  5. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  6. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  7. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  8. UVA数学入门训练Round1[6]

    UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...

  9. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

随机推荐

  1. Mac上安装Homebrew和wget

    实际上是使用Homebrew来安装wget 安装Homebrew Homebrew一般称为brew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件, 只需要一个命令, 非常 ...

  2. 推荐一个有趣的Chrome扩展程序-查看任意网站的开发技术栈

    对于前端开发人员来说,目前的前端框架层出不穷,最受欢迎的莫过于所谓的前端框架三驾马车:Angular, React和Vue.在学习的过程中,肯定好奇现在的互联网公司的网站用的何种前端框架来开发的. C ...

  3. 通过JS加载XML文件,跨浏览器兼容

    引言 通过JS加载XML文件,跨多种浏览器兼容. 在Chrome中,没有load方法,需要特殊处理! 解决方案 部分代码 try //Internet Explorer { xmlDoc=new Ac ...

  4. stay hungry stay foolish.

    I am honored to be with you today at your commencement from one of the finest universities in the wo ...

  5. WebGL 绘制Line的bug(二)

    上一篇文章简单介绍了WebGL绘制Line的bug,不少朋友给我发了私信,看来这个问题大家都遇上过哈.今天这篇文章会讲述解决这个问题的work around. 基本思路 上一篇文章结尾简单提了下解决的 ...

  6. RAID磁盘阵列及CentOS7启动流程

    1. 磁盘阵列 1.1 RAID,磁盘阵列磁盘通过硬件和软件的形式组合成一个容量巨大的磁盘组,提升整个磁盘的系统效能:RAID常见类型: RAID类型 最低磁盘个数 空间利用率 各自的优缺点 级别 说 ...

  7. windows下安装oracle客户端和php扩展

    先来抱怨下 ,按这玩楞费了我大半天的时间,一路的坑! 我的电脑是win7 64位的 第一步  打开php.ini  把 extension=php_oci8_12c.dll extension=php ...

  8. running Fluent on Apocrita Cluster

    two files: code.sh, code.jou code.sh #!/bin/bash #$ -cwd #$ -j y #$ -m bea #$ -M k.ai@qmul.ac.uk #$ ...

  9. LeetCode(73)Set Matrix Zeroes

    题目 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cli ...

  10. HDU 1166 排兵布阵(线段树单点更新)

    题意: 给定n个兵营的士兵初始值, 然后有最多40000个操作: 操作一共有两种, 一个是查询给定[a,b]区间兵营的士兵总和. 另一个是增加/减少指定兵营的士兵数目. 输出每次查询的值. 分析: 线 ...