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.
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.
题目大意
有 n 个乘客需要乘出租车出行,给出他们出发时间,出发地点,目的地(输入保证数据按照出发时间升序给出),每个乘客(设从(a,b)到(c,d))在路上需要花费|a-c|+|b-d|的时间。问最少需要安排多少辆车才能满足所有乘客的需求。
输入有多组数据,用N表示。
每组数据,第一行M(0 < M < 500)表示有m个乘客。接下来m行给出了出发时间xx:xx和a,b,c,d。
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
题解
最小路径覆盖。
因为“最小路径覆盖 = 点数 - 最大二分匹配”。所以,我们把每个乘客的旅程看成一个点,那么如果去搭载第 i 个人的车在把第 i 个人送到目的地后,立即启程去第 j 个人的出发点,并且能在第 j 个人的出发时间前赶到,那么这两个人就只需要一辆车就可以满足需要,我们就在点 i 和 点 j 之间建边就好,依次类推,枚举每两个人的旅途,依次判断然后建边,最后求出最大二分匹配即可。
最大二分匹配好像用网络流可以跑,但是感觉处理建图有点麻烦就没写。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int T,n,zz,head[502];
struct bian{int to,nx;} e[250002];
char ch[10];
struct map{int s,t,a,b,c,d;} dn[502];
int mark[502],vis[502];
int gettime()
{
double x,y;
x=10*(ch[0]-'0')+(ch[1]-'0');
y=10*(ch[3]-'0')+(ch[4]-'0');
return x*60+y;
}
int juli(int i,int j)
{return abs(dn[j].a-dn[i].c)+abs(dn[j].b-dn[i].d);}
void insert(int x,int y)
{zz++; e[zz].to=y; e[zz].nx=head[x]; head[x]=zz;}
void init()
{
scanf("%d",&n);
int i,j,x,y,x1,y1;
for(i=1;i<=n;i++)
{scanf("%s",ch);
dn[i].s=gettime();
scanf("%d%d%d%d",&dn[i].a,&dn[i].b,&dn[i].c,&dn[i].d);
dn[i].t=dn[i].s+abs(dn[i].c-dn[i].a)+abs(dn[i].d-dn[i].b);
}
memset(head,0,sizeof(head));
zz=0;
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
{if(dn[i].t+juli(i,j)<dn[j].s) insert(i,j);}
}
bool dfs(int x)
{
int i,p;
for(i=head[x];i;i=e[i].nx)
{p=e[i].to;
if(!vis[p])
{vis[p]=1;
if(mark[p]==-1||dfs(mark[p]))
{mark[p]=x;
return true;
}
}
}
return false;
}
void work()
{
int ct=0,i;
memset(mark,-1,sizeof(mark));
for(i=1;i<=n;i++)
{memset(vis,0,sizeof(vis));
if(dfs(i)) ct++;
}
printf("%d\n",n-ct);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{init(); work();}
return 0;
}