Question:
A taxi can keep running its tasks. The task is to drive a taxi from one place to another within a certain period of time. Give the specific time and location of each task, and ask at leastHow many taxis are used for the task?
Analysis:
First of all, because we are doing binary matching, we must know that we are using binary matching. If it happens by accident, I am not sure I can think of binary matching;
Since binary matching is used, the first part is the composition. How can we establish a matching relationship? It is to establish a relationship between tasks and regard any tasks that may be joined together as a match. Obviously, the next step is to find a minimum path overwrite in this figure, the minimum path overwrites all vertices to complete all tasks;
Minimum path overwrite = number of vertices-maximum match
In the calculation of the time, all the time is converted into a component, which reduces a lot of judgment.
# Include <iostream> # include <math. h> using namespace STD; bool map [501] [501], vis [501]; int match [501], n; struct road {int m; int EI, EJ ;} R [501]; int path (INT s) {for (INT I = 1; I <= N; I ++) if (Map [s] [I] &! Vis [I]) {vis [I] = 1; if (Match [I] =-1 | path (Match [I]) {match [I] = s; return 1 ;}} return 0 ;}int max_match () {memset (match,-1, sizeof (MATCH); int ans = 0; for (INT I = 1; I <= N; I ++) {memset (VIS, 0, sizeof (VIS); ans + = path (I );} return ans;} int main () {int cas, A, B, C, D, H1, M1, temp; scanf ("% d", & CAS ); while (CAS --) {scanf ("% d", & N); memset (MAP, 0, sizeof (MAP); For (INT I = 1; I <= N; I ++) {scanf ("% d: % d", & H1, & M1, & A, & B, & C, & D); R [I]. ei = C; R [I]. j = D; temp = ABS (c-A) + ABS (B-d); R [I]. M = temp + M1 + H1 * 60; For (Int J = 1; j <I; j ++) {temp = ABS (A-R [J]. EI) + ABS (B-r [J]. j); If (R [J]. m + temp <M1 + H1 * 60) map [I] [J] = 1 ;}} printf ("% d \ n", n-Max_Match ());} return 0 ;}
I still don't know why this is so much faster than me.
Pku2060 accelerated version
# Include < Iostream >
Using Namespace STD;
Struct Time
{
Int H, M;
};
Struct Task
{
Int SX, Sy, ex, ey;
Time st;
};
Task V [ 505 ];
Char Mat [ 505 ] [ 505 ];
Int MX [ 505 ], My [ 505 ];
Char Vstd [ 505 ];
Int NV;
Bool Early (Task A, Task B)
{
Int T = ABS (A. Ex - B. SX) + ABS (A. ey - B. Sy );
T + = ABS (A. SX - A. Ex) + ABS (A. Sy - A. Ey );
A. St. m + = T;
A. St. h + = A. St. m / 60 ;
A. St. m % = 60 ;
If (A. St. h < B. St. h)
{
Return True ;
}
Else If (A. St. h = B. St. h)
{
If (A. St. m < B. St. m)
Return True ;
}
Return False ;
}
Int PATH ( Int S)
{
Int E;
For (E = 1 ; E <= NV; e ++ )
{
If (MAT [s] [E] && ! Vstd [e])
{
Vstd [E] = 1 ;
If (My [E] =- 1 | PATH (my [e])
{
My [E] = S;
MX [s] = E;
Return 1 ;
}
}
}
Return 0 ;
}
Int Maxmatch ()
{
Int Res = 0 ;
Memset (MX, - 1 , Sizeof (MX ));
Memset (my, - 1 , Sizeof (My ));
For ( Int I = 1 ; I <= NV; I ++ )
{
If (MX [I] =- 1 )
{
Memset (vstd, 0 , Sizeof (Vstd ));
Res + = PATH (I );
}
}
Return Res;
}
Int Main ()
{
Int CAS;
Int I, J;
Scanf ( " % D " , & CAS );
While (CAS -- )
{
Scanf ( " % D " , & NV );
For (I = 1 ; I <= NV; I ++ )
{
Scanf ( " % D: % d " , & V [I]. St. H, & V [I]. St. m );
Scanf ( " % D " , & V [I]. SX, & V [I]. Sy, & V [I]. ex, & V [I]. Ey );
}
Memset (mat, 0 , Sizeof (MAT ));
For (I = 1 ; I <= NV; I ++ )
{
For (J = 1 ; J <= NV; j ++ )
{
If (I = J) Continue ;
If (Early (V [I], V [J])
{
Mat [I] [J] = 1 ;
}
}
}
Int Res = NV - Maxmatch ();
Printf ( " % D \ n " , Res );
}
Return 0 ;
}