UVa live3126Taxi Cab Scheme(二分最大匹配之最小路径覆盖)

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1127

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 pos-sible, 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 bthat are the coordinates of the source address and two integers c dthat 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
大白书上的练习题。DAG图上的最小路径覆盖。大体思路是如果第i辆出租车完全可以在运完这次客人后再运第j次客人,那么就连一条边,由此可知,图是有向图。然后求最小路径覆盖即可。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
int n, link[600], vis[600], head[600], cnt;
struct node
{
    int u, v;
    int next;
} edge[300000];
struct N
{
    int x1, y1, x2, y2, x, y;
}zuo[600];
void add(int u, int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int dfs(int u)
{
    int i, v;
    for(i=head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].v;
        if(!vis[v])
        {
            vis[v]=1;
            if(link[v]==-1||dfs(link[v]))
            {
                link[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
void hungary()
{
    int i, ans=0;
    memset(link,-1,sizeof(link));
    for(i=0;i<n;i++)
    {
        memset(vis,0,sizeof(vis));
        if(dfs(i))
            ans++;
    }
    printf("%d\n",n-ans);
}
int main()
{
    int t,i, j, m1, m2;
    scanf("%d",&t);
    while(t--)
    {
        cnt=0;
        memset(head,-1,sizeof(head));
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d:%d%d%d%d%d",&zuo[i].x,&zuo[i].y,&zuo[i].x1,&zuo[i].y1,&zuo[i].x2,&zuo[i].y2);
            m1=zuo[i].x*60+zuo[i].y;
            for(j=0;j<i;j++)
            {
                m2=zuo[j].x*60+zuo[j].y;
                if(abs(zuo[j].x1-zuo[j].x2)+abs(zuo[j].y1-zuo[j].y2)+abs(zuo[i].x1-zuo[j].x2)+abs(zuo[i].y1-zuo[j].y2)<m1-m2)
                {
                    add(j,i);
                }
                else if(abs(zuo[i].x1-zuo[i].x2)+abs(zuo[i].y1-zuo[i].y2)+abs(zuo[j].x1-zuo[i].x2)+abs(zuo[j].y1-zuo[i].y2)<m2-m1)
                {
                    add(i,j);
                }
            }
        }
        hungary();
    }
    return 0;
}


你可能感兴趣的:(编程,算法,C语言,uva,二分图匹配)