uva 10901 - Ferry Loading III(simulation)

本文探讨了一种优化算法,用于解决多辆车高效过河的问题。渡河过程涉及河岸间车辆的连续往返运输,考虑了车辆到达时间、渡河效率以及车辆装载数量限制。通过模拟不同场景,实现车辆有序、高效地过河,确保所有车辆在最短时间内到达对岸。

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

Problem B: Ferry Loading III

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 ncars across the river in t minutes and return in tminutes. A car may arrive at either river bank to be transported by the ferry to the opposite bank. The ferry travels continuously back and forth between the banks so long it is carrying a car or there is at least one car waiting at either bank. Whenever the ferry arrives at one of the banks, it unloads its cargo and loads up to n cars that are waiting to cross. If there are more than n, those that have been waiting the longest are loaded. If there are no cars waiting on either bank, the ferry waits until one arrives, loads it (if it arrives on the same bank of the ferry), and crosses the river. At what time does each car reach the other side of the river?

The first line of input contains c, the number of test cases. Each test case begins with n, t, mm lines follow, each giving the arrival time for a car (in minutes since the beginning of the day), and the bank at which the car arrives ("left" or "right"). For each test case, output one line per car, in the same order as the input, giving the time at which that car is unloaded at the opposite bank. Output an empty line between cases.

You may assume that 0 < n, t, m ≤ 10000. The arrival times for each test case are strictly increasing. The ferry is initially on the left bank. Loading and unloading time may be considered to be 0.

Sample input

2
2 10 10
0 left
10 left
20 left
30 left
40 left
50 left
60 left
70 left
80 left
90 left
2 10 3
10 right
25 left
40 left

Output for sample input

10
30
30
50
50
70
70
90
90
110

30
40
60

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;

#define ll long long
const int maxn = 10010;
struct car{
    int index;
    int Time;
    car(int I = 0 , int T = 0){
        index = I , Time = T;
    }
};
ll ans[maxn];
queue<car> Left , Right;
int n , t , m , current , now_Time;

void initial(){
    for(int i = 0; i < maxn; i++){
        ans[i] = 0;
    }
    current = 0;
    now_Time = 0;
}

void readcase(){
    scanf("%d%d%d" , &n , &t , &m);
    int T;
    string bank;
    for(int i = 0; i < m; i++){
        cin >> T >> bank;
        if(bank == "left"){
            Left.push(car(i , T));
        }else{
            Right.push(car(i , T));
        }
    }
}

int get_leftFirst(){
    if(!Left.empty()){
        car c = Left.front();
        return c.Time;
    }
    return -1;
}

int get_rightFirst(){
    if(!Right.empty()){
        car c = Right.front();
        return c.Time;
    }
    return -1;
}

void ferry_left(ll c1){
    if(c1 >= now_Time){
        if(current == 0) now_Time = c1;
        else now_Time = c1+t;
    }else if(current == 1) now_Time += t;
    int sum = 0;
    while(!Left.empty() && sum < n){
        car c = Left.front();
        if(c.Time <= now_Time){
            Left.pop();
            sum++;
            ans[c.index] = now_Time+t;
        }else{
            break;
        }
    }
    current = 1;
    now_Time += t;
}

void ferry_right(ll c1){
    if(c1 >= now_Time){
        if(current == 1) now_Time = c1;
        else now_Time = c1+t;
    }else if(current == 0) now_Time += t;
    int sum = 0;
    while(!Right.empty() && sum < n){
        car c = Right.front();
        if(c.Time <= now_Time){
            Right.pop();
            sum++;
            ans[c.index] = now_Time+t;
        }else{
            break;
        }
    }
    current = 0;
    now_Time += t;
}

void computing(){
    while(!Left.empty() || !Right.empty()){
        int l = get_leftFirst() , r = get_rightFirst();
        //cout << current << " " << now_Time << " " << l << " " << r << endl;
        if(current == 0){
            if(l != -1 && (now_Time >= l || l <= r || r == -1)) ferry_left(l);
            else ferry_right(r);
        }else if(current == 1){
            if(r != -1 && (now_Time >= r || r <= l || l == -1)) ferry_right(r);
            else ferry_left(l);
        }
    }
    for(int i = 0; i < m; i++){
        printf("%d\n" , ans[i]);
    }
}

int main(){
    int c;
    cin >> c;
    while(c--){
        initial();
        readcase();
        computing();
        if(c) printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值