HDU 1350 Taxi Cab Scheme

来源:互联网 发布:雅思6 知乎 编辑:程序博客网 时间:2024/06/07 09:54

Taxi Cab Scheme

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
2208:00 10 11 9 1608:07 9 16 10 11208:00 10 11 9 1608:06 9 16 10 11
 

Sample Output
12

题意:给出出租车站和时刻表,求要上多少辆车才能才能把所有的站走一遍,给出时刻表是起始时间,然后后面是两对数字,前面两个是起点坐标后面是终点,话费时间通过已给公式计算,只有再发车之前到达站才能坐上车。

构建二分图,最小点覆盖,建图考虑能赶上的站点之间连一条边,注意如果前一个段路终点和后一个段路起点之间也要通过上述公式计算时间。最好情况是一次车走完所有点,边数=点数-1,最坏情况是没有形成边,需要打车数等于点数,其中每形成一条边就少打一次车,所以最少打车数是点数-边数=点数-最大匹配数。

#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>#define N 502using namespace std;int m,last[N];int timelim[N][6];bool map[N][N],vis[N];bool find(int i){    int j;    for(j=1; j<=m; j++)    {        if(map[i][j]&&!vis[j])        {            vis[j]=1;            if(last[j]==-1||find(last[j]))            {                last[j]=i;                return 1;            }        }    }    return 0;}int main(){    int t;    int i,j,a,b,c,d;    int ti,t1,t2;    char ch;    scanf("%d",&t);    while(t--)    {        scanf("%d",&m);        for(i=1; i<=m; i++)        {            scanf("%d%c%d%d%d%d%d",&t1,&ch,&t2,&a,&b,&c,&d);            timelim[i][0]=t1*60+t2;            timelim[i][1]=timelim[i][0]+abs(a-c)+abs(d-b);timelim[i][2]=a;timelim[i][3]=b;timelim[i][4]=c;timelim[i][5]=d;        }        memset(map,0,sizeof(map));        memset(last,-1,sizeof(last));        for(i=1; i<=m; i++)            for(j=1; j<=m; j++)            {                if(i!=j&&timelim[i][1]+abs(timelim[i][4]-timelim[j][2])+abs(timelim[i][5]-timelim[j][3])<timelim[j][0])                    map[i][j]=1;            }        int sum=0;        for(i=1; i<=m; i++)        {            memset(vis,0,sizeof(vis));            if(find(i))                sum++;        }        printf("%d\n",m-sum);    }    return 0;}


1 0
原创粉丝点击