Pku2060 taxi cab scheme-minimum path Overwrite

Source: Internet
Author: User

Description:

The taxi company receives n (n <500) orders. Each order is described as follows: at t time, the [x, y] coordinates must be taken from the [Tx, Ty] coordinates. The time required for taxi driving is | X-TX | + | Y-ty |. Taxi companies want to dispatch the minimum number of vehicles to complete all orders. Send a taxi to pick up any order. If a taxi completes an order, it can arrive at the departure location before the next order time, then it can continue to take the next order. The minimum number of taxis required.

Analysis:

Use an order as a node with a directed acyclic graph. If order I can arrive at order J after it is completed, the directed edge is connected between directed graph I and J. Then find the Directed GraphMinimum path Overwrite.

The minimum path overwrite problem can be convertedBipartite Graph maximum matchingProblem: If node I connects to node J with a connected edge, I connects to node J on the left of the diagram. (In fact, it is the original directed graph, which is converted to a bipartite graph.) The minimum number of path overwrites = number of nodes N-the maximum number of matches.

About this questionNote:It is not only the end of the previous vertex that is equal to the start point of the next vertex that can be connected, as long as a taxi can drive to the next start point in the previous destination can also be connected.

 

  1. /*
  2. Pku2060 taxi cab Scheme
  3. */
  4. # Include <stdio. h>
  5. # Include <memory. h>
  6. # Define CLR (a) memset (A, 0, sizeof ())
  7. # Define ABS (A) (a)> 0? (A):-())
  8. # Define n 1005
  9. Typedef struct {
  10. Int H, M;
  11. } Time;
  12. Typedef struct {
  13. Int X, Y, TX, Ty;
  14. } Ride;
  15. Int find (int I, int M, int G [] [N], int mat [], int tmat []) {
  16. Int V, J;
  17. For (j = 0; j <m; j ++) if (G [I] [J] & tmat [J] = 0 ){
  18. Tmat [J] = 1; V = mat [J]; MAT [J] = I;
  19. If (V =-1 | find (v, M, G, mat, tmat) return 1;
  20. Mat [J] = V;
  21. }
  22. Return 0;
  23. }
  24. Int match (int g [] [N], int N, int M, int mat []) {
  25. Int I, j, v, k = 0;
  26. Int tmat [N];
  27. For (I = 0; I <m; I ++) mat [I] =-1;
  28. For (I = 0; I <n; I ++) {CLR (tmat); k + = find (I, m, G, mat, tmat );}
  29. Return K;
  30. }
  31. // Minimum path Overwrite
  32. Int pathcover (int g [] [N], int N ){
  33. Int mat [N];
  34. Return N-match (G, N, N, mat );
  35. }
  36. Int dist (int x, int y, int Tx, int ty ){
  37. Return ABS (X-TX) + ABS (Y-ty );
  38. }
  39. Int early (time A, ride P, time B, ride q ){
  40. Int Dist = 0;
  41. Dist + = Dist (p. x, p. y, p. tx, p. ty );
  42. Dist + = Dist (p. tx, p. ty, q. x, q. y );
  43. A. m + = dist;
  44. A. h + = a. m/60;
  45. A. m % = 60;
  46. If (a. h <B. h) return 1;
  47. If (a. h> B. h) return 0;
  48. If (a. m <B. m) return 1;
  49. Return 0;
  50. }
  51. Time t [N];
  52. Ride r [N];
  53. Int a [N] [N];
  54. Int main ()
  55. {
  56. Int I, j, k;
  57. Int T;
  58. Int n;
  59. Scanf ("% d", & T );
  60. While (t --){
  61. // Input
  62. Scanf ("% d", & N );
  63. For (k = 0; k <n; k ++ ){
  64. Scanf ("% d: % d", & T [K]. H, & T [K]. m );
  65. Scanf ("% d", & R [K]. x, & R [K]. y, & R [K]. TX, & R [K]. ty );
  66. }
  67. // Create Map
  68. CLR ();
  69. For (I = 0; I <n; I ++)
  70. For (j = I + 1; j <n; j ++)
  71. If (early (T [I], R [I], t [J], R [J])
  72. A [I] [J] = 1;
  73. // Output
  74. Printf ("% d/N", pathcover (A, n ));
  75. }
  76. Return 0;
  77. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.