POJ - 2060 Taxi Cab Scheme
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
题意:
有m个任务,每个任务有开始时间(xx:xx),起点(a,b)与终点(c,d)然后做完这个任务的时间是|a-c|+|b-d|分钟,如果一个车子在一天(00:00 to 23:59)之内做完i任务之后又能做j任务,那么这个车子就做2个任务,依次类推下去,问最少需要多少辆车子把所有任务都做完。
题解:
建图:i的开始时间+做完i任务的时间+做完i任务的终点到j任务的起点的时间 < j开始的时间
那么做完i又能做完j,则i指向j,然后求最小路径覆盖即可。
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 505;
struct Edge{
int to,next;
Edge(){}
}E[MAXN*MAXN];
int head[MAXN],tot;
inline void Add(int from,int to){
E[++tot].next = head[from];
head[from] = tot;
E[tot].to = to;
}
struct V{
int time,x,y,xt,yt;
bool operator < (const struct V &b)const {
return time < b.time;
}
}board[MAXN];
int pre[MAXN];
bool used[MAXN];
int Find(int x){
for(int i=head[x] ; i ; i=E[i].next){
if(used[E[i].to])continue;
used[E[i].to] = true;
if(pre[E[i].to] == 0 || Find(pre[E[i].to])){
pre[E[i].to] = x;
return 1;
}
}
return 0;
}
inline void init(){
memset(head,0,sizeof head);
memset(pre,0,sizeof pre);
tot = 0;
}
int main(){
int T,N;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
init();
for(int i=1 ; i<=N ; ++i){
int a,b;
scanf("%d:%d",&a,&b);
board[i].time = a*60+b;
scanf("%d %d %d %d",&board[i].x,&board[i].y,&board[i].xt,&board[i].yt);
}
sort(board+1,board+1+N);
for(int i=1 ; i<=N ; ++i){
for(int j=i+1 ; j<=N ; ++j){
int a = abs(board[i].xt-board[i].x) + abs(board[i].yt-board[i].y);
int b = abs(board[j].x-board[i].xt) + abs(board[j].y-board[i].yt);
if(a+b<board[j].time-board[i].time)Add(i,j);
}
}
int sum = 0;
for(int i=1 ; i<=N ; ++i){
memset(used,false,sizeof used);
sum += Find(i);
}
printf("%d\n",N-sum);
}
return 0;
}