Điều hành một trạm taxi hoàn toàn không đơn giản. Bạn cần phải sắp lịch cho các tài xế sao cho số lượng xe sử dụng là ít nhất. Có thể giả sử khu vực bạn đang quản lý là một hình vuông gồm các lưới ô vuông nhỏ. Địa chỉ trong khu vực được kí hiệu bằng hai số nguyên a b là tên đường và tòa nhà. Thời gian để di chuyển từ địa chỉ a b đến địa chỉ c d là |a-c| + |b-d| phút. Một xe taxi có thể nhận khách tại một địa chỉ bất kỳ nếu đó là chuyến xe đầu tiên trong ngày hoặc nếu xe có thể đến địa chỉ đón khách trước lịch hẹn đón 1 phút.
Yêu cầu: Cho trước các lịch hẹn về chuyến đi. Hãy viết một chương trình cho biết số lượng xe taxi tối thiểu cần sử dụng để thực hiện hết các yêu cầu đó.
Dữ liệu: Vào từ file văn bản TAXI.INP, dòng đầu là N cho biết số lượng test. Mỗi test bắt đầu bằng một dòng chứa số nguyên M (0< M < 500) là số lượng lịch hẹn. M dòng tiếp theo mô tả các lịch hẹn, bắt đầu là thời điểm đón theo định dạng hh:mm (từ 00:00 đến 23:59) rồi đến hai số nguyên a b cho biết địa điểm đó và hai số nguyên c d là địa điểm trả khách. Các địa chỉ là số nguyên trong khoảng [0..200]. Các lịch hẹn được cho theo thứ tự tăng dần theo thời điểm đón
Kết quả: Ghi ra file văn bản TAXI.OUT cho biết số lượng xe taxi tối thiểu cần sử dụng để thực hiện hết các lịch hẹn.
Ví dụ:
TAXI.INP TAXI.OUT
2
2
08:00 10 11 9 16
08:07 9 16 10 11
2
08:00 10 11 9 16
08:06 9 16 10 11 1
2
solve this problem using maimum matching in C++
Assistant
To solve the problem of determining the minimum number of taxis required to handle all scheduled trips, we can model this as a graph problem. Specifically, we can use the concept of minimum path cover in a Directed Acyclic Graph (DAG), which can be translated to finding a maximum matching in a bipartite graph.
Step-by-Step Approach:
Modeling the Trips:
Each trip can be represented as a node in the graph.
An edge from trip i to trip j exists if a taxi can complete trip i and then proceed to trip j in time. This means the taxi must finish trip i, travel to the pickup location of trip j, and arrive at least one minute before the scheduled pickup time of trip j.
Time Calculation:
Convert the pickup and drop-off times from hh:mm format to minutes since midnight for easier comparison and calculation.
Sorting:
Sort all trips based on their pickup times to ensure we process them in chronological order.
Building the Bipartite Graph:
Create a bipartite graph where each node in the left partition represents a trip, and each node in the right partition also represents a trip.
Add an edge from a node in the left partition to a node in the right partition if a taxi can feasibly go from one trip to the other.
Maximum Matching:
Find the maximum matching in this bipartite graph using the DFS-based approach.
The number of minimum taxis required will be the total number of trips minus the size of the maximum matching.
Implementation:
Implement the above logic in C++, ensuring efficient parsing and handling of input data.
Below is the complete C++ implementation of the described approach:
cpp
#include <bits/stdc++.h>
using namespace std;
// Struct to represent each trip
struct Trip {
int pickup_time; // in minutes since midnight
int a_pickup, b_pickup;
int a_drop, b_drop;
int finish_time; // pickup_time + travel_time
};
// Function to convert hh:mm to minutes since midnight
int time_to_minutes(const string& time_str) {
int hh = stoi(time_str.substr(0,2));
int mm = stoi(time_str.substr(3,2));
return hh * 60 + mm;
}
// Function to find maximum matching using DFS
bool bpm(int u, vector<vector<int>> &adj, vector<int> &match_to, vector<bool> &vis){
for(auto &v: adj[u]){
if(!vis[v]){
vis[v] = true;
if(match_to[v] == -1 || bpm(match_to[v], adj, match_to, vis)){
match_to[v] = u;
return true;
}
}
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
while(N--){
int M;
cin >> M;
vector<Trip> trips(M);
for(int i=0;i<M;i++){
string time_str;
cin >> time_str;
trips[i].pickup_time = time_to_minutes(time_str);
cin >> trips[i].a_pickup >> trips[i].b_pickup >> trips[i].a_drop >> trips[i].b_drop;
// Compute finish_time
int travel = abs(trips[i].a_pickup - trips[i].a_drop) + abs(trips[i].b_pickup - trips[i].b_drop);
trips[i].finish_time = trips[i].pickup_time + travel;
}
// Sort trips by pickup_time
sort(trips.begin(), trips.end(), [&](const Trip &a, const Trip &b) -> bool{
if(a.pickup_time != b.pickup_time)
return a.pickup_time < b.pickup_time;
else
return false;
});
// Build adjacency list
vector<vector<int>> adj(M, vector<int>());
for(int i=0;i<M;i++){
for(int j=i+1;j<M;j++){
// Time available between finish_time i and pickup_time j -1
int available_time = trips[j].pickup_time -1 - trips[i].finish_time;
if(available_time <0) continue;
// Travel time from drop of i to pickup of j
int travel_time = abs(trips[i].a_drop - trips[j].a_pickup) + abs(trips[i].b_drop - trips[j].b_pickup);
if(travel_time <= available_time){
adj[i].push_back(j);
}
}
}
// Perform maximum matching
// Left nodes: 0 to M-1
// Right nodes: 0 to M-1
// match_to[v] = u means right node v is matched to left node u
vector<int> match_to(M, -1);
int result =0;
for(int u=0; u<M; u++){
vector<bool> vis(M, false);
if(bpm(u, adj, match_to, vis)){
result++;
}
}
// Minimum number of taxis is M - matching size
cout << (M - result) << "\n";
}
}
Explanation:
Time Conversion: The time_to_minutes function converts the hh:mm format to minutes since midnight for easier calculations.
Trip Struct: Each trip is stored with its pickup time, pickup and drop coordinates, and the finish time (which is calculated by adding the travel time to the pickup time).
Sorting: The trips are sorted based on their pickup times to ensure chronological processing.
Building Adjacency List: For each pair of trips (i, j), an edge is added if trip j can be performed after trip i by the same taxi.
Maximum Matching: The bpm function uses DFS to find an augmenting path for the matching. The maximum matching size is determined by iterating through all trips and attempting to match them.
Output: For each test case, the minimum number of taxis required is printed, which is the total number of trips minus the size of the maximum matching.
This implementation ensures that all constraints are met and efficiently computes the minimum number of taxis necessary to handle all the scheduled trips.