UVA - 590Always on the run(递推)
题目:UVA - 590Always on the run(递推)
题目大意:有一个小偷如今在计划着逃跑的路线,可是又想省机票费。
他刚開始在城市1,必须K天都在这N个城市里跑来跑去。最后一天达到城市N。问如何计划路线的得到最少的费用。
解题思路:一開始题目意思就理解有些问题。
dp【k】【i】:代表在第k天小偷从某一个城市(除了i)坐飞机飞到城市i(到达城市i也是在这一天)。
第k天的话,就看这一天坐哪个航班,加上之前的费用是最小的,就选这个方法。
然后k+ 1天就又是由第k天推出来的。
状态转移方程:dp【k】【i】 = Min (dp【k - 1】【j】(i 。= j) + v[j][i][k]).
代码:
#include <cstdio>
#include <cstring> const int N = 15;
const int M = 35;
const int maxn = 1005;
const int INF = 0x3f3f3f3f; int n, k;
int t[N][N];
int v[N][N][M];
int dp[maxn][N];
int Min (const int a, const int b) { return a < b? a: b; } int main () { int cas = 0;
while (scanf ("%d%d", &n, &k), n || k) { for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) { if (i == j)
continue;
scanf ("%d", &t[i][j]);
for (int d = 0; d < t[i][j]; d++) { scanf ("%d", &v[i][j][d]);
if (v[i][j][d] == 0)
v[i][j][d] = INF;
}
}
} for (int i = 1; i <= k; i++)
for (int j = 1; j <= n; j++)
dp[i][j] = INF; for (int i = 2; i <= n; i++)
dp[1][i] = v[1][i][0]; for (int d = 2; d <= k; d++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) { if (i != j && dp[d - 1][j] != INF)
dp[d][i] = Min (dp[d][i], dp[d - 1][j] + v[j][i][(d - 1) % t[j][i]]);
} //printf ("%lld\n", INF);
printf ("Scenario #%d\n", ++cas);
if (dp[k][n] != INF)
printf ("The best flight costs %d.\n\n", dp[k][n]);
else
printf ("No flight possible.\n\n");
} return 0;
}
UVA - 590Always on the run(递推)的更多相关文章
- UVA 11077 - Find the Permutations(递推)
UVA 11077 - Find the Permutations option=com_onlinejudge&Itemid=8&page=show_problem&cate ...
- 牛客网暑期ACM多校训练营(第二场) 题解 A run 递推 dp
链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网 White Cloud is exercising in the playground. Whi ...
- UVA 557 Burger 排列组合递推
When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was held at t ...
- UVA 10559 Blocks(区间DP&&递推)
题目大意:给你玩一个一维版的消灭星星,得分是当前消去的区间的长度的平方,求最大得分. 现在分析一下题目 因为得分是长度的平方,不能直接累加,所以在计算得分时需要考虑前一个状态所消去的长度,仅用dp[l ...
- UVA 11077 Find the Permutations 递推置换
Find the Permutations Sorting is one of the most used operations in real ...
- UVa 1638 Pole Arrangement【递推】
题意:给出n根高度为1,2,3,---n的杆子,从左边能看到l根,右边能够看到r根,问有多少种可能 看的紫书的思路 先假设已经安排好了高度为2---i的杆子, 那么高度为1的杆子的放置方法有三种情况 ...
- UVA 11464 偶数矩阵(递推 | 进制)
题目链接:https://vjudge.net/problem/UVA-11464 一道比较好的题目. 思路如下: 如果我们枚举每一个数字“变”还是“不变”,那么需要枚举$2^{255}$种情况,很显 ...
- Uva 11300 Spreading the Wealth(递推,中位数)
Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...
- UVa 580 - Critical Mass(递推)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- Swift学习笔记(1)--基本语法
1.分号; 1>Swift不要求每个语句后面跟一个分号作为语句结束的标识,如果加上也可以,看个人喜好 2>在一行中写了两句执行语句,需要用分号隔开,比如 let x = 0; printl ...
- 【Uva 1631】Locker
[Link]: [Description] 有一个n(n≤1000)位密码锁,每位都是0-9,可以循环旋转.每次可以让1-3个相邻 数字同时往上或者往下转一格.例如,567890->567901 ...
- 洛谷 P1591 阶乘数码
P1591 阶乘数码 题目描述 求n!中某个数码出现的次数. 输入输出格式 输入格式: 第一行为t(≤10),表示数据组数.接下来t行,每行一个正整数n(≤1000)和数码a. 输出格式: 对于每组数 ...
- 为线程绑定CPU
// learn gcc atomic variable #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> ...
- amlogic M8操作gpio bank
參照规格书: r代表:读 a代表GPIOAO bank 0x28代表read bit echo r a 0x28 > /sys/class/amlogic/debug 操作GPIO口读取 w代表 ...
- css选择器和优先级总结
本文转自http://www.cnblogs.com/zxjwlh/p/6213239.html CSS三大特性—— 继承. 优先级和层叠. 继承:即子类元素继承父类的样式; 优先级:是指不同类别样式 ...
- 平板电脑上完美体验Windows 8 (视频)
平板电脑上完美体验Windows 8 (视频) 目前,计算机产业正面临重大变革,三网融合,云计算,物联网正加速终端产品的融合.4C融合成为终端产品的未来发展趋势,是4C融合的代表性产品,它破了传统的W ...
- 【Django】实现跨域请求
目录 JsonP实现跨域 在Django中间件中添加响应头 @ *** CORS 即 Cross Origin Resource Sharing 跨域资源共享. 跨域请求分两种:简单请求.复杂请求. ...
- 商业模式(二):P2P网贷平台,利差和服务费为主的金融玩法
2014~2015,先后在2家P2P平台工作过,还了解过其它若干武汉P2P平台. 结合自己的工作经历和理财经历,说几句~ 1.P2P网贷这种金融类的创业项目和经营风险,远高于制造业和服务业~ ...
- 洛谷——P1311 选择客栈
https://www.luogu.org/problem/show?pid=1311 题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰 ...