UVA11958 Coming Home【时间计算+最值】

本文介绍了一个简单的算法问题,即UVA11958回家时间计算。问题描述了一个人在聚会后等待公交车回家的情景,需要计算最短的回家时间。通过分析公交车的时间表和旅行时间,算法能够找到最佳的乘车方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

John is going back home after a party. Currently he is standing on a bus station and waiting for a bus to arrive. There is a timetable of arriving buses near the station. Beside this, John also knows the amount of time that is needed to travel with specific bus. As he has only one ticket, there are no possibilities to change the bus somewhere in the middle of a trip in order to make it shorter. Can you help John to calculate minimal time that he needs to get home?
Input
There is a number of tests T (T ≤ 100) on the first line. Each test case contains the number of buses K (1 ≤ K ≤ 100) and current time (in format ‘HH:MM’. Each of the next K lines contain arrival time of the bus (in the same format as current time) and travelling time 0 ≤ Q ≤ 1000 needed for John to get home (in minutes). Refer to the sample input as an example.
Output
For each test case output a single line ‘Case T: N’. Where T is the test case number (starting from 1) and N minimal time (in minutes) needed for John to go back home.
Sample Input
2
1 18:00
19:30 30
2 18:00
19:00 100
20:00 30
Sample Output
Case 1: 120
Case 2: 150

问题链接UVA11958 Coming Home
问题简述:(略)
问题分析
    计算回家时间问题。
    简单题,给出代码,暂时不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11958 Coming Home */

#include <bits/stdc++.h>

using namespace std;

const int D = 24;
const int H = 60;

int main()
{
    int t, caseno = 0, k, h, m, l;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d:%d", &k, &h, &m);

        int start = h * H + m, end = 10000;
        for(int i = 1; i <= k; i++) {
            scanf("%d:%d%d", &h, &m, &l);
            if(h * H + m < start) h += D;      // 当天没坐上车

            int e = h * H + m + l;
            if(e < end) end = e;
        }

        printf("Case %d: %d\n", ++caseno, end - start);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值