匹配问题_英文题_Taxi Cab Scheme

Title 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

思路:

     题目求得是派发出租车的数目最少是多少?

 注意点:

     第一个到达目的地的时间:出发时间加上| 开始地址x -  结束地址x| + |开始地址y  - 结束地址y |

     第二个出租车到达后,能不能接的到下一个人的条件 = 到达时间  + 到达地址与目标的起始地址 < 目标的开始时间(要求相差1)

解法:

     二分匹配问题:如果某一位可以接的到下一位,那么二者之间就存在一个关系。

     题目就转换成了一个求 n - 合租人的最大值 = 车的数目

 注意点:

   如果存在关系,则为1.1表示的是二者之间仅仅是存在关系,如果他被别人一起合租了,那也没办法。

大意:

    1.以每一个点进行搜索,如果他可以找到合租的人,那么就cnt++一次,表示合租人数加一。记住只要找到一个就可以了。

    解释:x找到了y,cnt = 1,如果y没有找到人合租,cnt = 1,(n = 2,x,y) n - cnt = 1,表示只要一部车。

    2.如果搜索到的人存在了合租关系,那么我们就递归查找他能不能找到一个合租的人,大意同上。

证明:

   如果二者有关系的话,形如传递性一般;

    x与y有关系,y与z有关系,假如y已经被匹配了,那么x找到y的时候,我们还可以通过y找z的方法,传递下去。

题目为接客方面的,x既然能够接到y,y又能接到z,那么不妨x等一会接z。

 

代码如下:

#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;

struct node
{
    int time;
    int juli;
    int x,y,z,l;
} data[505];

int vis[505],merg[505][505],n,ok[505];

int abs(int x,int y)
{
    return x>y?x-y:y-x;
}

bool Find(int x)
{
    for (int i=0; i<n; i++)
    {
        ///查找了与自己有关的点,如果他没有搜索过
        if (merg[x][i] == 1 && !vis[i])
        {
            vis[i] = 1;
            ///如果他还没有走,那么就一起。
            ///如果他走了,就搜索他能不能带一个人一起,如果他能就返回true
            if (ok[i] == -1 || Find( ok[i] ))
            {
                ok[i] = x;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    int T;
    int Num,t1,t2,x,y,z,l;
    scanf("%d",&T);
    while(T--)
    {
        memset(ok,-1,sizeof(ok));
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d:%d %d %d %d %d",&t1,&t2,&x,&y,&z,&l);
            data[i].time = t1*60+t2;
            data[i].juli = abs(x-z) + abs(y-l);
            data[i].x = x;
            data[i].y = y;
            data[i].z = z;
            data[i].l = l;
        }
        memset(merg,0,sizeof(merg));
        int sum = 0,s = 0;
        ///建立路径,表示可以一起走的
        for(int i=0; i<n; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                if( data[i].time + data[i].juli + abs(data[i].z-data[j].x) + abs(data[i].l-data[j].y) < data[j].time)
                {
                    merg[i][j] = 1;
                }
            }
        }
        int cnt = 0;
        for(int i=0;i<n;i++)
        {
            memset(vis,0,sizeof(vis));
            if( Find(i) )
                cnt++;
        }
        printf("%d\n",n-cnt);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值