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& ...
随机推荐
- 休假结束,Linus重回内核开发岗位
在休假反省一个多月之后,Linus Torvalds 又回来了.10 月 22 日爱丁堡举行的欧洲开源峰会上,Linus Torvalds 将与内核维护者们碰头,这是他重新接管Linux内核开发的第一 ...
- mysql 5.7 双主+主从配置
mysql5.7安装及赋权 wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm rpm -ivh mysql57 ...
- python垃圾回收算法
标准python垃圾回收器由两部分组成,即引用计数回收器和分代垃圾回收器(即python包中的gc module).其中,引用计数模块不能被禁用,而GC模块可以被禁用. 引用计数算法 python中每 ...
- 【Henu ACM Round#18 A】 Multiplication Table
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 遍历i从1..n 看看x%i==0以及x/i<=n是否成立. [代码] #include <iostream> u ...
- Linux中配置网桥
使用kvm虚拟机时,有时候需要自己添加网桥供guest使用. 不使用libvirt来管理的话,可以使用以下方法创建网桥并绑定到物理网卡(RHEL6/Fedora已实验): 1.创建网桥配置文件ifcf ...
- [Python] Execute a Python Script
Python scripts can be executed by passing the script name to the python command or created as execut ...
- Java 实现策略(Strategy)模式
策略模式:行为型模式 将同一行为,不同的处理算法分别封装起来.让它们之间能够互相替换 1. 定义一个超类型接口,及 行为方法 2. 定义不同的实现类,实现该行为的 不同的算法 /** * 策略模式:针 ...
- ASP.Net MVC默认目录结构
1.Controllers 保存处理URL请求的Controller类 2.Models 保存操纵业务与数据对象的类 3.Views 保存UI模板页面 4.Scripts 保存Javascript库文 ...
- SQL、Linq相关字段搜索
结合一些分词组件,如盘古,对于用户查询关键字红按钮很容易分出 ‘红’ ‘按钮’二个单词 我们假设产品名称列里面是红色,规格里面是按钮 /* 普通sql实现全文搜索declare @key1 nvarc ...
- c# 多态的美丽(虚方法、抽象、接口实现)
面向对象3大特性:封装.继承.多态. 面向对象2大原则: 1)里氏替换原则:子类可以给父类,父类不能赋给子类. 2)开放封闭原则: 封装变化,降低耦合.(对扩展开放,对修改封闭) ********** ...