二分图最少路径覆盖

http://poj.org/problem?id=2060

Taxi Cab Scheme
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5691   Accepted: 2386

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

最小路径覆盖:在途中找出最少的路径覆盖住途中的所有点,并且每个顶点只与一条路经相关联

题意:

给出每辆客车的出发时间,出发坐标和终点坐标,问至少派出多少辆客车可以满足所有乘客的要求;

两点之间的时间定义为:曼哈顿距离;

分析:如果某辆车的出发时间+该车的路程总时间+从该路线的终点到另一辆车的起点的时间+1<=另一辆车的出发时间,则把两点连在一块,注意是单项边

求最小路径覆盖=总点数-最大匹配数;

程序:

#include"string.h"
#include"stdio.h"
#include"iostream"
#include"queue"
#include"string"
#include"map"
#define M 555
#define inf 999999999
using namespace std;
int Fabs(int x)
{
    if(x<0)
        x=-x;
    return x;
}
int G[555][555],y[555],use[555],x[555];
int finde(int u,int n)
{
    int i;
    for(i=1;i<=n;i++)
    {
        if(!use[i]&&G[u][i])
        {
            use[i]=1;
            if(y[i]==0||finde(y[i],n))
            {
                y[i]=u;
                x[u]=i;
                return 1;
            }
        }
    }
    return 0;
}
int max_match(int n)
{
    memset(y,0,sizeof(y));
    memset(x,0,sizeof(x));
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        if(!x[i])
        {
            memset(use,0,sizeof(use));
            ans+=finde(i,n);
        }
    }
    return ans;
}
int main()
{
    int T,n,i,j;
    int x1[M],y1[M],x2[M],y2[M],hh,mm,tt[M];
    cin>>T;
    while(T--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d:%d%d%d%d%d",&hh,&mm,&x1[i],&y1[i],&x2[i],&y2[i]);
            tt[i]=hh*60+mm;
        }
        memset(G,0,sizeof(G));
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                int t1=Fabs(x1[i]-x2[i])+Fabs(y1[i]-y2[i])+Fabs(x1[j]-x2[i])+Fabs(y1[j]-y2[i]);
                if(tt[i]+t1+1<=tt[j])
                    G[i][j]=1;
            }
        }
        printf("%d\n",n-max_match(n));
    }
}


posted @ 2014-05-20 19:10  一样菜  阅读(191)  评论(0编辑  收藏  举报