前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ-2336 Ferry Loading II(简单DP)

POJ-2336 Ferry Loading II(简单DP)

作者头像
ShenduCC
发布2018-04-26 11:08:23
5160
发布2018-04-26 11:08:23
举报
文章被收录于专栏:算法修养

Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3763 Accepted: 1919 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

2 2 10 10 0 10 20 30 40 50 60 70 80 90 2 10 3 10 30 40 Sample Output

100 5 50 2

其实如果想到状态转移方程还是比较简单的,但是dp题目难的就是状态转移方程啊。我想了挺长时间,还是看了别人的博客。但是这道题目让我对dp题目又有了深刻的认识。我之前之所以想不出来是因为,我把状态弄混了,dp[i]表示第i辆车运送到对岸所花的时间,这个是 车的状态,而不是船的状态。所以所有的思考都应当围绕车子。这辆车被送到对岸,只有两种情况,要么是它不用等,船等着它,它一到就被带走。要么它等着穿,船一来它就跟船走。这就是车子的状态。

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>

using namespace std;
#define MAX 1<<30
int dp[2000];
int bp[2000];
int a[2000];
int n,t,m;
int main()
{
    int c;
    scanf("%d",&c);
    while(c--)
    {
        scanf("%d%d%d",&n,&t,&m);
        for(int i=1;i<=m;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=m;i++)
            dp[i]=bp[i]=MAX;
        dp[0]=-t;
        dp[1]=a[1]+t;
        bp[1]=1;
        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,a[i])+t);
                bp[i]=min(bp[i],bp[j]+1);
            }
        }
        printf("%d %d\n",dp[m],bp[m]);
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-01-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档