Always on the run - UVa 590 dp

本文探讨了一位艺术窃贼如何通过优化路径选择和航班预订来实现最小化逃逸成本的问题。面对每日航班价格周期性波动的挑战,作者运用动态规划算法,帮助窃贼在有限天数内从巴黎出发,最终抵达亚特兰大,同时尽可能减少旅行支出。

Always on the run

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 $1, 2, \dots, n$, where 1 is Paris, her starting point, and n is Atlanta, her final destination. The numbers will satisfy$2 \leŸ n \leŸ 10$ and $1 \leŸ k \leŸ 1000$.

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 ($2, 3, \dots, n$), the next n - 1 lines to those from city 2 to all others ( $1, 3, 4, \dots, n$), and so on.

The description of the flight schedule itself starts with an integer d, the length of the period in days, with$1 \leŸ d \leŸ 30$. Following this are d non-negative integers, representing the cost of the flight between the two cities on days $1, 2, \dots, d$. 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.

题意:一个人每天都要换一个城市,每天从一个城市到另一个城市的价钱会有周期性的变化,问你在m天后到达n城市最少的花费是多少。

思路:dp[i][j]表示在第i天当前在j点的最少花费,然后由j向i+1天递推。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int dp[1010][15],cost[15][15][35];
int main()
{ int t=0,n,m,i,j,k,money,p;
  while(~scanf("%d%d",&n,&m) && n+m)
  { for(i=1;i<=n;i++)
     for(j=1;j<=n;j++)
     { if(i==j)
        continue;
       scanf("%d",&cost[i][j][0]);
       for(k=1;k<=cost[i][j][0];k++)
        scanf("%d",&cost[i][j][k]);
     }
    memset(dp,-1,sizeof(dp));
    dp[0][1]=0;
    for(i=0;i<m;i++)
     for(j=1;j<=n;j++)
     { if(dp[i][j]>=0)
        for(k=1;k<=n;k++)
        { if(j==k)
           continue;
          p=(i+1)%cost[j][k][0];
          if(p==0)
           p=cost[j][k][0];
          money=cost[j][k][p];
          if(money==0)
           continue;
          if(dp[i+1][k]==-1)
           dp[i+1][k]=dp[i][j]+money;
          else
           dp[i+1][k]=min(dp[i+1][k],dp[i][j]+money);
        }
     }
    printf("Scenario #%d\n",++t);
    if(dp[m][n]==-1)
     printf("No flight possible.\n\n");
    else
     printf("The best flight costs %d.\n\n",dp[m][n]);
  }
}



### 回答1: docker run --restart=always 的意思是在容器退出时自动重启容器,并且在Docker守护进程启动时启动容器。这个命令可以保证容器的持续运行,即使容器出现故障或崩溃也会自动重启。 ### 回答2: docker run --restart=always 是docker命令中的一个参数,作用是设置在docker服务异常退出时自动重启容器。 在容器运行过程中,可能会出现一些问题导致容器异常退出,如应用程序或服务崩溃、内存溢出、网络故障等。如果不进行设置自动重启,这就需要手动对容器进行重启,否则服务将会中断,造成不必要的麻烦和时间浪费。 使用 --restart=always 参数可以在容器运行过程中自动重启,保证服务的不间断提供,提高了容器的可靠性和稳定性。无论是容器异常退出还是物理机或虚拟机故障,都能自动重启,保证应用可以及时恢复。同时,该参数还能让开发者更加便捷地进行容器的管理,提高了开发效率。 例如,使用命令 docker run --restart=always -d nginx ,启动nginx容器,如果在运行过程中nginx服务异常退出,docker服务会自动重启该容器,保证服务可以及时恢复,提高了应用程序的可靠性和稳定性。 总之,使用 docker run --restart=always 参数可以保证服务的可靠性,提高容器的稳定性和可靠性,为应用程序的开发和部署提供了保障。 ### 回答3: docker run --restart=always是一条Docker命令,用于在容器启动时自动重启容器。当容器运行出现故障或其他问题导致容器停止,自动重启容器可以确保应用程序始终处于运行状态,保持高可用性。 该命令中的--restart参数有多个选项,其中最常见的是always。使用该选项时,Docker会在容器异常退出时自动重启容器,直到手动停止容器。 为了实现容器的自动重启,Docker会在后台运行一个重启策略。当Docker检测到容器退出时,该策略会尝试根据预定义的规则自动重启容器。 在使用Docker部署应用程序时,特别是在生产环境中,使用--restart=always是一个很好的实践,可以确保应用程序在任何情况下都可以正常运行。 总之,Docker的--restart选项是一个很实用的功能,能够确保应用程序的高可用性和稳定性。在使用该选项时,需要根据实际情况选择适当的重启策略,以确保应用程序的恢复能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值