您的位置:首页 > 其它

Taxi Cab Scheme———最小路径覆盖

2015-02-07 16:22 369 查看
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

题意:有n个任务:开始时间、起始地点、终止地点。每个地点可以派出一辆出租车,如果出租车完成任务i后还可以到达任务j,那么它可以继续执行任务j。现在问最少可以排多少辆出租车?
这道题是一个最小路径覆盖:我们把满足条件的点进行建图:先求出最大二分匹配,n-最大二分匹配就是答案
#include<stdio.h>

#include<iostream>

#include<string.h>

#include<algorithm>

#include<math.h>

using namespace std;

#define mx 1000

struct node

{

    int time;

    int cost;

    int x[2],y[2];

}ff[mx];

int ma[mx][mx];

bool use[mx];

int d[mx];

int mm;

int n;

int fin(int id)

{

    int i;

    for(i=1;i<=n;i++)

    {

        if(ma[id][i]==1&&use[i]==false)

        {

            use[i]=true;

            if(fin(d[i])||d[i]==0)

            {

                d[i]=id;

                return 1;

            }

        }

    }

 return 0;

}

int add(int x1,int y1,int x2,int y2)

{

    int dd=abs(x1-x2)+abs(y1-y2);

    return dd;

}

int main()

{

    int i,j;

    int t, a,b;

    int k;

    int mid[2];

    int hh;

    while(scanf("%d",&k)!=EOF)

    {

        for(hh=1;hh<=k;hh++)

        {

            scanf("%d",&n);

            for(i=1;i<=n;i++)

            {

                t=0;

                scanf("%d:%d %d %d %d %d",&a,&b,&ff[i].x[0],&ff[i].y[0],&ff[i].x[1],&ff[i].y[1]);

                t=a*60+b;

                ff[i].time=t; //将时间转化为分钟,这样方便比较

                mid[0]=abs(ff[i].x[0]-ff[i].x[1]);

                mid[1]=abs(ff[i].y[0]-ff[i].y[1]);

                 ff[i].cost=mid[0]+mid[1];

            }
//////////////////////////////////////////////////////////////////////////////////////建图

            memset(ma,0,sizeof(ma));  

            for(i=1;i<=n;i++)

            {

                for(j=i+1;j<=n;j++)

                {

                    if( ff[i].time + ff[i].cost + add(ff[i].x[1],ff[i].y[1],ff[j].x[0],ff[j].y[0]) <ff[j].time)  //这里要注意,不光要计算从起点到终点的时间,还要加上从当前终点到下一个起点的时间,我就是错在这里::

                    {

                        ma[i][j]=1;

                    }

                }

            }
////////////////////////////////////////////////////////////////////////////////////////
            int cnt=0;

            memset(d,0,sizeof(d));

            for(i=1;i<=n;i++)

            {

                memset(use,false,sizeof(use));

               // mm=ff[i].time+ff[i].cost;

              //  printf("%d****\n",mm);

                if(fin(i))

                    cnt++;

            }

            printf("%d\n",n-cnt);

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: