uva1025 A Spy in the Metro (dp)
A Spy in the MetroTime Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %lluSubmit StatusDescriptionSecret agent Maria was sent to Algorithms
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examining the time table. The Algorithms City Metro consists of a single line with trains running both ways, so its time table is not complicated.
Maria has an appointment with a local spy at the last station of Algorithms City Metro. Maria knows that a powerful organization is after her. She also knows that while waiting at a station, she is at great risk of being caught. To hide in a running train is much safer, so she decides to stay in running trains as much as possible, even if this means traveling backward and forward. Maria needs to know a schedule with minimal waiting time at the stations that gets her to the last station in time for her appointment. You must write a program that finds the total waiting time in a best schedule for Maria.
The Algorithms City Metro system has N stations, consecutively numbered from 1 to N. Trains move in both directions: from the first station to the last station and from the last station back to the first station. The time required for a train to travel between two consecutive stations is fixed since all trains move at the same speed. Trains make a very short stop at each station, which you can ignore for simplicity. Since she is a very fast agent, Maria can always change trains at a station even if the trains involved stop in that station at the same time.
Input
The input file contains several test cases. Each test case consists of seven lines with information as follows.-
Line 1.
- The integer N ( 2N50), which is the number of stations. Line 2.
- The integer T ( 0T200), which is the time of the appointment. Line 3.
- N - 1 integers: t1, t2,..., tN - 1 ( 1ti70), representing the travel times for the trains between two consecutive stations: t1 represents the travel time between the first two stations, t2 the time between the second and the third station, and so on. Line 4.
- The integer M1 ( 1M150), representing the number of trains departing from the first station. Line 5.
- M1 integers: d1, d2,..., dM1 ( 0di250 and di < di + 1), representing the times at which trains depart from the first station. Line 6.
- The integer M2 ( 1M250), representing the number of trains departing from the N-th station. Line 7.
- M2 integers: e1, e2,..., eM2 ( 0ei250 and ei < ei + 1) representing the times at which trains depart from the N-th station.
The last case is followed by a line containing a single zero.
Output
For each test case, print a line containing the case number (starting with 1) and an integer representing the total waiting time in the stations for a best schedule, or the word ` impossible' in case Maria is unable to make the appointment. Use the format of the sample output.
Sample Input
4 55 5 10 15 4 0 5 10 20 4 0 5 10 15 4 18 1 2 3 5 0 3 6 10 12 6 0 3 5 7 12 15 2 30 20 1 20 7 1 3 5 7 11 13 17 0
Sample Output
Case Number 1: 5 Case Number 2: 0 Case Number 3: impossible
Source
Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 9. Dynamic Programming :: Examples
/*这题真的是一道非常好的dp题,我看到它来之刘汝佳的红书,以下就由我来分析分析这道题,这题我WA了一次,因为当时
混乱之中数组开小了,我后来花了不少时间才检查出来,这题有三种走法,1是等待一秒,2是向右走一站(有向右车),3是向左走一站(有向左的车),一种dp塔的思想,先将塔底部T时填入足够大的数(1~n-1),T时n站填0,然后利用三种走递推出0秒1站的状态,如果是一个小值则输出结果,否则输出impossible*/
#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int t[55]={0};//1--2,2--3,~~n-1--n所花费的时间,个数n-1
int w[500][55][2];//第一个维度是时间T,第二个维度是第i个站台,第三个维度0是向左的车,1是向右的车
//w实际上是一个bool变量,有车时w[][][]=1;否则w[][][]=0;相当于红书里的has_train数组
int dp[500][500];//dp数塔
int m1t[55],m2t[55];//m1t用来存放1-1,1-2,1-3,~~1-n的时间,m2t用来存放n-(n-1)~~n-2,n-1的时间
int m1d[55],m2d[55];//m1d存放向右行驶的列车的开车时间,m2d存放向左行驶的列车开车时间
int main() {
int n;
//freopen("/Users/wenhanmac/Desktop/1.txt", "r", stdin);
//freopen("/Users/wenhanmac/Desktop/2.txt", "w", stdout);
int my=1;//count=1
while(cin>>n&&n)
{
memset(w, 0, sizeof(w));
memset(dp, 0, sizeof(dp));
int T;
cin>>T;
for(int i=1;i<n;i++)
scanf("%d",&t[i]);
m1t[1]=0;
for(int i=1;i<n;i++)//处理m1t
m1t[i+1]=m1t[i]+t[i];
m2t[1]=0;
int v=1;
for(int i=n-1;i>=1;i--)//处理m2t
{
m2t[v+1]=m2t[v]+t[i];
v++;
}
int m1,m2;
cin>>m1;
for(int i=1;i<=m1;i++)
scanf("%d",&m1d[i]);
int s;
for(int i=0;i<=T;i++)//将向右开的列车情况统计在w中,w统计是否有列车在T时在i站台且向右
{
s=1;
for(int j=n;j>=1;j--)
{
if(s>m1)
break;
if(m1d[s]+m1t[j]==i)
{
s++;
w[i][j][0]=1;
}
else if(m1d[s]+m1t[j]<i)
{
j++;
s++;
}
}
}
cin>>m2;
for(int i=1;i<=m2;i++)
scanf("%d",&m2d[i]);
for(int i=0;i<=T;i++)//道理同上面相同,方向相反
{
s=1;
for(int j=n;j>=1;j--)
{
if(s>m2)
break;
if(m2d[s]+m2t[j]==i)
{
s++;
w[i][n-j+1][1]=1;
}
else if(m2d[s]+m2t[j]<i)
{
j++;
s++;
}
}
}
for(int i=1;i<=n-1;i++)//初始化底层
dp[T][i]=1e9+7;
dp[T][n]=0;//n站标1
for(int i=T-1;i>=0;i--)//核心代码
{
for(int j=1;j<=n;j++)
{
dp[i][j]=dp[i+1][j]+1;//原地等待
if(j<n&&w[i][j][0]&&i+t[j]<=T)//有车且可以向右移动
dp[i][j]=min(dp[i][j], dp[i+t[j]][j+1]);
if(j>1&&w[i][j][1]&&i+t[j-1]<=T)//有车且可以向左移动
dp[i][j]=min(dp[i][j], dp[i+t[j-1]][j-1]);
}
}
printf("Case Number %d: ",my++);
if(dp[0][1]>=1e9+7)
printf("impossible\n");
else
printf("%d\n",dp[0][1]);
}
// insert code here...
//std::cout << "Hello, World!\n";
return 0;
}
//请大牛勿喷,大佬们这题是60——80行,我写了100行,还有很多地方要改
更多推荐
所有评论(0)