uva1025 a spy in the metro

来源:互联网 发布:matlab矩阵相加 编辑:程序博客网 时间:2024/06/05 00:22

Secret agent Maria was sent to Algorithms City to carry out an
especially dangerous mission. After several thrilling events we nd
her in the rst 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 nds 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 rst station to the last
station and from the last station back to the rst station. The time
required for a train to travel between two consecutive stations is
xed 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 le contains several test cases. Each test case consists of
seven lines with information as follows. Line 1. The integer N (2  N
 50), which is the number of stations. Line 2. The integer T (0  T 
200), which is the time of the appointment. Line 3. N

设dp[i][j]表示i时刻在车站j需要的最少等待时间。则可以采用逆推法。
分三种情况,不动,向左走,向右走。
dp[i][j]=min(dp[i+1][j]+1,dp[i+t[j]][j+1],i时刻j车站有一辆向右的火车,dp[i+t[j-1]][j-1],i时刻j车站有一辆向左的火车)。
判断的时候可以预处理出每一时刻每一车站是否有向左/右的火车。

#include<cstdio>#include<cstring>#include<iostream>using namespace std;bool b[300][55][2];int t[55],dp[300][55];const int oo=0x3f3f3f3f;int main(){    int i,j,k,l,m,m1,m2,n,p,q,x,y,z,T,cas=0;    while (scanf("%d",&n)&&n)    {        cas++;        memset(b,0,sizeof(b));        memset(t,0,sizeof(t));        memset(dp,0x3f,sizeof(dp));        scanf("%d",&m);        for (i=1;i<n;i++)          scanf("%d",&t[i]);        scanf("%d",&m1);        for (i=1;i<=m1;i++)        {            scanf("%d",&x);            for (j=1;x<=m&&j<=n;x+=t[j],j++)              b[x][j][0]=1;        }        scanf("%d",&m2);        for (i=1;i<=m2;i++)        {            scanf("%d",&x);            for (j=n;x<=m&&j>=1;j--,x+=t[j])              b[x][j][1]=1;        }        dp[m][n]=0;        for (i=m-1;i>=0;i--)          for (j=1;j<=n;j++)          {            dp[i][j]=dp[i+1][j]+1;            if (j<n&&i+t[j]<=m&&b[i][j][0])              dp[i][j]=min(dp[i][j],dp[i+t[j]][j+1]);            if (j>1&&i+t[j-1]<=m&&b[i][j][1])              dp[i][j]=min(dp[i][j],dp[i+t[j-1]][j-1]);          }        printf("Case Number %d: ",cas);        if (dp[0][1]>=oo) printf("impossible\n");        else printf("%d\n",dp[0][1]);    }}
0 0
原创粉丝点击