poj2060——Taxi Cab Scheme(最小路径覆盖)

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

题意是说有n个出车安排,一辆车能接到这个安排的条件是:1、这辆车第一次发车;2、这辆车接了上一个安排,回到这个安排的起点的时间正好是这个安排的前一分钟或者更早

每一次安排有五个输入数据,第一个是发车时间,2、3是起点位置,4、5是终点位置,因此计算每两个安排之间的时间差可以用第一个的最后两个数和第二个的第二和三个数。我一开始就是这里没明白才不知道怎么算两个安排之间的关系

接下来就是用二分图,把每个安排都放在二分图的两个点集上,显然两个相同的任务之间不会有边,只有符合题意的两个不同的任务可以连一条边

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
#include <set>
#include <cstring>
#include <string>
#define MAXN 510
#define inf 0xffffffff
using namespace std;
struct Node
{
    int a,b,c,d;
    int begin;
};
Node s[MAXN];
int n;
int get_map(int i,int j)
{
    if(i==j)
        return 0;
    int tmp=abs(s[i].a-s[i].c)+abs(s[i].b-s[i].d)+s[i].begin;
    tmp+=abs(s[i].c-s[j].a)+abs(s[i].d-s[j].b);
    if(tmp<s[j].begin)
        return 1;
    else
        return 0;
}
bool vis[MAXN];
int map[MAXN][MAXN],pre[MAXN];  //匹配路径;
int find(int cur)
{
    for(int i=1; i<=n; ++i)
    {
        if(!vis[i]&&map[cur][i])
        {
            vis[i] = true;
            if(pre[i] == 0 || find(pre[i]))
            {
                pre[i] = cur;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int t,h,m;
    scanf("%d",&t);
    while(t--)
    {
        memset(map,0,sizeof(map));
        scanf("%d",&n);
        for(int i=1; i<=n; ++i)
        {
            scanf("%d:%d",&h,&m);
            s[i].begin=h*60+m;
            scanf("%d%d%d%d",&s[i].a,&s[i].b,&s[i].c,&s[i].d);
        }
        for(int i=1; i<=n; ++i)
            for(int j=1; j<=n; ++j)
                map[i][j]=get_map(i,j);
        memset(pre,0,sizeof(pre));
        int num=0;
        for(int i=1; i<=n; ++i)
        {
            memset(vis,0,sizeof(vis));
            if(find(i))
                num++;
        }
        printf("%d\n",n-num);
    }
    return 0;
}
时间: 2024-08-27 06:17:27

poj2060——Taxi Cab Scheme(最小路径覆盖)的相关文章

poj 2060 Taxi Cab Scheme 最小路径覆盖

//二分匹配的最小路径覆盖 //对于第i次ride,如果在第i次ride结束后还能在第j次ride出发前赶到第j次的出发点 //那么i到j就有一条边 //根据最小路径覆盖 = N - 最大匹配即可得到答案 #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; const int maxn  = 510; int line[max

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

题目链接:https://vjudge.net/problem/UVALive-3126 题解: 代码如下: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <string> 6 #include <vector> 7 #include <map> 8 #include <se

hdu1350Taxi Cab Scheme (最小路径覆盖)

Taxi Cab Scheme Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 712 Accepted Submission(s): 337 Problem Description Running a taxi station is not all that simple. Apart from the obvious demand f

二分图最小路径覆盖--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

UVA 1201 - Taxi Cab Scheme(二分图匹配+最小路径覆盖)

UVA 1201 - Taxi Cab Scheme 题目链接 题意:给定一些乘客,每个乘客需要一个出租车,有一个起始时刻,起点,终点,行走路程为曼哈顿距离,每辆出租车必须在乘客一分钟之前到达,问最少需要几辆出租车 思路:如果一辆车载完一个乘客a,能去载乘客b,就连一条有向边,这样做完整个图形成一个DAG,然后要求的最少数量就是最小路径覆盖,利用二分图最大匹配去做,把每个点拆成两点,如果有边就连边,做一次最大匹配,n - 最大匹配数就是答案 代码: #include <cstdio> #inc

POJ 2060 Taxi Cab Scheme【最小路径覆盖】

T - Taxi Cab Scheme Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2060 Appoint description:  System Crawler  (2014-08-22) Description Running a taxi station is not all that simple. Apart from

【HDU1960】Taxi Cab Scheme(最小路径覆盖)

Taxi Cab Scheme Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 231    Accepted Submission(s): 142 Problem Description Running a taxi station is not all that simple. Apart from the obvious deman

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

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 whi

UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32568 [思路] DAG的最小路径覆盖. 将每个人看做一个结点,如果时间允许到达就连边,则问题转化为DAG上的最小路径覆盖问题,即找到最少的路径使得每个点位于一条路径上. 算法:将DAG中的每个结点u拆分成2个为u1,u2,如果DAG中有边uv则连边u1-v2.如果该二分图的最大匹配数为ans,则答案为n-ans.可以这样想:在一条路径中除尾结点外其他结点都有且仅