Mountain Road
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 250 | Accepted: 95 |
Description
In the Franconian Switzerland, there is a narrow mountain road. With only a single lane, this is a bottleneck for two-way traffic. Your job is to schedule incoming cars at both ends so that the last car leaves the road as early as possible.
Each car is specified by three values: the direction in which it is going, the arrival time at the corresponding beginning of the road, and the driving time this car needs to get through, provided it is not slowed down by other cars in front. Cars cannot overtake each other on the mountain road, and reordering cars in the queues at the ends of the road is not allowed.
For safety reasons, two successive cars going in the same direction may not pass any point of the road within less than 10 seconds. This ensures that the second car will not crash into the first car if the latter brakes hard. However, if another car passes in the other direction in between, it will be clear that the road is empty, so in this case, this rule does not apply.
Each car is specified by three values: the direction in which it is going, the arrival time at the corresponding beginning of the road, and the driving time this car needs to get through, provided it is not slowed down by other cars in front. Cars cannot overtake each other on the mountain road, and reordering cars in the queues at the ends of the road is not allowed.
For safety reasons, two successive cars going in the same direction may not pass any point of the road within less than 10 seconds. This ensures that the second car will not crash into the first car if the latter brakes hard. However, if another car passes in the other direction in between, it will be clear that the road is empty, so in this case, this rule does not apply.
Input
The first line of the input consists of a single integer c (1 <= c <= 200), the number of test cases.
Then follow the test cases, each beginning with a single line consisting of an integer n (1 <= n <= 200), the number of cars you are to consider in this test case. The remainder of each test case consists of n lines, one line per car, starting with a single upper case letter ("A" or "B"), giving the direction in which the car is going. Then follow, on the same line, two integers t (0 <= t <= 100 000) and d (1 <= d <= 100 000), giving the arrival time at the beginning of the road and the minimum travel time, respectively, both in seconds.
Within a test case, the cars are given in order of increasing arrival time, and no two cars will arrive at the same time.
Then follow the test cases, each beginning with a single line consisting of an integer n (1 <= n <= 200), the number of cars you are to consider in this test case. The remainder of each test case consists of n lines, one line per car, starting with a single upper case letter ("A" or "B"), giving the direction in which the car is going. Then follow, on the same line, two integers t (0 <= t <= 100 000) and d (1 <= d <= 100 000), giving the arrival time at the beginning of the road and the minimum travel time, respectively, both in seconds.
Within a test case, the cars are given in order of increasing arrival time, and no two cars will arrive at the same time.
Output
For each test case, print a single line consisting of the point in time (in seconds) the last car leaves the road when the cars are scheduled optimally.
Sample Input
2 4 A 0 60 B 19 10 B 80 20 A 85 100 4 A 0 100 B 50 100 A 100 1 A 170 100
Sample Output
200 270
题意:在一个狭窄的路上,只能允许一辆车通过,每个车有来的方向,来的时间,以及以最大速度经过的时间。另外有一个限制因素,如果是向同一方向的连续的两辆车,对每个点来说,前后经过的时间不能小于10秒。
思路:先吐槽一下(我擦,看这数据范围和时间限制,我想了一个多小时n^2的做法,比较好想,但是证明正确性的时候怎么证明都好像不对,结果最后无奈搜题解发现是n^3的,我默默有一种想像CF一样出200组200的数据卡死出题人的冲动)。好,言归正传,dp[i][j][0,1]表示已经经过了左边来的i辆车和右边来的j辆车,并且最后一辆是从左/右走过的。然后枚举之后左边走剩下的车,和右边走剩下的车的情况。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[10];
struct node
{
int tim,d;
}car1[210],car2[210];
int dp[210][210][2],INF=1e9;
int main()
{
int T,t,n,i,j,k,l,r,tim,d,st,en;
scanf("%d",&T);
for(t=1;t<=T;t++)
{
scanf("%d",&n);
l=0;r=0;
for(i=1;i<=n;i++)
{
scanf("%s%d%d",s,&tim,&d);
if(s[0]=='A')
{
l++;
car1[l].tim=tim;car1[l].d=d;
}
else
{
r++;
car2[r].tim=tim;car2[r].d=d;
}
}
for(i=0;i<=l;i++)
for(j=0;j<=r;j++)
for(k=0;k<=1;k++)
dp[i][j][k]=INF;
dp[0][0][0]=dp[0][0][1]=0;
for(i=0;i<=l;i++)
for(j=0;j<=r;j++)
{
st=dp[i][j][1]-10;
en=dp[i][j][1]-10;
for(k=i+1;k<=l;k++)
{
st=max(st+10,car1[k].tim);
en=max(en+10,st+car1[k].d);
dp[k][j][0]=min(dp[k][j][0],en);
}
st=dp[i][j][0]-10;
en=dp[i][j][0]-10;
for(k=j+1;k<=r;k++)
{
st=max(st+10,car2[k].tim);
en=max(en+10,st+car2[k].d);
dp[i][k][1]=min(dp[i][k][1],en);
}
}
printf("%d\n",min(dp[l][r][0],dp[l][r][1]));
}
}

本文介绍了一种山路双向交通调度算法,旨在最小化最后一辆车离开狭窄山路的时间。考虑到车辆不可超车且同一方向行驶的车辆间需保持至少10秒的安全距离等约束条件,采用动态规划方法解决该问题。
108

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



