MENU

UVA1201 Taxi Cab Scheme(最小路径覆盖)

2018 年 08 月 30 日 • 阅读: 1124 • 图论阅读设置

Taxi Cab Scheme

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 7529 Accepted: 3083

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

Source

Northwestern Europe 2004


链接

https://cn.vjudge.net/problem/UVA-1201

题意

给定一些客人的出行计划(开始时间,起点位置,终点位置),派最少的出租车去接这些客人。出租车接人的条件是:要么这位客人是着凉出租车接送的第一个人,要么在接送完上一个客人后,至少提前一分钟时间到达新客人的位置。

题解

建图,转化为DAG的最小路径覆盖。

最小路径覆盖,就是在图中找尽量少的路径,使得每个结点恰好在一条路径上(即不同的路径不能有公共点),单独的结点也可以作为一条路径。

本题中,“时间”是一个天然的序,因此可以构图如下:每个客人作为一个结点,如果同一个出租车在接完客人u后来得及接客人v,那么连边$u \rightarrow v$,不难发现,这个图是一个DAG,并且它的最小路径覆盖就是答案。

最小路径覆盖等于总点数减去最大匹配,详情查看:传送门

注意,在建图的时候,条件应该是出租车接完客人u的时间加上u的终点到达v的时间小于v的开始时间。

代码

StatusAccepted
Time343ms
Memory2160kB
Length2002
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 510;
const int maxm = 250010;
using  namespace std;

struct Edge
{
    int to, next;
}edge[maxm<<1];
int head[maxn], tot;
int linker[maxn];
bool used[maxn];

struct Node
{
    int hh, mm;
    int x1, y1, x2, y2;
}node[maxn];

void init()
{
    tot = 0;
    memset(head, -1, sizeof(head));
}

void addedge(int u, int v)
{
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}

bool dfs(int u)
{
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].to;
        if (!used[v])
        {
            used[v] = true;
            if (linker[v] == -1 || dfs(linker[v]))
            {
                linker[v] = u;
                return true;
            }
        }
    }
    return false;
}

int hungary(int n) // 最大匹配
{
    int res = 0;
    memset(linker, -1, sizeof(linker));
    for (int u = 0; u < n; ++u)
    {
        memset(used, 0, sizeof(used));
        if (dfs(u))
            ++res;
    }
    return res;
}

int time_hours(Node a)
{
    return a.hh * 60 + a.mm;
}

int time(Node a, Node b)
{
    return abs(a.x1 - a.x2) + abs(a.y1 - a.y2) + abs(a.x2 - b.x1) + abs(a.y2 - b.y1);
}

int main()
{
//    freopen("/Users/taifu/Codes/NOW/data.in", "r", stdin);
//    freopen("/Users/taifu/Codes/NOW/data.out","w",stdout);
    int T, m;
    scanf("%d", &T);
    while (T--)
    {
        init();
        scanf("%d", &m);
        for (int i = 0; i < m; ++i)
            scanf("%d:%d %d%d%d%d", &node[i].hh, &node[i].mm, &node[i].x1, &node[i].y1, &node[i].x2, &node[i].y2);
        for (int i = 0; i < m; ++i)
        {
            for (int j = i + 1; j < m; ++j)
            {
                if (time_hours(node[i]) + time(node[i], node[j]) < time_hours(node[j])) // 符合条件,连边
                    addedge(i, j);
            }
        }
        int ans = m - hungary(m); // 点数-最大匹配数
        printf("%d\n", ans);
    }
    return 0;
}

因为边集数组开小了导致连边错误,找了一上午BUG。


The end.
2018-08-30 星期四
最后编辑于: 2018 年 09 月 01 日