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& ...
随机推荐
- Windows系统的关机、休眠和睡眠状态究的区别
原文:Windows系统的关机.休眠和睡眠状态究的区别 相信有些小伙伴跟我一样,对电脑的一些常用操作或名词不太熟悉,今天我们就简单来聊聊电脑的关机.休眠和睡眠状态究竟有何区别吧! 如果你在电脑左下角的 ...
- Springboot源码——应用程序上下文分析
前两篇(Spring MVC源码——Root WebApplicationContext 和 Spring MVC源码——Servlet WebApplicationContext)讲述了spring ...
- Vijos——T1406 拉力赛
https://vijos.org/p/1460 描述 车展结束后,游乐园决定举办一次盛大的山道拉力赛,平平和韵韵自然也要来参加大赛. 赛场上共有n个连通的计时点,n-1条赛道(构成了一棵树).每个计 ...
- 洛谷 P1718 图形复原
P1718 图形复原 题目描述 HWX小朋友对几何的热爱在电脑组是出了名的,号称“每题必解”,这天,LXC在玩logo的时候突然想到了一个题目,刚好可以去测试一下他封号的虚实,于是,他logo编程画了 ...
- ArcGIS Engine能够做什么?
转自原文ArcGIS Engine能够做什么? ArcGIS Engine是一组跨平台的嵌入式ArcObjects,它是ArcGIS软件产品的底层组件,用来构建定制的GIS和桌面制图应用程序,或是向原 ...
- 设置eclipse中的字体大小
- nginx最新配置
#user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/error.log notice;#er ...
- Json应用案例
Json应用案例之FastJson 这几天在网上找关于Json的一些案例,无意当中找到了一个我个人感觉比较好的就是阿里巴巴工程师写的FastJson. package com.jerehedu.f ...
- 迅雷云监工crysadm搭建
之前在淘宝众筹买了个迅雷赚钱宝Pro,2016年01月到手,玩了半个月. 之后在看百度贴吧时,看到好多大神都在用PC上用网页版软件来管理赚钱宝.非常是好奇.搜索了非常多资料,基本上都已经过时了.都无法 ...
- gridView -item 大小调节(dimen-代码引用)
今天在修改一个gridview的时候,发现里面的内容并不会自动适应,填满整个gridview,而是会产生滑动,尝试了很多的方法,包括在item文件中设定width和height,结果,宽度可调,高度却 ...