题目: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(递推)的更多相关文章

  1. UVA 11077 - Find the Permutations(递推)

    UVA 11077 - Find the Permutations option=com_onlinejudge&Itemid=8&page=show_problem&cate ...

  2. 牛客网暑期ACM多校训练营(第二场) 题解 A run 递推 dp

    链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网 White Cloud is exercising in the playground. Whi ...

  3. UVA 557 Burger 排列组合递推

    When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was held at t ...

  4. UVA 10559 Blocks(区间DP&&递推)

    题目大意:给你玩一个一维版的消灭星星,得分是当前消去的区间的长度的平方,求最大得分. 现在分析一下题目 因为得分是长度的平方,不能直接累加,所以在计算得分时需要考虑前一个状态所消去的长度,仅用dp[l ...

  5. UVA 11077 Find the Permutations 递推置换

                               Find the Permutations Sorting is one of the most used operations in real ...

  6. UVa 1638 Pole Arrangement【递推】

    题意:给出n根高度为1,2,3,---n的杆子,从左边能看到l根,右边能够看到r根,问有多少种可能 看的紫书的思路 先假设已经安排好了高度为2---i的杆子, 那么高度为1的杆子的放置方法有三种情况 ...

  7. UVA 11464 偶数矩阵(递推 | 进制)

    题目链接:https://vjudge.net/problem/UVA-11464 一道比较好的题目. 思路如下: 如果我们枚举每一个数字“变”还是“不变”,那么需要枚举$2^{255}$种情况,很显 ...

  8. Uva 11300 Spreading the Wealth(递推,中位数)

    Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...

  9. UVa 580 - Critical Mass(递推)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. COGS——T 7. 通信线路

    http://www.cogs.pro/cogs/problem/problem.php?pid=7 ★★   输入文件:mcst.in   输出文件:mcst.out   简单对比时间限制:1.5 ...

  2. Androidbutton事件的五中写法总结

    button事件的五中写法: 1.匿名内部类 2.类实现View.OnClickListener接口 3.创建实例化接口对象 4.使用内部类 5.自己定义方法,配置Android:onclick属性 ...

  3. Timus 1935. Tears of Drowned 具体解释

    Old Captain Jack Sparrow's friend Tia Dalma, the fortuneteller and prophetess, often makes potions. ...

  4. js插件---评分插件Rating如何使用

    js插件---评分插件Rating如何使用 一.总结 一句话总结:form下的input和a标签,input记录值,a标签显示样式 12 <form data-am-rating> 13 ...

  5. POJ 2111 DP+记录路径

    题意: 思路: 类似滑雪 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm& ...

  6. OpenCV —— 图像处理

    使用图像结构中所定义的高层处理方法(图形和视觉范畴)来完成特定任务 平滑处理   cvSmooth 处理后图像与输入图像的大小相同(不用考虑边缘) 中值滤波  CV_MEDIAN 不支持 in pla ...

  7. Postman APP

    http://chromecj.com/web-development/2017-12/870.html    Postman 工具模拟http各种协议请求.

  8. 【2017 Multi-University Training Contest - Team 3】RXD and math

    [Link]: [Description] [Solution] 发现1010mod(109+7)=999999937; 猜测答案是nk 写个快速幂; 注意对底数先取模; [NumberOf WA] ...

  9. JS-网页中分页栏

    原理 三部分 我给分页栏分成了3部分 上一页:调用prePage()函数 下一页:调用nextPage()函数 带有数字标识的部,调用skipPage()函数 prePage函数 function p ...

  10. [AngularFire2 & Firestore] Example for collection and doc

    import {Injectable} from '@angular/core'; import {Skill} from '../models/skills'; import {AuthServic ...