UVA - 590 Always on the run

有n个点,问第k天从1到n的最少花费(第0天在点1)

已知两两之间穿梭需要的花费

已知从x到y,第几天的花费是多少(每天花费都可能不同,为0代表该天不可走)

啊,就是这样

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int a[20][20];
int b[40][20][20];
int dp[1010][20];
int main()
{
	int i,j,x,t,n,k,inf,cs;
	cs=0;
	while(cin>>n>>k)
	{
		if(n==0&&k==0)
			break;
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				if(i!=j)
				{
					cin>>a[i][j];
					for(x=0;x<a[i][j];x++)
						cin>>b[x][i][j];
				}
		memset(dp,127,sizeof(dp));
		inf=*dp[0];
		dp[0][0]=0;
		for(i=1;i<=k;i++)
			for(j=0;j<n;j++)
				for(x=0;x<n;x++)
				{
					if(j==x||b[(i-1)%a[x][j]][x][j]==0)
						continue;
					dp[i][j]=min(dp[i][j],dp[i-1][x]+b[(i-1)%a[x][j]][x][j]);
				}
		printf("Scenario #%d\n",++cs);
		if(dp[k][n-1]==inf)
			printf("No flight possible.\n\n");
		else
			printf("The best flight costs %d.\n\n",dp[k][n-1]);
	}
	return 0;
}

Always on the run

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

SubmitStatus

Description

 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 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.

Miguel A. Revilla

1998-03-10

Source

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Graph :: Special Graph (Directed Acyclic Graph) ::
Converting General Graph to DAG

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 5. Dynamic Programming

Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Graph :: Special Graph (Directed Acyclic Graph) ::
Converting General Graph to DAG

Root :: AOAPC I: Beginning Algorithm Contests -- Training Guide (Rujia Liu) :: Chapter 1. Algorithm Design :: Dynamic Programming ::
Exercises: Beginner

Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 3. Problem Solving Paradigms :: Dynamic Programming ::
DP on ‘Graph Problem‘

Root :: Western and Southwestern European Regionals :: 1997 - Ulm

时间: 2024-11-14 20:39:57

UVA - 590 Always on the run的相关文章

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 s

递推DP UVA 590 Always on the run

题目传送门 题意:题意难懂,就是一个小偷在m天内从城市1飞到城市n最小花费,输入的是每个城市飞到其他城市的航班. 分析:dp[i][j] 表示小偷第i天在城市j的最小花费.状态转移方程:dp[i][j] = min (dp[i-1][k] + cost[k][j][t%day]) t表示在t天时k飞往j的飞机的花费 收获: 代码: /************************************************ * Author :Running_Time * Created

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 eve

UVA 590

题意:好长的题看了好久= =有 n 个城市,编号 1~N, Trisha要坐飞机旅行 k 天,每天到一个城市,最后一天要到 N 城市.也就是起点是城市1,第 k 天要到达 N 城市.求 k 天旅行的最小花费.每个城市到其他城市都有一个航班表,x 天为周期,循环,接下来 x 个数表示航班价格. 多组输入,每组第一行输入 n 和 k:输入 0 0结束.接下来 n(n-1)行,表示每个城市到其他城市的航班表.也就是说第 i 个 (n-1)行, 表示 城市 i 到其余(n-1)个城市的航班表.航班表第一

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

算法入门经典大赛 Dynamic Programming

111 - History Grading LCS 103 - Stacking Boxes 最多能叠多少个box DAG最长路 10405 - Longest Common Subsequence LCS 674 - Coin Change 全然背包求方案数 10003  - Cutting Sticks 区间DP dp[l][r]代表分割l到r的最小费用 116 - Unidirectional TSP 简单递推 输出字典序最小解 从后往前推 10131 - Is Bigger Smarte

小白书关于动态规划

10192 最长公共子序列 http://uva.onlinejudge.org/index.php?option=com_onlinejudge& Itemid=8&page=show_problem&category=114&problem=1133&mosmsg= Submission+received+with+ID+13297616 */ #include <cstdio> #include <string.h> #include&

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天推出来的. 状态转移方

【UVA】10285-Longest Run on a Snowboard(动态规划)

这题出简单了,不需要打印路径. 状态方程dp[i][j] = max(dp[i-1][j],dp[i][j-1],dp[i+1][j],dp[i][j+1]); 14003395 10285 Longest Run on a Snowboard Accepted C++ 0.026 2014-08-07 11:43:51 枚举每个点进行遍历,使用记忆化搜索.要不会超时. #include<cstdio> #include<cstring> #include<iostream&