Taxi Cab Scheme【最大匹配】

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

  做这道题的第一反应就是,如果每个人的计划都相互冲突的话,那么就是需要N辆车,但是每有一对不冲突,就可以少用一辆车,然后找到最大匹配数目,用N减去它即可。


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 505;
int N, hour, minute, sx, sy, ex, ey;
struct node
{
    int time, sx, sy, ex, ey;
    node(int a=0, int b=0, int c=0, int d=0, int f=0):time(a), sx(b), sy(c), ex(d), ey(f) {}
    friend bool operator < (node e1, node e2) { return e1.time < e2.time; }
}a[maxN];
int get_time(int hou, int minu) { return hour * 60 + minu; }    //时间刻度
int Dis_In(int i) { return abs(a[i].sx - a[i].ex) + abs(a[i].sy - a[i].ey); }   //内部花费总时长
int Dis_Out(int i, int j) { return abs(a[i].ex - a[j].sx) + abs(a[i].ey - a[j].sy); }   //链接需要的时间(还需要+1等待的时间)
bool check(int i, int j) { return ( Dis_In(i) + Dis_Out(i, j) < (a[j].time - a[i].time) ); }    //能否链接
int cnt, head[maxN];
struct Eddge
{
    int nex, to;
    Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxN*maxN];
inline void addEddge(int u, int v)
{
    edge[cnt] = Eddge(head[u], v);
    head[u] = cnt++;
}
int fa[maxN];
bool vis[maxN];
bool match(int u)
{
    for(int i=head[u]; i!=-1; i=edge[i].nex)
    {
        int v = edge[i].to;
        if(vis[v]) continue;
        vis[v] = true;
        if(!fa[v] || match(fa[v]))
        {
            fa[v] = u;
            return true;
        }
    }
    return false;
}
int Hungery()
{
    int ans = 0;
    memset(fa, 0, sizeof(fa));
    for(int i=1; i<=N; i++)
    {
        memset(vis, false, sizeof(vis));
        if(match(i)) ans++;
    }
    return ans;
}
inline void init()
{
    cnt = 0;
    memset(head, -1, sizeof(head));
}
int main()
{
    int T;  scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &N);
        init();
        for(int i=1; i<=N; i++)
        {
            scanf("%d:%d%d%d%d%d", &hour, &minute, &sx, &sy, &ex, &ey);
            a[i] = node(get_time(hour, minute), sx, sy, ex, ey);
        }
        sort(a + 1, a + N + 1);
        for(int i=1; i<=N; i++) for(int j=1; j

 

你可能感兴趣的:(图论)