#POJ 2060 Taxi Cab Scheme (最小路径覆盖)

本文探讨了出租车调度中的最小路径覆盖问题,通过将顾客行程建模为网格图,利用二分图最大匹配算法,优化出租车数量以满足所有顾客需求。

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

Description

Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coordination of the cabs in order to pick up the customers calling to get a cab as soon as possible,there is also a need to schedule all the taxi rides which have been booked in advance.Given a list of all booked taxi rides for the next day, you want to minimise the number of cabs needed to carry out all of the rides.
For the sake of simplicity, we model a city as a rectangular grid. An address in the city is denoted by two integers: the street and avenue number. The time needed to get from the address a, b to c, d by taxi is |a - c| + |b - d| minutes. A cab may carry out a booked ride if it is its first ride of the day, or if it can get to the source address of the new ride from its latest,at least one minute before the new ride's scheduled departure. Note that some rides may end after midnight.

Input

On the first line of the input is a single positive integer N, telling the number of test scenarios to follow. Each scenario begins with a line containing an integer M, 0 < M < 500, being the number of booked taxi rides. The following M lines contain the rides. Each ride is described by a departure time on the format hh:mm (ranging from 00:00 to 23:59), two integers a b that are the coordinates of the source address and two integers c d that are the coordinates of the destination address. All coordinates are at least 0 and strictly smaller than 200. The booked rides in each scenario are sorted in order of increasing departure time.

Output

For each scenario, output one line containing the minimum number of cabs required to carry out all the booked taxi rides.

Sample Input

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

Sample Output

1
2

题目大意 : 输入多样例, 每组样例有N个顾客, 每个顾客 都有各自预定的出发时间, 出发点和目的地, 地点之间的距离为网格图的距离, 问最少需要几辆出租车,才可以满足所有顾客的乘车需求

思路 : 最小路径覆盖问题, 行程看成一条路, 如果这条边走完还可继续走,那就继续延申, 如果延申完确没有遍历整张图, 那就再加一个路径。建图方法是, 保存每个顾客的出发时间,方便起见,全部转化为分钟, 注意可能有24点之后的出发时间, 但是题目说了输入的时间是按照时间增加的次序, 所以24点之后的直接加上24 * 60即可, 然后枚举每一个点, 如果当前顾客跑到目的地后, 再到下一个顾客的出发点,花的时间是小于 两个顾客的出发时间之差的,那么这两个顾客只需要一辆车就好了,把他们拆点并连边, 最后跑二分图最大匹配, 用N - 匹配数就是答案

Accepted code

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

#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define P2(x) ((x) * (x))

typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1e3 + 10;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }

struct node
{
	int id, ai, bi, ci, di;
}p[MAXN];
int op[MAXN][MAXN], pre[MAXN], n, m, T;
bool vis[MAXN];
void init() {
	MEM(p, 0); MEM(op, 0); MEM(pre, 0);
}
bool find_(int x) {
	for (int i = n + 1; i <= 2 * n; i++) {
		if (op[x][i] && !vis[i]) {
			vis[i] = 1;
			if (!pre[i] || find_(pre[i])) {
				pre[i] = x;
				return true;
			}
		}
	}
	return false;
}

int main()
{
	cin >> T;
	while (T--) {
		sc("%d", &n); init();
		for (int i = 1; i <= n; i++) {
			int hour, mm, xx, yy, xi, yi;
			sc("%d:%d %d %d %d %d", &hour, &mm, &xx, &yy, &xi, &yi); 
			p[i].id = hour * 60 + mm; p[i].ai = xx, p[i].bi = yy, p[i].ci = xi, p[i].di = yi;
			if (p[i].id < p[i - 1].id) p[i].id += 24 * 60; // 凌晨出发
		}
		for (int i = 1; i < n; i++) {
			for (int j = i + 1; j <= n; j++) {
				int tmp = p[j].id - p[i].id, dis = abs(p[i].ai - p[i].ci) + abs(p[i].bi - p[i].di) + abs(p[i].ci - p[j].ai) + abs(p[i].di - p[j].bi);
				if (tmp > dis) op[i][j + n] = 1; //跑完上一家还来得及返回			}
		}
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			MEM(vis, 0);
			if (find_(i)) ans++;
		}
		cout << n - ans << endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值