uva 662
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的更多相关文章
- Problem W UVA 662 二十三 Fast Food
Fast Food Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status P ...
- DP(递归打印路径) UVA 662 Fast Food
题目传送门 题意:n个饭店在一条直线上,给了它们的坐标,现在要建造m个停车场,饭店没有停车场的要到最近的停车场,问所有饭店到停车场的最短距离 分析:易得区间(i, j)的最短距离和一定是建在(i + ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- 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 ...
- UVA - 1625 Color Length[序列DP 代价计算技巧]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
随机推荐
- scriptPubKey and scriptSig
First of all two matching scripts are used in two different transactions, one that transfers funds t ...
- COM(Component Object Model)接口定义
a COM interface is defined using a language called Interface Definition Language (IDL). The IDL file ...
- PHP18 PHP与AJAX
学习要点 JavaScript实现Ajax jQuery实现Ajax JSON PHP的JSON函数 JavaScript处理JSON数据 JavaScript实现Ajax 什么是Ajax 搜狗地图 ...
- mongdb查询数据并且返回数据条数
var totall; var a = db.db("Magiccat").collection("jishi_content").find().count({ ...
- jquery-closest
1.closest() 本例演示如何通过 closest() 完成事件委托.当被最接近的列表元素或其子后代元素被点击时,会切换黄色背景: $( document ).bind("click& ...
- layui使用小记(持续更新)
关于Select等Form表单元素,在使用的时候部分特性会失效 如select自带的Search功能: 其实在使用Form表单元素的时候,你如果需要layui自带的一些功能(搜索,验证等),请用< ...
- ubuntu中执行docker info出现警告信息WARNING: No memory limit support 或 WARNING: No swap limit support
docker info 指令报若下错误:WARNING: No memory limit support 或WARNING: No swap limit support 解决方法: 1.打开/etc/ ...
- Python进阶之网络编程
网络通信 使用网络的目的 把多方链接在一起,进行数据传递: 网络编程就是,让不同电脑上的软件进行数据传递,即进程间通信: ip地址 ip地址概念和作用 IP地址是什么:比如192.168.1.1 这样 ...
- C语言学习13
快速排序 //快速排序 #include <stdio.h> void quicksort(int a[], int left, int right); void main() { ] = ...
- LeetCode(20)Valid Parentheses
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...