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& ...
随机推荐
- Yeslab 华为安全HCIE-第五门-防火墙攻击防范技术
Yeslab 华为安全HCIE-第五门-防火墙攻击防范技术 课程目录 Yeslab华为安全HCIE-第五门-防火墙攻击防范技术(8篇)\1_单包攻击防范.aviYeslab华为安全HCIE-第五门-防 ...
- linux ifconfig找不到
提示命令不存在 原因: 系统默认的环境变量设置不对 而,ifconfig恰恰就在/sbin里面. 下cat /etc/profile, 可以发现没有关于/sbin的环境变量. Linux,习惯用ged ...
- 洛谷——P1027 Car的旅行路线
https://www.luogu.org/problem/show?pid=1027#sub 题目描述 又到暑假了,住在城市A的Car想和朋友一起去城市B旅游.她知道每个城市都有四个飞机场,分别位于 ...
- poj 1087 A Plug for UNIX(字符串编号建图)
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14862 Accepted: 5026 ...
- Qt源码编译
Qt源码编译 eryar@163.com Key words. Qt, 源码编译 1.Introduction 随着Qt版本升级,源码编译出来的库体积越来越大.如果只是用Qt来做GUI,Qt提供的预编 ...
- 一步一步跟我学hadoop(1)----hadoop概述和安装配置
这几年云计算大数据非常火,借这个东风.今天開始学习apache的分布式计算框架hadoop,希望不要太落后. Hadoop说明 对于hadoop.apache官方wiki给出的说明为 Apache H ...
- JavaScript--数据结构与算法之图
图和图的算法:图的定义:由边的集合及顶点的集合组成. 例如地图,每个城镇是顶点,道路是边,由顶点对来定义(城镇1,城镇2)简称(v1,v2)顶点也有权重——成本.基本概念: 有向图:图的顶点对是有序的 ...
- PHP 获取完整URL地址
/** * 获取当前完整URL * @return string */ function get_url() { $sys_protocal = isset($_SERVER['SERVER_PORT ...
- oracle多实例的启动与关闭
Oracle/oracle登录 1.启监听器 lsnrctl start 监听一般不需要动,如果机器重新启动的话需要将监听启动. 查看当前SID:echo $ORACLE_SID 2.启动数据库实例: ...
- 【UWP通用应用开发】控件、应用栏
控件的属性.事件与样式资源 怎样加入控件 加入控件的方式有多种,大家更喜欢以下哪一种呢? 1)使用诸如Blend for Visual Studio或Microsoft Visual Studio X ...