算法题:UVA 590 Always on the run(dp)

Screeching tires. Searching lights. Wailing sirens. Police cars everywhere. Trisha Quickfinger did it again! Stealing the `Mona Lisa' had been more difficult than planned, but being the world's best art thief means expecting the unexpected. So here she is, the wrapped frame tucked firmly under her arm, running to catch the northbound metro to Charles-de-Gaulle airport.

But even more important than actually stealing the painting is to shake off the police that will soon be following her. Trisha's plan is simple: for several days she will be flying from one city to another, making one flight per day. When she is reasonably sure that the police has lost her trail, she will fly to Atlanta and meet her `customer' (known only as Mr. P.) to deliver the painting.

Her plan is complicated by the fact that nowadays, even when you are stealing expensive art, you have to watch your spending budget. Trisha therefore wants to spend the least money possible on her escape flights. This is not easy, since airlines prices and flight availability vary from day to day. The price and availability of an airline connection depends on the two cities involved and the day of travel. Every pair of cities has a `flight schedule' which repeats every few days. The length of the period may be different for each pair of cities and for each direction.

Although Trisha is a good at stealing paintings, she easily gets confused when booking airline flights. This is where you come in.

Input

The input file contains the descriptions of several scenarios in which Trisha tries to escape. Every description starts with a line containing two integers n and k. n is the number of cities through which Trisha's escape may take her, and k is the number of flights she will take. The cities are numbered, where 1 is Paris, her starting point, and n is Atlanta, her final destination. The numbers will satisfy and .

Next you are given n(n - 1) flight schedules, one per line, describing the connection between every possible pair of cities. The first n - 1 flight schedules correspond to the flights from city 1 to all other cities (), the next n - 1 lines to those from city 2 to all others ( ), and so on.

The description of the flight schedule itself starts with an integer d, the length of the period in days, with . Following this are d non-negative integers, representing the cost of the flight between the two cities on days . A cost of 0 means that there is no flight between the two cities on that day.

So, for example, the flight schedule ``3 75 0 80'' means that on the first day the flight costs 75, on the second day there is no flight, on the third day it costs 80, and then the cycle repeats: on the fourth day the flight costs 75, there is no flight on the fifth day, etc.

The input is terminated by a scenario having n = k = 0.

Output

For each scenario in the input, first output the number of the scenario, as shown in the sample output. If it is possible for Trisha to travel k days, starting in city 1, each day flying to a different city than the day before, and finally (after k days) arriving in city n, then print ``The best flight costs x.'', where x is the least amount that the k flights can cost.

If it is not possible to travel in such a way, print ``No flight possible.''.

Print a blank line after each scenario.

Sample Input

3 6
2 130 150
3 75 0 80
7 120 110 0 100 110 120 0
4 60 70 60 50
3 0 135 140
2 70 80
2 3
2 0 70
1 80
0 0

Sample Output

Scenario #1
The best flight costs 460.

Scenario #2
No flight possible.

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索to
, and
, is
The
always on the run、hc340 590dp、dp590、b340 590dp、dp590材料参数,以便于您获取更多的相关知识。

时间: 2024-11-05 18:59:02

算法题:UVA 590 Always on the run(dp)的相关文章

算法:uva 10934 Dropping water balloons(dp | 难想)

题意 你有k个一模一样的水球,在一个n层楼的建筑物上进行测试,你想知道水球最低从几层楼往 下丢可以让水球破掉.由于你很懒,所以你想要丢最少次水球来测出水球刚好破掉的最低楼层.(在最糟情 况下,水球在顶楼也不会破)你可以在某一层楼丢下水球来测试,如果水球没破,你可以再捡起来继续用. Input 输入的每一行包含多组测试,每组测试为一行.每组测试包含两个整数 k 和 n, 1 <= k <= 100 而 n 是一个 64 位的整数(没错,这栋建筑物的确很高),最后一组k = 0,n=0 代表 结束

算法:uva 1351 String Compression(字符串区间dp)

题目大意 给一个字符串,可以把连续相同的部分进行缩写成k(S)的形式,S是一个字符串,k表示 有连续相同的S 例如,abgogogogo,可以缩写成ab4(go). 还可以嵌套缩写,比如 "nowletsgogogoletsgogogo", 缩写成"now2(lets3(go))" 思路 一道区间dp,但是这题并 不好想 f(i, j)表示字符串的i~j位的最小位数 那么 f(i, j) = min{  min{ f(i,k)+f(k+1, j), i<=k&

算法:uva 10003 Cutting Sticks (区间dp)

题目大意 一根长为l的木棍,上面有n个"切点",每个点的位置为c[i] 要按照一定顺 序把每个点都砍段,最后变成了n+1段 每砍一次,就会有一个花费,例如长度为10,"切点"为2,那么砍完 后会变成两段2,8, 那么花费为2+8=10 如果有多个"切点",那么不同顺序切会得到不同的花费. 最小 花费是多少? 思路 注意要增加一个c[n] = l f(i, j) 表示(i,j)区间的最小花费 f(i, j) = min{ f(i,k)+f(k+1,

算法题之UVA 763

Fibinary Numbers The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate way to view the sequence ``1010'' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence

算法题:uva 10318

题目链接: 首先,可以确定每个格子只能选一次,因为选任何大于0的偶数次,等于没有效果 一样. 然后,就可以把这题理解成从r*c的矩阵中选择一些格子进行"点亮"操作,使得最终所 有格子都是"亮"的状态.那么,每个格子要么有点亮操作,要么没有,总共复杂度为2^25,显然必须 进行减枝. 假设从第一行第一列开始,从左往右,从上往下一次依次选择,对于当前所在位置( x, y),它已经不能影响到x-2以前的行数了,所以当到x行时,如果第x-2行没有全部点亮,则进行减枝 . 此

算法题:uva 1330

题目链接: http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&category=460&page=show_problem&problem=4076 以前做过一道一维的,这题只是变成了二维的,其他方法都一样.HDU 1506  Largest Rectangle in a Histogram   题解 代码1: #include<cstdio> #include<cs

一个算法题,求答案啊啊啊啊

问题描述 一个算法题,求答案啊啊啊啊 白班 09:00-18:00 通班 09:00-21:00 每个人每个月通班数量必须等于早中班和中晚班数量之和 早中班 09:00-15:00 中晚班 15:00-21:00 假设:每月按照30计算. 排班规则: 1.每个人每个月固定休息6天连续上班天数不超过7天. 2.每天各班次上班的人数最低需求:8个白班5个通班1个早中班,2个中晚班. 3.每个月每个人的通班天数安排不超过8天. 4.每个人每个月早中班和中晚班的天数之和需要与通班天数相等. 5.每月最多

算法:uva 10817

题目大意 某校有n个教师和m个求职者,已知每人的工资和能教的课程集合,要求支付最少的工资 使得每门课都至少有两名教师教学.在职教师必须招聘. 思路 这题不太好想,搞了很久.. f[s1][s2]: s1表示课程集合{ s1 }都至少有一个教师教的情况. s2表示课程集合{ s2 }都至少有两个教师教的情况. 每个求职者的pi, 对于每个求职者,要 么选,要么不选,就是01背包问题. 对于s1,s2,可以根据当前枚举到的求职者课程即可,可推出下 一个状态: nextS1 = p[i] | s1,

算法题:把阿拉伯金额转化为汉字表示的金额

n年没写算法题了,今天用了20分钟写了一个,要求如题,感觉算法有所退步,老了 using System; using System.Text; namespace money { class Program { static void Main(string[] args) { StringBuilder sb=new StringBuilder(); var strValue = Console.ReadLine(); var strlist = strValue.Split('.'); if