1. 程式人生 > >poj2060——Taxi Cab Scheme(最小路徑覆蓋)

poj2060——Taxi Cab Scheme(最小路徑覆蓋)

起點 schedule n) def simple ted des single output

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

題意是說有n個出車安排,一輛車能接到這個安排的條件是:1、這輛車第一次發車;2、這輛車接了上一個安排,回到這個安排的起點的時間正好是這個安排的前一分鐘或者更早
每一次安排有五個輸入數據,第一個是發車時間,2、3是起點位置,4、5是終點位置,因此計算每兩個安排之間的時間差可以用第一個的最後兩個數和第二個的第二和三個數。我一開始就是這裏沒明白才不知道怎麽算兩個安排之間的關系
接下來就是用二分圖,把每個安排都放在二分圖的兩個點集上,顯然兩個相同的任務之間不會有邊,只有符合題意的兩個不同的任務可以連一條邊

以上內容來自 https://blog.csdn.net/blue_skyrim/article/details/51331383

跑一邊匈牙利就直接出來了 這個的數據量小 暴力也可以出來

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define pi acos(-1.0)
13 #define eps 1e-6
14 #define fi first
15 #define se second
16 #define lson l,m,rt<<1
17 #define rson m+1,r,rt<<1|1
18 #define bug         printf("******")
19 #define mem(a,b)    memset(a,b,sizeof(a))
20 #define fuck(x)     cout<<"["<<x<<"]"<<endl
21 #define f(a)        a*a
22 #define sf(n)       scanf("%d", &n)
23 #define sff(a,b)    scanf("%d %d", &a, &b)
24 #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
25 #define pf          printf
26 #define FRE(i,a,b)  for(i = a; i <= b; i++)
27 #define FREE(i,a,b) for(i = a; i >= b; i--)
28 #define FRL(i,a,b)  for(i = a; i < b; i++)
29 #define FRLL(i,a,b) for(i = a; i > b; i--)
30 #define FIN freopen("in.txt","r",stdin)
31 #define lowbit(x)   x&-x
32 #pragma comment (linker,"/STACK:102400000,102400000")
33 using namespace std;
34 const int maxn = 200004;
35 typedef long long LL;
36 int  cas, n, vis[505], mp[505][505], match[505], dfscnt;
37 struct node {
38     int time, a, b, c, d, later;
39 } qu[maxn];
40 int cal(int x1, int y1, int x2, int y2) {
41     return abs(x1 - x2) + abs(y1 - y2);
42 }
43 int dfs(int rt) {
44     for (int i = 1 ; i <= n ; i++) {
45         if (mp[rt][i]) {
46             if (vis[i] != dfscnt) {
47                 vis[i] = dfscnt;
48                 if (!match[i] || dfs(match[i])) {
49                     match[i] = rt;
50                     return 1;
51                 }
52             }
53         }
54     }
55     return 0;
56 }
57 
58 int main() {
59     scanf("%d", &cas);
60     while(cas--) {
61         scanf("%d", &n);
62         mem(vis, 0);
63         mem(mp, 0);
64         mem(match, 0);
65         dfscnt = 0;
66         for (int i = 1 ; i <= n ; i++) {
67             int x, y;
68             scanf("%d:%d %d%d%d%d", &x, &y, &qu[i].a, &qu[i].b, &qu[i].c, &qu[i].d);
69             qu[i].time = x * 60 + y;
70             qu[i].later = qu[i].time + cal(qu[i].a, qu[i].b, qu[i].c, qu[i].d);
71         }
72         for (int i = 1 ; i <= n ; i++)
73             for (int j =  i ; j <= n; j++)
74                 if (qu[i].later + cal(qu[i].c, qu[i].d, qu[j].a, qu[j].b) < qu[j].time) mp[i][j] = 1;
75         int ans = 0;
76         for (int i = 1 ; i <= n ; i++) {
77             dfscnt++;
78             if (dfs(i)) ans++;
79         }
80         printf("%d\n", n - ans);
81     }
82     return 0;
83 }

poj2060——Taxi Cab Scheme(最小路徑覆蓋)