TOJ 1023 Taxi Cab Scheme

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

Source

Northwestern Europe 2004

 

把每一次的路途(起点-终点)看成一个结点的话。如果一辆出租车能够完成两两路途,就表示两个结点之间存在匹配。

如果建的图是无向图:

最小路径覆盖=结点数-最大匹配数/2

如果建的是有向图:

最小路径覆盖=结点数-最大匹配数

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #define MAXN 550
 5 
 6 int bmap[MAXN][MAXN];
 7 bool bmask[MAXN];//寻找增广路径时的标志数组
 8 int nx,ny;//nx左集合的顶点数目,ny为右集合的顶点数目
 9 int cx[MAXN];//cx[i]表示左集合i顶点所匹配到的右集合的顶点序号 
10 int cy[MAXN];//cy[i]表示右集合i顶点所匹配到的左集合的顶点序号
11 
12 struct Node{
13     int bx,by,ex,ey;
14     int begin,end;    
15 }nod[MAXN];
16 
17 //寻找增广路径 
18 int findpath(int u){
19     for(int i=0; i<ny; i++){
20         //如果匹配,且i不在增广路上 
21         if( bmap[u][i] && !bmask[i] ){
22             //把i加到增广路上 
23             bmask[i]=1;
24             //如果i是未盖点或者从i出发有增广路
25             if(cy[i]==-1 || findpath(cy[i])){
26                 //修改对应的项为u,表示有增广路
27                 cy[i]=u;
28                 return 1;        
29             }
30         }
31     }
32     return 0;
33 }
34 
35 int hungray(){
36     int res=0;
37     for(int i=0; i<nx; i++){
38         cx[i]=-1;
39     }
40     for(int j=0; j<ny; j++){
41         cy[j]=-1;
42     }
43     for(int i=0; i<nx; i++){
44         //如果从左边开始是未盖点的 
45         if(cx[i]==-1){
46             for(int j=0; j<ny; j++){
47                 bmask[j]=0;
48             }
49             res+=findpath(i);
50         }
51     }
52     return res;
53 }
54 
55 int main()
56 {
57     int n,t;
58     int a,b,c,d;
59     int h,m;
60     scanf("%d",&t);
61     while( t-- ){
62         scanf("%d",&n);
63         nx=n;
64         ny=n;
65         for(int i=0; i<n; i++){
66             scanf("%d:%d" ,&h ,&m);
67             scanf("%d %d %d %d" ,&a ,&b ,&c ,&d);
68             nod[i].bx=a;
69             nod[i].by=b;
70             nod[i].ex=c;
71             nod[i].ey=d;
72             nod[i].begin=60*h+m;
73             nod[i].end=nod[i].begin+fabs(a-c)+fabs(b-d);
74         }
75         //建图
76         memset(bmap ,0 ,sizeof(bmap));
77         for(int i=0; i<n; i++){
78             for(int j=i+1; j<n; j++){
79                 int dis= fabs(nod[j].bx-nod[i].ex) + fabs(nod[j].by-nod[i].ey);
80                 if( nod[i].end +dis < nod[j].begin ){
81                     bmap[i][j]=1;
82                 }
83             }
84         }
85         int ans=hungray();
86         printf("%d\n",n-ans);
87     }    
88     return 0;
89 }

 

你可能感兴趣的:(Scheme)