【HDU1960】Taxi Cab Scheme(最小路徑覆蓋)

 

Taxi Cab Scheme

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 231    Accepted Submission(s): 142


node

Problem 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
 
題意:就是用少的出租車接送全部的預約的客人 距離時間公式已經給好了 
最小路徑覆蓋問題 具體本身去學習匈牙利算法就行了 這道題幾乎是匈牙利算法的裸題 DFS實現 最小路徑覆蓋數 = 頂點數 - 最大匹配數
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <vector>
 7 using namespace std;
 8 
 9 const int maxn = 500 + 11;
10 vector<int> gra[maxn];
11 struct Point{
12     int x, y;
13 };
14 struct Node{
15     int st, en;
16     Point p1, p2;
17 }node[maxn];
18 bool mark[maxn];
19 int xx[maxn], yy[maxn];
20 int m;
21 
22 inline int getTime(Point a, Point b);
23 int dfs(int u);
24 int maxMatch();
25 
26 int main(){
27     int t;
28     scanf("%d", &t);
29     while(t--){
30         scanf("%d", &m);
31         for(int i = 1; i <= m; ++i){
32             gra[i].clear();
33         }
34 
35         int hour, mi;
36         for(int i = 1; i <= m; ++i){
37             scanf("%d:%d %d %d %d %d", &hour, &mi, &node[i].p1.x, &node[i].p1.y, &node[i].p2.x, &node[i].p2.y);
38             node[i].st = hour * 60 + mi;
39             node[i].en = node[i].st + getTime(node[i].p1, node[i].p2);
40         }
41 
42         for(int i = 1; i <= m; ++i){
43             for(int j = i+1; j <= m; ++j){
44                 if(node[i].en + getTime(node[i].p2, node[j].p1) < node[j].st){
45                     gra[i].push_back(j);
46                 }
47             }
48         }
49 
50         int ans = maxMatch();
51         printf("%d\n", m-ans);
52     }
53     return 0;
54 }
55 
56 inline int getTime(Point a, Point b){
57     return abs(a.x-b.x) + abs(a.y-b.y);
58 }
59 
60 int maxMatch(){
61     int res = 0;
62     memset(xx, -1, sizeof(xx));
63     memset(yy, -1, sizeof(yy));
64 
65     for(int i = 1; i <= m; ++i){
66         if(xx[i] == -1){
67             memset(mark, false, sizeof(mark));
68             res += dfs(i);
69         }
70     }
71 
72     return res;
73 }
74 
75 int dfs(int u){
76     for(int i = 0;  i < (int)gra[u].size(); ++i){
77         int v = gra[u][i];
78         if(!mark[v]){
79             mark[v] = true;
80             if(yy[v] == -1 || dfs(yy[v])){
81                 yy[v] = u;
82                 xx[u] = v;
83                 return 1;
84             }
85         }
86     }
87     return 0;
88 }
相關文章
相關標籤/搜索