码迷,mamicode.com
首页 > 其他好文 > 详细

Taxi Cab Scheme POJ - 2060 二分图最小路径覆盖

时间:2019-10-02 17:06:07      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:math   false   and   eof   ret   container   scheme   note   坐标   

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

OJ-ID:
poj-2060

author:
Caution_X

date of submission:
20191002

tags:
二分图最小点覆盖

description modelling:
给定一个二维坐标图,从一个点a到另一个点b费时(a.x-b.x)+(a.y-b.y),现在有n个出租车订单,每个订单提供起点终点坐标和用车时间,问最少需要几辆出租车才可以在用车时间内接完所有客人

major steps to solve it:
(1) 假设我们派出了n辆出租车,如果两个订单恰好可以由一辆车完成,那么出租车数-1.
(2) 建图:现在以一辆车能否在接完这单并且及时接下下一单为依据建立一个二分图,如果两个订单可以由一辆车接下,那么这两个订单设定成匹配状态
(3) 算出最小路径覆盖(二分图最小路径覆盖:用最少的边覆盖所有的点)
最小路径覆盖=N-二分图最大匹配

AC code:

#include<cstdio>
#include<cstring>
#include<math.h>
using namespace std;
int N;
int line[550][550];
int g[550],used[550];
struct Node{
    int t,h,m,a,b,c,d;
}node[550];
int is_link(Node A,Node B)
{
    int dis1=fabs(A.a-A.c)+fabs(A.b-A.d);
    int dis2=fabs(B.a-A.c)+fabs(B.b-A.d);
    return dis1+dis2+1<=fabs(A.t-B.t)?1:0;
}
bool found(int x)
{
    for(int i=1;i<=N;i++) {
        if(line[x][i]&&!used[i]) {
            used[i]=1;
            if(g[i]==-1||found(g[i])) {
                g[i]=x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    //freopen("input.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&N);
        memset(line,0,sizeof(line));
        memset(g,-1,sizeof(g));
        for(int i=1;i<=N;i++) {
            int h,m,a,b,c,d;
            scanf("%d:%d %d %d %d %d",&node[i].h,&node[i].m,&node[i].a,&node[i].b,&node[i].c,&node[i].d);
            node[i].t=node[i].h*60+node[i].m;
        }
        for(int i=1;i<=N;i++) {
            for(int j=i+1;j<=N;j++) {
                line[i][j]=is_link(node[i],node[j]);
            }
        }
        int ans=N;
        for(int i=1;i<=N;i++) {
            memset(used,0,sizeof(used));
            if(found(i))    ans--;
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

Taxi Cab Scheme POJ - 2060 二分图最小路径覆盖

标签:math   false   and   eof   ret   container   scheme   note   坐标   

原文地址:https://www.cnblogs.com/cautx/p/11617388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!