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 题意: ...
随机推荐
- mysql innodb插入意向锁
innodb中有插入意向锁.专门针对insert,假设插入前,该间隙已经由gap锁,那么Insert会申请插入意向锁. 那么这个插入意向锁的作用是什么? 1.为了唤起等待.由于该间隙已经有锁,插入时必 ...
- Robot Framework操作
Robot Framework 介绍 RobotFramework是一款基于python的开源自动化测试框架,遵守Apache License 2.0协议,在此协议下所有人都可以免费开发和使用.因为R ...
- 在云服务器 ECS Linux CentOS 7 下重启服务不再通过 service 操作,而是通过 systemctl 操作
在云服务器 ECS Linux CentOS 7 下重启服务不再通过 service 操作,而是通过 systemctl 操作. 操作说明如下: 1. 查看 sshd 服务是否启动: 看到上述信息就 ...
- LeetCode 1.两数之和(JS)
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- mysql 系统函数
SELECT VERSION() -- 获取 mysql版本号 SELECT CONNECTION_ID() -- 查看服务启动后 用户的连接次数 SELECT DATABASE(),SCHEMA() ...
- mysql11---主键普通全文索引
.1主键索引添加 当一张表,把某个列设为主键的时候,则该列就是主键索引 create table aaa (id int unsigned primary key auto_increment , n ...
- Oracle - 创建表视图等 - DDL
解锁scott: sqlplus / as sysdba; alter user scott account unlock; alter user scott identified by tiger; ...
- UESTC149 解救小Q
小Q被邪恶的大魔王困在了迷宫里,love8909决定去解救她.迷宫里面有一些陷阱,一旦走到陷阱里,就会被困身亡:(,迷宫里还有一些古老的传送阵,一旦走到传送阵上,会强制被传送到传送阵的另一头.现在请你 ...
- NDK编程中如何在C文件中打印调试信息
1,在Android.mk文件中加上 LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog LOCAL_PATH := $(call my-dir)include ...
- Python之Split函数
python中的split()函数用来拆分一个字符串,通过指定的分隔符对字符串进行切割,返回切割后的字符串列表list. split()函数用法: str.split(str=' ',num = st ...