Descriptionios
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. 數組
Inputide
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.ui
Outputspa
For each scenario, output one line containing the minimum number of cabs required to carry out all the booked taxi rides. code
Sample Inputorm
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 Outputblog
1
2
Sourceip
把每一次的路途(起點-終點)當作一個結點的話。若是一輛出租車可以完成兩兩路途,就表示兩個結點之間存在匹配。
若是建的圖是無向圖:
最小路徑覆蓋=結點數-最大匹配數/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 }