UVA 590 二十一 Always on the run

Always on the run

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status Practice UVA 590

Appoint description: 
System Crawler  (2015-08-26)

Description

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 kn 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 ared 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 kdays, 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.

Miguel A. Revilla
1998-03-10

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 int dp[1005][12];
 7 int fly[12][12][35];
 8 int main()
 9 {
10     int n,k,ca=1;
11     int i,j,l;
12     while(scanf("%d %d",&n,&k)!=EOF)
13     {
14         if(n==0 && k==0)
15             break;
16         memset(fly,0,sizeof(fly));
17         for(i=1;i<=n;i++)
18         {
19             for(j=1;j<=n;j++)
20             {
21                 if(j==i)
22                     continue;
23                 scanf("%d",&fly[i][j][0]);
24                 for(l=1;l<=fly[i][j][0];l++)
25                 {
26                     scanf("%d",&fly[i][j][l]);
27                 }
28             }
29         }
30
31         /*for(i=1;i<=n;i++)
32         {
33             for(j=1;j<=n;j++)
34             {
35                 printf("%d:",fly[i][j][0]);
36                 for(l=1;l<=k;l++)
37                     printf("%d ",fly[i][j][l]);
38                 printf("\n");
39             }
40             printf("\n");
41         }*/
42
43         memset(dp,-1,sizeof(dp));
44         dp[0][1]=0;
45         for(l=1;l<=k;l++)
46         {
47             for(i=1;i<=n;i++)
48             {
49                 if(dp[l-1][i]!=-1)
50                 {
51                     for(j=1;j<=n;j++)
52                     {
53                         if(fly[i][j][0]==0)
54                             continue;
55                         int ll;
56                         if(l%(fly[i][j][0])==0)
57                             ll=fly[i][j][0];
58                         else
59                             ll=l%fly[i][j][0];
60                         if(fly[i][j][ll]!=0)
61                         {
62                             if(dp[l][j]==-1)
63                                 dp[l][j]=dp[l-1][i]+fly[i][j][ll];
64                             else
65                                 dp[l][j]=min(dp[l][j],dp[l-1][i]+fly[i][j][ll]);
66                         }
67                     }
68                 }
69             }
70         }
71
72         if(dp[k][n]!=-1)
73             printf("Scenario #%d\nThe best flight costs %d.\n\n",ca++,dp[k][n]);
74         else
75             printf("Scenario #%d\nNo flight possible.\n\n",ca++);
76     }
77     return 0;
78 }

时间: 2024-10-10 04:33:45

UVA 590 二十一 Always on the run的相关文章

企业搜索引擎开发之连接器connector(二十一)

从上文中的QueryTraverser对象的BatchResult runBatch(BatchSize batchSize)方法上溯到CancelableBatch类,该类实现了TimedCancelable接口,后者又extends了TimedCancelable接口,后者又extends了Cancelable接口,后者又extends了Runnable接口 Cancelable接口源码 /** * A {@link Runnable} that supports cancellation.

菜鸟学Java(二十一)——如何更好的进行单元测试——JUnit

测试在软件声明周期中的重要性,不用我多说想必大家也都非常清楚.软件测试有很多分类,从测试的方法上可分为:黑盒测试.白盒测试.静态测试.动态测试等:从软件开发的过程分为:单元测试.集成测试.确认测试.验收.回归等. 在众多的分类中,与开发人员关系最紧密的莫过于单元测试了.像其他种类的测试基本上都是由专门的测试人员来完成,只有单元测试是完全由开发人员来完成的.那么今天我们就来说说什么是单元测试,为什么要进行单元测试,以及如更好的何进行单元测试. 什么是单元测试? 单元测试(unit testing)

Go语言开发(二十一)、GoMock测试框架

Go语言开发(二十一).GoMock测试框架 一.GoMock简介 1.GoMock简介 GoMock是由Golang官方开发维护的测试框架,实现了较为完整的基于interface的Mock功能,能够与Golang内置的testing包良好集成,也能用于其它的测试环境中.GoMock测试框架包含了GoMock包和mockgen工具两部分,其中GoMock包完成对桩对象生命周期的管理,mockgen工具用来生成interface对应的Mock类源文件.GoMock官网:https://github

Docker最全教程之Python爬网实战(二十一)

原文:Docker最全教程之Python爬网实战(二十一) Python目前是流行度增长最快的主流编程语言,也是第二大最受开发者喜爱的语言(参考Stack Overflow 2019开发者调查报告发布).笔者建议.NET.Java开发人员可以将Python发展为第二语言,一方面Python在某些领域确实非常犀利(爬虫.算法.人工智能等等),另一方面,相信我,Python上手完全没有门槛,你甚至无需购买任何书籍! 由于近期在筹备4.21的长沙开发者大会,耽误了不少时间.不过这次邀请到了腾讯资深技术

我的编程之路(二十一) 规范

不知不觉一周就过去了,这周细想真的没有做成什么,因为几乎都是做了改,改了做··· 1.代码规范 以前自认为自己很注重代码规范,但是没有想到自己项目组的开发会将代码规范审查的那么严谨,这突然让我想到之前说到程序员都喜欢追求完美,都喜欢自己和自己的风格一致,否则就会觉得别扭,不过与此不同,这是团队开发,代码规范是为了统一风格,便于别人的阅读与后人的修改与维护,虽然因为这我前前后后改了三次dao层的代码,一是要用公司封装的借口,二是要把异常处理都放在这一层或者service层,三是要用公司自己写的工具

QT开发(二十一)——QT布局管理器

QT开发(二十一)--QT布局管理器 一.布局管理器简介 QT中使用绝对定位的布局方式无法自适应窗口的变化. QT中提供了对界面组件进行布局管理的类,用于对界面组件进行管理,能够自动排列窗口中的界面组件,窗口大小变化后自动更新界面组件的大小. QLayout是QT中布局管理器的抽象基类,通过对QLayout的继承,实现了功能各异且互补的布局管理器. 布局管理器不是界面组件,而是界面组件的定位策略. 任意容器类型的组件都可以指定布局管理器. 同一个布局管理器管理中的组件拥有相同的父组件,在设置布局

Android学习路线(二十一)运用Fragment构建动态UI——创建一个Fragment

你可以把fragment看成是activity的模块化部分,它拥有自己的生命周期,接受它自己的输入事件,你可以在activity运行时添加或者删除它(有点像是一个"子activity",你可以在不同的activity中重用它).本课将向你展示如何使用Support Libaray继承 Fragment 类来让你的应用能够兼容正在运行Android 1.6的设备. 提示: 如果你决定你的应用需求的最低API级别是11或者更高,那么你不需要使用Support Library,你可以直接使用

Java Web总结二十一Listener监听器

一.事件三要素 1.事件源:操作事件的对象,例如:窗体Frame 2.事件监听器:事件监听器监听事件源,例如WindowListner,它是一个接口 3.事件,例如:单击事件,通过事件,可以取得事件源 二.适配器模式 1.当一个接口有较多的方法时,而实现类只需对其中少数几个实现,此时可以使用适配器模式 2.适配器模式常用于GUI编程 三.八种Web监听器 1.Web中有三个事件源,分别是ServletContext->HttpSession->ServletRequest 2.ServletC

justinmind夜话:数据母板系列视频教程之原型设计二十一条军规

案例描述:使用数据母板实现原型设计二十一条军规 知识点: 数据母板 效果图: 本站在线效果预览:(原型文件) 原型下载地址:数据母板原型设计二十一条军规 .vp  数据母板原型设计二十一条军规.html 在线视频: 实现步骤: