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

Taxi Cab Scheme

时间限制: 1 Sec  内存限制: 64 MB

题目描述

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.node

给你N个出租车的预约单表,有初始时间,起点和终点。问最少用多少辆出租车能够知足这N个预订单。ios

输入

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.算法

输出

For each scenario, output one line containing the minimum number of cabs required to carry out all the booked taxi rides.网络

样例输入

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

样例输出

1 2

提示

 

 一样的转化为图G=(V,E),则问题转化为:

在图G中选取尽量少的点,使得图中每一条边至少有一个端点被选中。

这个问题在二分图问题中被称为最小点覆盖问题。即用最少的点去覆盖全部的边。

结论:由König定理可知最小点覆盖的点数 = 二分图最大匹配ide



    König定理是一个二分图中很重要的定理,它的意思是,一个二分图中的最大匹配数等于这个图中的最小点覆盖数。若是你还不知道什么是最小点覆盖,我也在这里说一下:假如选了一个点就至关于覆盖了以它为端点的全部边,你须要选择最少的点来覆盖全部的边。好比,下面这个图中的最大匹配和最小点覆盖已分别用蓝色和红色标注。它们都等于3。这个定理相信大多数人都知道,可是网络上给出的证实并很少见。有一些网上常见的“证实”明显是错误的。所以,我在这里写一下这个定理的证实,但愿对你们有所帮助。


    假如咱们已经经过匈牙利算法求出了最大匹配(假设它等于M),下面给出的方法能够告诉咱们,选哪M个点能够覆盖全部的边。

    匈牙利算法须要咱们从右边的某个没有匹配的点,走出一条使得“一条没被匹配、一条已经匹配过,再下一条又没匹配这样交替地出现”的路(交错轨,增广路)。可是,如今咱们已经找到了最大匹配,已经不存在这样的路了。换句话说,咱们能寻找到不少可能的增广路,但最后都以找不到“终点是尚未匹配过的点”而失败。咱们给全部这样的点打上记号:从右边的全部没有匹配过的点出发,按照增广路的“交替出现”的要求能够走到的全部点(最后走出的路径是不少条不完整的增广路)。那么这些点组成了最小覆盖点集:右边全部没有打上记号的点,加上左边已经有记号的点。看图,右图中展现了两条这样的路径,标记了一共6个点(用 “√”表示)。那么,用红色圈起来的三个点就是咱们的最小覆盖点集。

    首先,为何这样获得的点集点的个数刚好有M个呢?答案很简单,由于每一个点都是某个匹配边的其中一个端点。若是右边的哪一个点是没有匹配过的,那么它早就当成起点被标记了;若是左边的哪一个点是没有匹配过的,那就走不到它那里去(不然就找到了一条完整的增广路)。而一个匹配边又不可能左端点是标记了的,同时右端点是没标记的(否则的话右边的点就能够通过这条边到达了)。所以,最后咱们圈起来的点与匹配边一一对应。

    其次,为何这样获得的点集能够覆盖全部的边呢?答案一样简单。不可能存在某一条边,它的左端点是没有标记的,而右端点是有标记的。缘由以下:若是这条边不属于咱们的匹配边,那么左端点就能够经过这条边到达(从而获得标记);若是这条边属于咱们的匹配边,那么右端点不多是一条路径的起点,因而它的标记只能是从这条边的左端点过来的(想一想匹配的定义),左端点就应该有标记。

    最后,为何这是最小的点覆盖集呢?这固然是最小的,不可能有比M还小的点覆盖集了,由于要覆盖这M条匹配边至少就须要M个点(再次回到匹配的定义)。

    证完了。
 
题解:

这道题乍一看不知道怎么作,可是仔细想想,就能够发现若是一辆车能够在另一个乘客出发以前赶到出发点,就能够将这两个乘客之间连一条单向边,从而能够用二分图来解决。
题目中的小时和分钟混在了一块儿,比较烦人,可是能够将小时化成分钟,再定义一个函数来求时间就能够了。
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
using namespace std;
int n,t;
struct node
{
    int time,time2;
    int x1,y1,x2,y2;
}a[501];
int f[501][501],match[501],vis[501],dfscnt;
int suan(int x1,int y1,int x2,int y2)
{
    return abs(x2-x1)+abs(y2-y1);
}
bool dfs(int root)
{
    int i;
    for(i=1;i<=n;i++)
    {
        if(f[root][i])
        {
            if(vis[i]!=dfscnt)
            {
                vis[i]=dfscnt;
                if(!match[i]||dfs(match[i]))
                {
                    match[i]=root;
                    return 1;
                }
            }
        }
    }
    return 0;
}
int main()
{
    int i,j;
    scanf("%d",&t);
    while(t--)
    {
        memset(f,0,sizeof(f));
        memset(match,0,sizeof(match));
        memset(vis,0,sizeof(vis));
        dfscnt=0;
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            int hour,minute;
            scanf("%d:%d",&hour,&minute);
            a[i].time=hour*60+minute;
            scanf("%d%d%d%d",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
            a[i].time2=a[i].time+suan(a[i].x1,a[i].y1,a[i].x2,a[i].y2);
        }
        for(i=1;i<=n;i++)
        {
            for(j=i;j<=n;j++)
            {
                if(a[i].time2+suan(a[i].x2,a[i].y2,a[j].x1,a[j].y1)<a[j].time)
                f[i][j]=1;
            }
        }
        int ans=0;
        for(i=1;i<=n;i++)
        {
            dfscnt++;
            if(dfs(i))ans++;
        }
        printf("%d\n",n-ans);
    }
}
相关文章
相关标签/搜索