POJ 2336 Ferry Loading II(运车过河的最短时间和次数)

来源:互联网 发布:pscc2018软件下载 编辑:程序博客网 时间:2024/05/18 01:03
Ferry Loading II
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 4042
Accepted: 2030

Description

Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry. 
There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. m cars arrive at the ferry terminal by a given schedule. What is the earliest time that all the cars can be transported across the river? What is the minimum number of trips that the operator must make to deliver all cars by that time?

Input

The first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day). The operator can run the ferry whenever he or she wishes, but can take only the cars that have arrived up to that time.

Output

For each test case, output a single line with two integers: the time, in minutes since the beginning of the day, when the last car is delivered to the other side of the river, and the minimum number of trips made by the ferry to carry the cars within that time. 

You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order
.

Sample Input

22 10 1001020304050607080902 10 3103040

Sample Output

100 550 2

Source

Waterloo local 2003.01.25
题目大意:一个渡河问题,船每次最多可以载n辆车,单程渡河的时间是t,有m辆车要渡河,问最少时间把所有车运过河去的时间是多少,这个情况下,最少的运输次数是多少。
解题思路:
1、渡船次数在时间最少的情况下是一定的。
2、dp[ i ]表示第 i 辆车用到的最少的时间
3、dp[ i ]=min(dp[ i ],max(dp[ j ] + t , f[ i ]) + t)
因为每一辆车只有两种情况,
一   船等车,车一到,船就开 f[ i ] +t
二   车等船, ( dp[ j ] + t ) + t
4、b[ i ]表示第 i 辆车的最少次数
AC代码
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int dp[1500],f[1500],b[1500];int main(){int n,t,m;int c;scanf("%d",&c);while(c--){scanf("%d%d%d",&n,&t,&m);for(int i=1;i<=m;i++){scanf("%d",&f[i]);} for(int i=1;i<=m;i++)            dp[i]=b[i]=1<<30;b[1]=1;dp[0]=-t;dp[1]=f[1]+t;for(int i=2;i<=m;i++){for(int j=max(0,i-n);j<i;j++){dp[i]=min(dp[i],max(dp[j]+t,f[i])+t);b[i]=min(b[i],b[j]+1);}}printf("%d %d\n",dp[m],b[m]);}return 0;}



原创粉丝点击