dp(uva590Always on the run)
来源:互联网 发布:淘宝店铺如何做爆款 编辑:程序博客网 时间:2024/05/01 16:59
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 integersn and k. n is the number of cities through which Trisha's escape may take her, andk is the number of flights she will take. The cities are numbered , where 1 is Paris, her starting point, andn 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 firstn - 1 flight schedules correspond to the flights from city 1 to all other cities (), the nextn - 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 travelk days, starting in city 1, each day flying to a different city than the day before, and finally (afterk days) arriving in city n, then print `` The best flight costsx.'', 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 62 130 1503 75 0 807 120 110 0 100 110 120 04 60 70 60 503 0 135 1402 70 802 32 0 701 800 0
Sample Output
Scenario #1The best flight costs 460.Scenario #2No flight possible.
题意:从城市1出发,经过k天到达城市N ,从城市 i到j第d天的价格是x(这个是循环来的),文最小费用是多少
思路:dp[i][d]表示第d天到达城市i的最小费用,dp[i][d]=min(dp[j][d-1]+cost[j][i][tmp],dp[i][d]);
其中tmp=(d-1)%cost[j][i][0]+1
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;const int maxn=15;const int maxm=1010;int cost[maxn][maxn][35];int N,K;int dp[maxn][maxm];void solve(){ memset(dp,INF,sizeof(dp)); dp[1][0]=0; for(int d=1;d<=K;d++) { for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { if(i==j)continue; int tmp=(d-1)%cost[j][i][0]+1; if(cost[j][i][tmp]==0||dp[j][d-1]>=INF)continue; dp[i][d]=min(dp[i][d],dp[j][d-1]+cost[j][i][tmp]); } } } if(dp[N][K]>=INF)printf("No flight possible.\n\n"); else printf("The best flight costs %d.\n\n",dp[N][K]);}int main(){ int cas=1; bool first=true; while(scanf("%d%d",&N,&K)!=EOF&&N&&K) { for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { if(i==j)continue; int d,x; scanf("%d",&d); cost[i][j][0]=d; for(int k=1;k<=d;k++) { scanf("%d",&cost[i][j][k]); if(cost[i][j][k]==0); } } } printf("Scenario #%d\n",cas++); solve(); } return 0;}
- dp(uva590Always on the run)
- uva_590_Always on the run (普通DP)
- UVA 590 - Always on the run (dp)
- poj3042 Grazing on the Run[区间dp]
- UVA 590 Always on the run(dp)
- uva 590 Always on the run(dp)
- UVA 590 Always on the run | dp
- Always on the run - UVa 590 dp
- UVA 590 Always on the run(DP)
- UVA 590--Always on the run +dp
- poj 3042 Grazing on the Run(区间DP,三维DP)
- POJ 3042 Grazing on the Run (区间DP)
- Always on the run+uva+DAG图上的dp
- Uva 590 Always on the run (Dp)
- UVA 590 Always on the run(dp)
- uva 590 Always on the run(dp)
- UVA 590-Always on the run(DP)
- BZOJ 1694 [Usaco2007 Demo]Grazing on the Run DP
- 关于MSXML更改xml文件,removeChild方法成功但是文件依旧没有改变mxl文件
- 常用MyEclipse快捷键
- String,StringBuffer与StringBuilder的区别
- android webview和ji交互分析
- GC学习笔记
- dp(uva590Always on the run)
- 循序渐进学习嵌入式linux开发---引子
- Divein_day03_进制转换 十进制到其他进制的转换
- 使用调试器攻击安卓APP
- 《猩球崛起2:黎明之战》之《汉武大帝》贰师将军
- iOS工程师综合练习卷/ iOS工程师能力评估/搜狗2015 iOS工程师笔试题
- Android使用BitmapFactory.Options解决加载大图片内存溢出问题
- mock non-virtual methods
- 最小生成树