hdu 1350 Taxi Cab Scheme(二分匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1350

Taxi Cab Scheme

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 943    Accepted Submission(s): 456


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
 

Source
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1054  1083  1528  1287  1507 
 
题目大意:直白一点讲解第一组数据:先输入一个n,表示有几个人要乘车,接下去n行,每一行输入的是:时间、起始位置坐标、目标位置坐标

按照上述所给的数据及要求,输出最少所需要的司机数量。

解题思路:开始看到这道题目第一想法就是贪心,直接拿到达目标位置时间和下一个人的起始时间比较。但是做下去发现不能够得到最少的司机数量。这题我们应该采用二分匹配的最少边覆盖,最小路径覆盖=|n|-最大匹配数(n为定点数)

具体实现:将每一个人的坐标看成一个点。如果第一位司机接到a顾客后接到b,就将a,b连成一条有向边。并且要最少的司机覆盖所有点。完美的转化~~~

特别注意:

1、送乘客的时间为曼哈距离,公式为 |a - c| + |b - d| 

2、每换乘一次需要间隔一分钟,所以不要忘记加1


详见代码。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

struct node
{
    int stime,etime,sx,sy,ex,ey;
} p[510];

int Map[510][510];
int n;
int ok[510],vis[510];

int fun(int a)
{
    return a<0?-a:a;
}

bool Find(int x)
{
    for (int i=0; i<n; i++)
    {
        if (Map[x][i]==1&&!vis[i])
        {
            vis[i]=1;
            if (ok[i]==-1)
            {
                ok[i]=x;
                return true;
            }
            else
            {
                if (Find(ok[i]))
                {
                    ok[i]=x;
                    return true;
                }
            }
        }
    }
    return false;
}

int main()
{
    int t;
    while (~scanf("%d",&t))
    {
        while (t--)
        {
            memset(ok,-1,sizeof(ok));
            int q,w,a,b,c,d;
            scanf("%d",&n);
            for (int i=0; i<n; i++)
            {
                scanf("%d:%d%d%d%d%d",&q,&w,&a,&b,&c,&d);
                int l=q*60+w;
                p[i].stime=l;
                p[i].sx=a;
                p[i].sy=b;
                p[i].ex=c;
                p[i].ey=d;
                p[i].etime=fun(a-c)+fun(b-d);
            }
            for (int i=0; i<n; i++)
            {
                for (int j=i+1; j<n; j++)
                {
                    if (p[i].stime+p[i].etime+fun(p[i].ex-p[j].sx)+fun(p[i].ey-p[j].sy)+1<=p[j].stime)
                    {
                        Map[i][j]=1;
                    }
                    else
                        Map[i][j]=0;
                }
            }
            int k=0;
            for (int i=0;i<n;i++)
            {
                memset(vis,0,sizeof(vis));
                if (Find(i))
                    k++;
            }
            printf ("%d\n",n-k);
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值