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.
【题目分析】
用has数组表示i时刻j站台,向左/向右有没有车。然后决策有左走,右走和等一分钟。状态数nT,时间复杂度nT
【代码】 写的好冗长
#include <cstdio>
#include <cstring>
int dp[201][51],has[201][51][2],ti[51],kase=0,x,m;
const int INF=0x3f3f3f3f;
inline int min(int a,int b)
{return a>b?b:a;}
int main()
{
int n,T;
while (scanf("%d",&n),n)
{
memset(has,0,sizeof has);
memset(ti,0,sizeof ti);
memset(dp,0,sizeof dp);
scanf("%d",&T);
for (int i=1;i<n;++i) scanf("%d",&ti[i]);
scanf("%d",&m);
for (int i=1;i<=m;i++)
{
scanf("%d",&x);
for (int j=1;x<=T&&j<=n;x+=ti[j],j++)
has[x][j][0]=1;
}
scanf("%d",&m);
for (int i=1;i<=m;i++)
{
scanf("%d",&x);
for (int j=n;x<=T&&j>=1;j--,x+=ti[j])
has[x][j][1]=1;
}//预处理
for (int i=1;i<n;++i) dp[T][i]=INF;
dp[T][n]=0;
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&&has[i][j][0]&&i+ti[j]<=T) dp[i][j]=min(dp[i][j],dp[i+ti[j]][j+1]);
if (j>1&&has[i][j][1]&&i+ti[j-1]<=T) dp[i][j]=min(dp[i][j],dp[i+ti[j-1]][j-1]);
}
}
printf("Case Number %d: ",++kase);
if (dp[0][1]>=INF) printf("impossible\n");
else printf("%d\n",dp[0][1]);
}
}
本文介绍了一个基于算法的城市地铁系统调度问题。特工Maria需要利用最优的列车时刻表,在避免危险的同时,从第一站抵达最后一站。通过对每个站点和时刻的状态进行动态规划,找到等待时间最短的方案。
269

被折叠的 条评论
为什么被折叠?



