UVALive 3126 Taxi Cab Scheme(最大匹配问题)

Running a taxi station is not all that simple. Apart from the obvious demand for a centralised coordi-
nation 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个客人要来,给出n个客人的到达时间和出发地点以及目的地点,你的任务是用尽量少的出租车去接送客人,接送客人要求出租车至少要比客人先到达出发地点一分钟,从(x1,y1)到(x2,y2)花费时间 |x2-x1|+|y2-y1| 分钟,求最少需要的出租车数量。

我是看的题解的思路,如题解:我们先把每个人的信息记录下来,然后按时间排个序,然后呢,存储一下他送完前一个乘客之后后面可以赶去的地点,那么就可以看成一个二分图了,这样遍历一下每一个点,那么他如果能到下一个点,就会产生一组配对,最后用n减去配对数就可以了。可能这样说有点不清楚,我们因为我们每一个人都会遍历到,那么他后面如果还能继续接人,那么就有一个配对,但是我们遍历到第二个点的时候,我们这个点可能与前一个点形成了一个配对,所以我们减去配对数的时候,其实就是把一辆车跑完第一个地方之后又能去的地方都减去了,因为那些地方都不需要多余的车了,所以就直接从总数里面减掉了。
emmm,我直接把题解代码贴上了哈:

#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=500;
struct node
{
	int time,x1,y1,x2,y2,dist;
}a[maxn];
int vis[maxn],left[maxn],n;
vector<int>G[maxn];
void init()
{
	for(int i=0;i<maxn;i++)
	G[i].clear();
	memset(left,0,sizeof(left));
}
bool cmp(node c,node d)
{
	return c.time<d.time;
}
int dis(int i,int j)
{
	return abs(a[j].x1-a[i].x2)+abs(a[j].y1-a[i].y2);
}
int dfs(int i)
{
	for(int j=0;j<G[i].size();j++)
	{
		int v=G[i][j];
		if(!vis[v])
		{
			vis[v]=1;
			if(!left[v]||dfs(left[v]))
			{
				left[v]=i;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		init();
		for(int i=1;i<=n;i++)
		{
			int t1,t2;
			scanf("%d:%d%d%d%d%d",&t1,&t2,&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
			a[i].time=t1*60+t2;
			a[i].dist=abs(a[i].x1-a[i].x2)+abs(a[i].y1-a[i].y2);
		}
		sort(a+1,a+n+1,cmp);
		for(int i=1;i<n;i++)
		for(int j=i+1;j<=n;j++)
		{
			if(a[j].time-1>=a[i].time+a[i].dist+dis(i,j))
			G[i].push_back(j);
		}
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			memset(vis,0,sizeof(vis));
			ans+=dfs(i);
		}
		printf("%d\n",n-ans);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_40788897/article/details/83245074
今日推荐